2022-06-10 01:48:02 +02:00
|
|
|
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"]
|
|
|
|
|
2022-06-10 01:48:02 +02:00
|
|
|
|
|
|
|
def get_prefix(client: commands.Bot, message: Message) -> str:
|
|
|
|
"""Match a prefix against a message
|
2022-07-13 22:54:16 +02:00
|
|
|
|
2022-06-10 01:48:02 +02:00
|
|
|
This is done dynamically to allow variable amounts of whitespace,
|
|
|
|
and through regexes to allow case-insensitivity among other things.
|
|
|
|
"""
|
2022-06-16 00:29:38 +02:00
|
|
|
mention = f"<@!?{client.user.id}>"
|
2022-06-10 01:48:02 +02:00
|
|
|
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"
|