Discord.py How to mention user?

Question:

Im trying to make a code that sends a message when someone react and it works but I would like the user that reacted to be mentioned and I tried to do it as you can see here below but it dont work.

   @bot.event
    async def on_reaction_add(reaction, user_id):
      livechannel = bot.get_channel(1015516535223242762)
      user = await bot.fetch_user(user_id)
      if str(reaction.emoji) == "✅":
        await livechannel.send("@" + user + " *is cool!*")
Asked By: Jellie

||

Answers:

If you want to mention the author, use

@bot.command()
async def pingtest(ctx):
    channel = bot.get_channel(981973830325116998)
    author = ctx.author.mention
    await channel.send(author + " is cool!")

If you want to mention another user:

@bot.command()
async def pingtest(ctx, member:discord.Member):
    channel = bot.get_channel(981973830325116998)
    pingmention = member.mention

    await channel.send(pingmention + " is cool!")

Please note that the ping may won’t work if the mentioned user does not have permission to that channel. I hope it helps!

Answered By: Peter Till

Without using ctx as mentioned in Peter’s answer, you can simply use the user object provided in the on_reaction_add event handler like so:

import discord

# ...

@bot.event
async def on_reaction_add(reaction: discord.Reaction, user: discord.Member | discord.User):
    livechannel: discord.TextChannel = bot.get_channel(1015516535223242762)
    if str(reaction.emoji) == "✅":
        await livechannel.send(user.mention + " *is cool!*")
Answered By: MagicalCornFlake
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.