Reputation: 93
So I am just wondering, how can I create an event in discord.py where if a user pings the bot it will respond with a message?
I have not found anything concrete anywhere on how to do this and I would appreciate it if someone could help get my feet wet on how to do this. I appreciate it!
Upvotes: 8
Views: 17680
Reputation: 109
I found that the default function discord.User.mentioned_in
works
In order for it to check for our bot, we add client.user
(which is our bot) in front of the function, making it client.user.mentioned_in(message)
. The argument message
, should be the same argument that you gave for the async def on_message(message)
line
Example:
@client.event
async def on_message(message):
if client.user.mentioned_in(message):
await message.channel.send('You mentioned me!')
I'm not sure why but I haven't seen any others use this function though. Maybe they just aren't aware of this, and instead use client.user.id in message.content
Upvotes: 8
Reputation: 31
Some thing that I found to work was:
if str(client.user.id) in message.content:
await message.channel.send('You mentioned me!')
Upvotes: 3
Reputation: 31
@bot.event
async def on_message(message):
mention = f'<@!{bot.user.id}>'
if mention in message.content:
await message.channel.send("You mentioned me")
Upvotes: 3
Reputation: 1829
Discord pings in pure text are done with special strings. Luckily you don't have to generate these yourself as discord.py has user.mention
(documentation). Your clientuser has this too documentation. So we just get the string of our own mention via client.user.mention
Now we just have to check if this particular string is in the message:
@client.event
async def on_message(message):
if client.user.mention in message.content.split():
await message.channel.send('You mentioned me!')
Upvotes: 1