ChimBot includes a sophisticated spam detection system that automatically identifies and moderates users who send messages too quickly or repeat the same content. The system uses two detection methods:
Rapid Fire Detection
Detects users sending too many messages in a short time period
Repetition Detection
Identifies users sending the same message multiple times
The spam detection system is configured with these constants:
main.py
# Spam detection configurationLIMITE_MENSAJES = 4 # How many messages in a rowTIEMPO_LIMITE = 4 # Within how many secondsLIMITE_REPETICIONES = 3 # How many times the same message# Channel where spam is allowedCANAL_SPAM_ID = 1004171793101230151
Spam detection is disabled in the designated spam channel (CANAL_SPAM_ID), allowing users to freely spam there without consequences.
The detectar_spam_rapido() function tracks message timestamps:
main.py
def detectar_spam_rapido(user_id, timestamp): """Detecta si un usuario está enviando mensajes demasiado rápido""" data = spam_contador[user_id] # Agregar timestamp actual data['mensajes'].append(timestamp) # Limpiar timestamps antiguos (más de TIEMPO_LIMITE segundos) tiempo_limite = timestamp - TIEMPO_LIMITE data['mensajes'] = [t for t in data['mensajes'] if t > tiempo_limite] # Verificar si superó el límite return len(data['mensajes']) >= LIMITE_MENSAJES
How It Works
Track Timestamps: Each message timestamp is stored in a user-specific list
Clean Old Data: Timestamps older than TIEMPO_LIMITE (4 seconds) are removed
Count Recent Messages: If 4+ messages exist in the last 4 seconds, spam is detected
Auto-Reset: The sliding window automatically clears old messages
The detectar_spam_repetido() function analyzes message content:
main.py
def detectar_spam_repetido(user_id, texto): """Detecta si un usuario está enviando el mismo mensaje repetidamente""" data = spam_contador[user_id] # Agregar texto actual data['ultimos_textos'].append(texto.lower().strip()) # Mantener solo los últimos 10 mensajes if len(data['ultimos_textos']) > 10: data['ultimos_textos'].pop(0) # Contar cuántas veces aparece el mismo mensaje if len(data['ultimos_textos']) >= LIMITE_REPETICIONES: ultimo_texto = data['ultimos_textos'][-1] count = data['ultimos_textos'].count(ultimo_texto) return count >= LIMITE_REPETICIONES return False
How It Works
Normalize Text: Convert to lowercase and strip whitespace for comparison
Store History: Keep the last 10 messages from each user
Count Duplicates: Check if the same message appears 3+ times
Sliding Buffer: Automatically maintains a 10-message window per user
When spam is detected, ChimBot automatically applies a timeout:
main.py
async def silenciar_usuario(member, duracion_segundos=10): """Aplica timeout a un usuario por X segundos""" try: duracion = timedelta(seconds=duracion_segundos) await member.timeout(duracion, reason="Spam detectado") return True except discord.Forbidden: print(f"No tengo permisos para silenciar a {member.name}") return False except Exception as e: print(f"Error al silenciar: {e}") return False
The bot requires the Moderate Members permission to timeout users. Without this permission, it will delete the spam message and send a warning instead.