繁体   English   中英

Discord.py 如何提及用户?

[英]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.

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