Use regex for prefixes

pull/10/head
Stijn De Clercq 2020-11-04 10:40:02 +01:00
parent 69fedb1428
commit c735124719
1 changed files with 10 additions and 15 deletions

View File

@ -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