mirror of https://github.com/stijndcl/didier
Use regex for prefixes
parent
69fedb1428
commit
c735124719
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue