Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Discord.py - how to detect if a user mentions/pings the bot

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!

like image 351
z1234556631 Avatar asked Apr 16 '20 05:04

z1234556631


People also ask

Can discord bots Ping?

As we all know, bots can ping @everyone and @here but they cannot ping roles.


1 Answers

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

like image 55
Andhavarapu Balu Avatar answered Sep 30 '22 12:09

Andhavarapu Balu