didier/didier/utils/discord/prefix.py

31 lines
874 B
Python
Raw Normal View History

import re
from discord import Message
from discord.ext import commands
from didier.data import constants
2022-07-11 22:23:38 +02:00
__all__ = ["get_prefix"]
def get_prefix(client: commands.Bot, message: Message) -> str:
"""Match a prefix against a message
This is done dynamically to allow variable amounts of whitespace,
and through regexes to allow case-insensitivity among other things.
"""
mention = f"<@!?{client.user.id}>"
regex = r"^({})\s*"
# Check which prefix was used
for prefix in [*constants.PREFIXES, mention]:
match = re.match(regex.format(prefix), message.content, flags=re.I)
if match is not None:
# Get the part of the message that was matched
# .group() is inconsistent with whitespace, so that can't be used
return message.content[: match.end()]
# Matched nothing
return "didier"