[英]Discord.py How to mention user?
我試圖制作一個代碼,當有人做出反應並且它可以工作時發送消息,但我希望提及做出反應的用戶,我嘗試這樣做,如下所示,但它不起作用。
@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!*")
如果要提及作者,請使用
@bot.command()
async def pingtest(ctx):
channel = bot.get_channel(981973830325116998)
author = ctx.author.mention
await channel.send(author + " is cool!")
如果您想提及其他用戶:
@bot.command()
async def pingtest(ctx, member:discord.Member):
channel = bot.get_channel(981973830325116998)
pingmention = member.mention
await channel.send(pingmention + " is cool!")
請注意,如果提到的用戶沒有該頻道的權限,則 ping 可能不起作用。 我希望它有幫助!
如果不使用彼得回答中提到的ctx
,您可以簡單地使用on_reaction_add
事件處理程序中提供的user
object ,如下所示:
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!*")
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.