[SOLVED] How can I make my discord bot reply if its been pinged? (discord.py?

Issue

This Content is from Stack Overflow. Question asked by Pure.

I want my discord bot to reply to a message that contains a mention to the bot. Ive tried multiple ways, and currently im using

@bot.event
async def on_message(message):
    mention = f'<@!{bot.user.id}>'
    if mention in message.content:
        await ctx.reply("You mentioned me")

but it isnt working. Can anyone help me?



Solution

try this out:

@bot.event
async def on_message(message):
    if bot.user.mentioned_in(message):
        await message.channel.send('You mentioned me!', reference=message)

here i used the mentioned_in method to see if the bot was mentioned in the message
if so, then the bot will reply to the message. that’s why there is a reference=message in the send method.

EDIT:
as said in a commment by Hiigari Yoshinova, you will have to use discord.intents, to be able to reply to messages.
this can be done by commands.Bot(intents=discord.Intents.all())


This Question was asked in StackOverflow by Pure. and Answered by hardik singh It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?