From e858a3682ffe36675570508802f633046b39ebd8 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Fri, 23 Apr 2021 02:55:03 +0300 Subject: Adds Link Suppressing Helper Adds a helper to find and escape links in a message. Signed-off-by: Hassan Abouelela --- bot/utils/helpers.py | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 bot/utils/helpers.py diff --git a/bot/utils/helpers.py b/bot/utils/helpers.py new file mode 100644 index 00000000..74c2ccd0 --- /dev/null +++ b/bot/utils/helpers.py @@ -0,0 +1,8 @@ +import re + + +def suppress_links(message: str) -> str: + """Accepts a message that may contain links, suppresses them, and returns them.""" + for link in set(re.findall(r"https?://[^\s]+", message, re.IGNORECASE)): + message = message.replace(link, f"<{link}>") + return message -- cgit v1.2.3 From 9e03064e9c116b0dd2bcc65b149b7ac9ee389ff3 Mon Sep 17 00:00:00 2001 From: Hassan Abouelela Date: Fri, 23 Apr 2021 02:59:30 +0300 Subject: Suppresses Links In Commands Suppresses links in certain commands that can echo back user input. Signed-off-by: Hassan Abouelela --- bot/exts/easter/egg_decorating.py | 4 +++- bot/exts/evergreen/catify.py | 3 ++- bot/exts/evergreen/fun.py | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bot/exts/easter/egg_decorating.py b/bot/exts/easter/egg_decorating.py index b18e6636..a847388d 100644 --- a/bot/exts/easter/egg_decorating.py +++ b/bot/exts/easter/egg_decorating.py @@ -10,6 +10,8 @@ import discord from PIL import Image from discord.ext import commands +from bot.utils import helpers + log = logging.getLogger(__name__) with open(Path("bot/resources/evergreen/html_colours.json"), encoding="utf8") as f: @@ -65,7 +67,7 @@ class EggDecorating(commands.Cog): if value: colours[idx] = discord.Colour(value) else: - invalid.append(colour) + invalid.append(helpers.suppress_links(colour)) if len(invalid) > 1: return await ctx.send(f"Sorry, I don't know these colours: {' '.join(invalid)}") diff --git a/bot/exts/evergreen/catify.py b/bot/exts/evergreen/catify.py index ae8d54b6..d8a7442d 100644 --- a/bot/exts/evergreen/catify.py +++ b/bot/exts/evergreen/catify.py @@ -6,6 +6,7 @@ from discord import AllowedMentions, Embed, Forbidden from discord.ext import commands from bot.constants import Cats, Colours, NEGATIVE_REPLIES +from bot.utils import helpers class Catify(commands.Cog): @@ -74,7 +75,7 @@ class Catify(commands.Cog): else: string_list.insert(random.randint(0, len(string_list)), random.choice(Cats.cats)) - text = " ".join(string_list) + text = helpers.suppress_links(" ".join(string_list)) await ctx.send( f">>> {text}", allowed_mentions=AllowedMentions.none() diff --git a/bot/exts/evergreen/fun.py b/bot/exts/evergreen/fun.py index 101725da..7152d0cb 100644 --- a/bot/exts/evergreen/fun.py +++ b/bot/exts/evergreen/fun.py @@ -11,6 +11,7 @@ from discord.ext.commands import BadArgument, Bot, Cog, Context, MessageConverte from bot import utils from bot.constants import Client, Colours, Emojis +from bot.utils import helpers log = logging.getLogger(__name__) @@ -83,6 +84,7 @@ class Fun(Cog): if embed is not None: embed = Fun._convert_embed(conversion_func, embed) converted_text = conversion_func(text) + converted_text = helpers.suppress_links(converted_text) # Don't put >>> if only embed present if converted_text: converted_text = f">>> {converted_text.lstrip('> ')}" @@ -101,6 +103,7 @@ class Fun(Cog): if embed is not None: embed = Fun._convert_embed(conversion_func, embed) converted_text = conversion_func(text) + converted_text = helpers.suppress_links(converted_text) # Don't put >>> if only embed present if converted_text: converted_text = f">>> {converted_text.lstrip('> ')}" -- cgit v1.2.3