from typing import Dict, Iterable, List, Optional, Tuple

from discord import Member, Message


async def apply(
    last_message: Message, recent_messages: List[Message], config: Dict[str, int]
) -> Optional[Tuple[str, Iterable[Member], Iterable[Message]]]:
    """Detects duplicated messages sent by a single user."""
    relevant_messages = tuple(
        msg
        for msg in recent_messages
        if (
            msg.author == last_message.author
            and msg.content == last_message.content
        )
    )

    total_duplicated = len(relevant_messages)

    if total_duplicated > config['max']:
        return (
            f"sent {total_duplicated} duplicated messages in {config['interval']}s",
            (last_message.author,),
            relevant_messages
        )
    return None

bot/rules/duplicates.py
from typing import Dict, Iterable, List, Optional, Tuple

from discord import Member, Message


async def apply(
    last_message: Message, recent_messages: List[Message], config: Dict[str, int]
) -> Optional[Tuple[str, Iterable[Member], Iterable[Message]]]:
    """Detects duplicated messages sent by a single user."""
    relevant_messages = tuple(
        msg
        for msg in recent_messages
        if (
            msg.author == last_message.author
            and msg.content == last_message.content
            and msg.content
        )
    )

    total_duplicated = len(relevant_messages)

    if total_duplicated > config['max']:
        return (
            f"sent {total_duplicated} duplicated messages in {config['interval']}s",
            (last_message.author,),
            relevant_messages
        )
    return None

bot/rules/duplicates.py
diff --git a/bot/rules/duplicates.py b/bot/rules/duplicates.py
index 455764b535..8e4fbc12df 100644
--- a/bot/rules/duplicates.py
+++ b/bot/rules/duplicates.py
@@ -13,6 +13,7 @@ async def apply(
         if (
             msg.author == last_message.author
             and msg.content == last_message.content
+            and msg.content
         )
     )
 
diff --git a/config-default.yml b/config-default.yml
index 25bbcc3c5b..beaf89f2cc 100644
--- a/config-default.yml
+++ b/config-default.yml
@@ -371,7 +371,7 @@ anti_spam:
     rules:
         attachments:
             interval: 10
-            max: 9
+            max: 6
 
         burst:
             interval: 10

Add an `attachments` rule
# Abstract 

We should have an antispam rule filtering small burst of images. 

# Rationale

Currently, when a user posts 4 images in less than 10 seconds without any comment, the `duplicates` rule will trigger. While we still want to be informed when many images are posted, having the `duplicates` rule trigger doesn't make much sense. Besides, if different message content is given for each image, it will only trigger `burst` if more than 9 messages are sent in 10 seconds. 

# Specification

- [ ] Make sure that the `duplicates` filter won't be triggered by messages with images. We can safely skip empty messages with attachments.
- [ ] Create an `images` filter based on `duplicates` that will trigger when more than 3 images are posted in less than 10 seconds. It should ignore the message content. 
