aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar kraktus <[email protected]>2019-10-07 18:40:02 +0200
committerGravatar kraktus <[email protected]>2019-10-07 18:40:02 +0200
commit1b6f3d23d4c0b1f6dfe1354a3a210e589f7b4956 (patch)
tree52fa8e646881dbf580d6d1a0177a066d22c92aba
parentMerge pull request #481 from bendiller/fetch_posts_retries (diff)
Make sure that poor code does not contains token
Added a new function `is_token_in_message` in `token_remover`. This function returns a `bool` and if the code contains a token then the embed message about the poorly formatted code is not displayed.
-rw-r--r--bot/cogs/bot.py3
-rw-r--r--bot/cogs/token_remover.py32
2 files changed, 21 insertions, 14 deletions
diff --git a/bot/cogs/bot.py b/bot/cogs/bot.py
index 7583b2f2d..e8ac0a234 100644
--- a/bot/cogs/bot.py
+++ b/bot/cogs/bot.py
@@ -7,6 +7,7 @@ from typing import Optional, Tuple
from discord import Embed, Message, RawMessageUpdateEvent
from discord.ext.commands import Bot, Cog, Context, command, group
+from bot.cogs.token_remover import TokenRemover
from bot.constants import Channels, DEBUG_MODE, Guild, MODERATION_ROLES, Roles, URLs
from bot.decorators import with_role
from bot.utils.messages import wait_for_deletion
@@ -237,7 +238,7 @@ class Bot(Cog):
and len(msg.content.splitlines()) > 3
)
- if parse_codeblock:
+ if parse_codeblock and not TokenRemover.is_token_in_message: # if there is no token in the code
on_cooldown = (time.time() - self.channel_cooldowns.get(msg.channel.id, 0)) < 300
if not on_cooldown or DEBUG_MODE:
try:
diff --git a/bot/cogs/token_remover.py b/bot/cogs/token_remover.py
index 7dd0afbbd..8f356cf19 100644
--- a/bot/cogs/token_remover.py
+++ b/bot/cogs/token_remover.py
@@ -52,19 +52,8 @@ class TokenRemover(Cog):
See: https://discordapp.com/developers/docs/reference#snowflakes
"""
- if msg.author.bot:
- return
-
- maybe_match = TOKEN_RE.search(msg.content)
- if maybe_match is None:
- return
-
- try:
- user_id, creation_timestamp, hmac = maybe_match.group(0).split('.')
- except ValueError:
- return
-
- if self.is_valid_user_id(user_id) and self.is_valid_timestamp(creation_timestamp):
+ if self.is_token_in_message(msg):
+ user_id, creation_timestamp, hmac = TOKEN_RE.search(msg.content).group(0).split('.')
self.mod_log.ignore(Event.message_delete, msg.id)
await msg.delete()
await msg.channel.send(DELETION_MESSAGE_TEMPLATE.format(mention=msg.author.mention))
@@ -86,6 +75,23 @@ class TokenRemover(Cog):
channel_id=Channels.mod_alerts,
)
+ def is_token_in_message(self, msg: Message) -> bool:
+ """Check if `msg` contains a seemly valid token."""
+ if msg.author.bot:
+ return False
+
+ maybe_match = TOKEN_RE.search(msg.content)
+ if maybe_match is None:
+ return False
+
+ try:
+ user_id, creation_timestamp, hmac = maybe_match.group(0).split('.')
+ except ValueError:
+ return False
+
+ if self.is_valid_user_id(user_id) and self.is_valid_timestamp(creation_timestamp):
+ return True
+
@staticmethod
def is_valid_user_id(b64_content: str) -> bool:
"""