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 data.constants import prefixes
from discord.ext import commands from discord.ext import commands
import os import os
import re
fallback = os.urandom(32).hex() fallback = os.urandom(32).hex()
def get_prefix(bot: commands.Bot, message): def get_prefix(bot: commands.Bot, message):
content = message.content.lower()
mention = "<@!{}>".format(bot.user.id) mention = "<@!{}>".format(bot.user.id)
regex = r"^({})\s*"
# Used @Didier # Check which prefix was used
if content.startswith(mention): for prefix in [*prefixes, mention]:
if content.startswith(mention + " "): r = re.compile(regex.format(prefix), flags=re.I)
return mention + " " m = r.match(message.content)
return mention if m:
# match.group() randomly ignores whitespace for some reason
# Used a prefix # so use string slicing
for prefix in prefixes: return message.content[:m.end()]
# 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
return fallback return fallback