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!*")
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!
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!*")
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!*")
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!
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!*")