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

Viewed 24

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?

1 Answers

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())

Related