如何在Pycord的斜杠命令中实现带用户提及的回复?
在Pycord斜杠命令中实现提及用户的回复
有两种方式可以实现你需要的效果:
1. 使用ctx.reply()关联消息并自动提及
Pycord的ctx.reply()方法和你之前用的discord.py里message.reply()逻辑一致,通过mention_author=True参数可以自动在回复开头提及发起命令的用户,同时消息会以"回复"的形式关联到用户的命令消息:
import discord bot = discord.Bot() @bot.event async def on_ready(): print(f"{bot.user} is ready and online!") @bot.slash_command(name="chat", description="回复用户并提及") async def chat(ctx, msg): await ctx.reply(msg, mention_author=True) bot.run(TOKEN)
2. 在回复内容中手动插入用户提及
如果你不需要消息关联,只是想在回复文本里@用户,可以使用ctx.author.mention获取用户的提及标记,将其拼接在回复内容中:
@bot.slash_command(name="chat", description="回复用户并提及") async def chat(ctx, msg): await ctx.respond(f"{ctx.author.mention} {msg}")
说明
ctx.author.mention会生成<@用户ID>格式的字符串,发送后Discord会自动渲染为@用户的可点击提及。ctx.reply()的mention_author=True参数会自动在回复内容前添加用户提及,同时让消息显示为回复链形式,和你之前用discord.py实现的效果完全一致。
内容的提问来源于stack exchange,提问作者syrok
相关产品推荐
相关产品推荐

