From c735124719df9ae86bfc41041a3d79ce8fadbec1 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Wed, 4 Nov 2020 10:40:02 +0100 Subject: [PATCH] Use regex for prefixes --- functions/prefixes.py | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/functions/prefixes.py b/functions/prefixes.py index 4624d3b..e2a984e 100644 --- a/functions/prefixes.py +++ b/functions/prefixes.py @@ -1,28 +1,23 @@ from data.constants import prefixes from discord.ext import commands import os +import re fallback = os.urandom(32).hex() def get_prefix(bot: commands.Bot, message): - content = message.content.lower() mention = "<@!{}>".format(bot.user.id) + regex = r"^({})\s*" - # Used @Didier - if content.startswith(mention): - if content.startswith(mention + " "): - return mention + " " - return mention - - # Used a prefix - for prefix in prefixes: - # Find correct prefix - if content.startswith(prefix): - # Check if a space has to be added to invoke commands - if content.startswith(prefix + " "): - return prefix + " " - return prefix + # Check which prefix was used + for prefix in [*prefixes, mention]: + r = re.compile(regex.format(prefix), flags=re.I) + m = r.match(message.content) + if m: + # match.group() randomly ignores whitespace for some reason + # so use string slicing + return message.content[:m.end()] return fallback