diff options
| author | 2020-03-28 18:03:50 +0200 | |
|---|---|---|
| committer | 2020-03-28 18:03:50 +0200 | |
| commit | 3a9494da375a7aedf5b2c8554ae1cdd0170ba7f1 (patch) | |
| tree | 3610011f24256e4b5ebf8901e57b029bb06ba4e8 | |
| parent | (Webhook Detection): Added `ModLog` fetching property. (diff) | |
(Webhook Detection): Created `delete_and_respond` helper function to handle Webhook URLs.
| -rw-r--r-- | bot/cogs/webhook_remover.py | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/bot/cogs/webhook_remover.py b/bot/cogs/webhook_remover.py index 54222b007..a19f9c196 100644 --- a/bot/cogs/webhook_remover.py +++ b/bot/cogs/webhook_remover.py @@ -1,13 +1,24 @@ +import logging import re -from discord import Message +from discord import Colour, Message from discord.ext.commands import Cog from bot.bot import Bot from bot.cogs.moderation.modlog import ModLog +from bot.constants import Channels, Colours, Event, Icons WEBHOOK_URL_RE = re.compile(r"discordapp\.com/api/webhooks/\d+/\S+/?") +ALERT_MESSAGE_TEMPLATE = ( + "{user}, looks like you posted Discord Webhook URL to chat. " + "I removed this, but we **strongly** suggest to change this now " + "to prevent any spam abuse to channel. Please avoid doing this in future. " + "If you believe this was mistake, please let us know." +) + +log = logging.getLogger(__name__) + class WebhookRemover(Cog): """Scan messages to detect Discord webhooks links.""" @@ -21,13 +32,41 @@ class WebhookRemover(Cog): return self.bot.get_cog("ModLog") async def scan_message(self, msg: Message) -> bool: - """Scan message content to detect Webhook URLs. Return `bool` about does this have webhook URL.""" + """Scan message content to detect Webhook URLs. Return `bool` about does this have Discord webhook URL.""" matches = WEBHOOK_URL_RE.search(msg.content) if matches: return True else: return False + async def delete_and_respond(self, msg: Message, url: str) -> None: + """Delete message and show warning when message contains Discord Webhook URL.""" + # Create URL that will be sent to logs, remove token + parts = url.split("/") + parts[-1] = "xxx" + url = "/".join(parts) + + # Don't log this, due internal delete, not by user. Will make different entry. + self.mod_log.ignore(Event.message_delete, msg.id) + await msg.delete() + await msg.channel.send(ALERT_MESSAGE_TEMPLATE.format(user=msg.author.mention)) + + message = ( + f"{msg.author} ({msg.author.id}) posted Discord Webhook URL " + f"to {msg.channel}. Webhook URL was {url}" + ) + log.debug(message) + + # Send entry to moderation alerts. + await self.mod_log.send_log_message( + icon_url=Icons.token_removed, + colour=Colour(Colours.soft_red), + title="Discord Webhook URL removed!", + text=message, + thumbnail=msg.author.avatar_url_as(static_format="png"), + channel_id=Channels.mod_alerts + ) + def setup(bot: Bot) -> None: """Load `WebhookRemover` cog.""" |