You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Discord机器人命令日志如何@执行命令的成员?

问题解决方法

你的代码存在几处问题,导致无法正确@命令执行者:

  1. 字符串未启用变量解析:日志的description字符串没有用f-string包裹,{message.author}会被当作纯文本输出,不会解析为实际变量。
  2. 上下文对象使用错误:在Discord.py的命令函数中,应通过ctx.author获取命令执行者,而非message.author。
  3. 缺少用户提及属性:要在消息中@用户,需要调用用户对象的mention属性,也就是ctx.author.mention。
  4. 语法错误: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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 00:42:05