Skip to content

Commit f1815bf

Browse files
Improve the spam detection (#7)
If a user sends a message to three channels in a 5 minute window containing a link or attachment they will be banned. These requirements are a bit loose but we can deal with any false positives manually. I don't think there will be many, if any. The latest spam examples only have image attachments and the images are slightly different so there is no easy way to detect this.
1 parent f083f5e commit f1815bf

File tree

1 file changed

+29
-25
lines changed

1 file changed

+29
-25
lines changed

amulet_discord_bot/bot.py

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import gzip
55
import os
66
from difflib import SequenceMatcher
7+
from datetime import datetime, timedelta, timezone
78

89
import discord
910

@@ -184,33 +185,36 @@ async def _process_message(self, message: discord.Message) -> None:
184185
await self._log(msg)
185186
return
186187

187-
if (
188-
amulet_server is not None
189-
and self.has_link(message_text)
190-
and not self.has_github_link(message_text)
188+
if amulet_server is not None and (
189+
self.has_link(message_text) or message.attachments
191190
):
192-
# remove spam messages
193-
count = 1
194-
for channel in amulet_server.text_channels:
195-
if (
196-
channel.id != channel_id
197-
and channel.permissions_for(author).read_message_history
198-
):
199-
async for other_message in channel.history(limit=30):
200-
other_author = other_message.author
201-
if (
202-
other_author.id == author_id
203-
and SequenceMatcher(
204-
None, message_text, other_message.content
205-
).ratio()
206-
> 0.9
191+
192+
async def is_spam() -> bool:
193+
count = 1
194+
for channel in amulet_server.text_channels:
195+
if (
196+
channel.id != channel_id
197+
and channel.permissions_for(author).read_message_history
198+
):
199+
async for other_message in channel.history(
200+
limit=30,
201+
after=datetime.now(timezone.utc) - timedelta(minutes=5),
207202
):
208-
count += 1
209-
break
210-
if count >= 3:
211-
break
212-
if count >= 3:
213-
await self.ban(author, f"spamming\n{message_text}")
203+
if other_message.author.id == author_id and (
204+
self.has_link(other_message.content)
205+
or other_message.attachments
206+
):
207+
count += 1
208+
break
209+
if count >= 3:
210+
return True
211+
return False
212+
213+
if await is_spam():
214+
reason = f"spamming\nSTART MESSAGE\n{message_text}\nEND MESSAGE"
215+
for attachment in message.attachments:
216+
reason += f"\n{attachment.url}"
217+
await self.ban(author, reason)
214218
return
215219

216220
async def on_message(self, message: discord.Message) -> None:

0 commit comments

Comments
 (0)