Skip to content

Commit f946fb4

Browse files
committed
Add _prefix_callable() to enable bot-mentions as prefix for commands
1 parent 0581f4f commit f946fb4

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/main.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,30 @@
1515
"""
1616

1717
intents = discord.Intents.all()
18-
bot = commands.Bot(command_prefix=PREFIX, intents=intents)
18+
19+
20+
# inspired by https://github.com/Rapptz/RoboDanny
21+
# This function will be evaluated for each message
22+
# you can define specific behaviours for different messages or guilds, like custom prefixes for a guild etc...
23+
def _prefix_callable(_bot: commands.Bot, msg: discord.Message):
24+
25+
user_id = _bot.user.id
26+
# way discord expresses mentions
27+
# mobile and desktop have a different syntax how mentions are sent, so we handle both
28+
prefixes = [f'<@!{user_id}> ', f'<@{user_id}> ']
29+
if msg.guild is None: # we're in DMs, using default prefix
30+
prefixes.append(PREFIX)
31+
return prefixes
32+
33+
# TODO: This would be the place to add guild specific custom prefixes
34+
# you've got the current message hence the guild-id which is perfect to store and load prefixes for a guild
35+
# just append them to base and only append the default prefix if there is no custom prefix for that guild
36+
37+
prefixes.append(PREFIX)
38+
return prefixes
39+
40+
41+
bot = commands.Bot(command_prefix=_prefix_callable, intents=intents)
1942

2043

2144
# login message

0 commit comments

Comments
 (0)