didier/functions/prefixes.py

24 lines
593 B
Python
Raw Normal View History

from data.constants import prefixes
from discord.ext import commands
import os
2020-11-04 10:40:02 +01:00
import re
fallback = os.urandom(32).hex()
def get_prefix(bot: commands.Bot, message):
mention = "<@!{}>".format(bot.user.id)
2020-11-04 10:40:02 +01:00
regex = r"^({})\s*"
2020-11-04 10:40:02 +01:00
# 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