Discord机器人命令日志如何@执行命令的成员?
问题解决方法
你的代码存在几处问题,导致无法正确@命令执行者:
- 字符串未启用变量解析:日志的
description字符串没有用f-string包裹,{message.author}会被当作纯文本输出,不会解析为实际变量。 - 上下文对象使用错误:在Discord.py的命令函数中,应通过
ctx.author获取命令执行者,而非message.author。 - 缺少用户提及属性:要在消息中@用户,需要调用用户对象的
mention属性,也就是ctx.author.mention。 - 语法错误:
async def ping(ctx)末尾缺少冒号,会导致代码无法正常运行。
修正后的完整代码:
@bot.command() async def ping(ctx): embed = discord.Embed(title = "Bot Latency", description=f"Latency: {round(bot.latency * 1000)}ms") await ctx.reply(embed = embed) channel = bot.get_channel(1376165325950812222) embed = discord.Embed(title="Bot Command Log", description=f"The ~ping command was just executed by {ctx.author.mention}", color=discord.Color.from_rgb(210, 4, 45)) await channel.send(embed = embed)
关键修改说明:
- 给
async def ping(ctx)补充了结尾的冒号 - 给日志的
description字符串添加f前缀,启用f-string变量解析 - 将
message.author替换为ctx.author.mention,实现@命令执行者的效果
内容的提问来源于stack exchange,提问作者Morgan Cain
相关产品推荐
相关产品推荐

