aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar ks123 <[email protected]>2020-03-28 19:42:31 +0200
committerGravatar ks123 <[email protected]>2020-03-28 19:42:31 +0200
commit2544670fa38cea1f53147307b6b1e1134265a74f (patch)
tree92760ce05692ba14f1c49a04bc502c7930ddc712
parent(Webhook Detection): Call `on_message` instead repeating code. (diff)
(Webhook Detection): Added grouping to RegEx compilation, removed unnecessary function `scan_message`, moved this content to `on_message` event.
-rw-r--r--bot/cogs/webhook_remover.py17
1 files changed, 4 insertions, 13 deletions
diff --git a/bot/cogs/webhook_remover.py b/bot/cogs/webhook_remover.py
index 5fb676045..afa88ce89 100644
--- a/bot/cogs/webhook_remover.py
+++ b/bot/cogs/webhook_remover.py
@@ -1,6 +1,5 @@
import logging
import re
-import typing as t
from discord import Colour, Message
from discord.ext.commands import Cog
@@ -9,7 +8,7 @@ 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+/?")
+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. "
@@ -32,14 +31,6 @@ class WebhookRemover(Cog):
"""Get current instance of `ModLog`."""
return self.bot.get_cog("ModLog")
- async def scan_message(self, msg: Message) -> t.Tuple[bool, t.Optional[str]]:
- """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, matches[0]
- else:
- return False, None
-
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
@@ -71,9 +62,9 @@ class WebhookRemover(Cog):
@Cog.listener()
async def on_message(self, msg: Message) -> None:
"""Check is Discord Webhook URL in sent message."""
- is_url_in, url = await self.scan_message(msg)
- if is_url_in:
- await self.delete_and_respond(msg, url)
+ matches = WEBHOOK_URL_RE.search(msg.content)
+ if matches:
+ await self.delete_and_respond(msg, "".join(matches.groups()[:-1]) + "xxx")
@Cog.listener()
async def on_message_edit(self, before: Message, after: Message) -> None: