简体   繁体   中英

Trigger function on bot mention - discord.py

I am currently working on a discord bot using discord.py. My question is, how can I trigger a function, when I mention the bot. I'm thinking about a if statement, but i don't know, which variables I need to call.

if [mention] == [bot id]:
    # Here comes the code

The rest of the code is working properly. Would nice if I can get a code snippet:)

You can do this in your on_message event/listener.

@bot.event
async def on_message(message):
    if message.author == client.user:
        return

    if bot.user.mentioned_in(message) and message.mention_everyone is False:
        await ctx.send("The message")

The message.mention_everyone part prevents the bot to respond to @everyone or @here and the first if statement prevents from bot responding to itself.

You could use the on_message event to check whether there is a mention in the message or if the whole message is just a mention. A simple if statement should do the trick

if f"<@!{bot.user.id}>" in message.content:
    ctx.channel.send("You mentioned me!")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM