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

discord.py编写的Ping机器人未提及消息且无法运行求助

问题解决:Discord.py Ping命令无法提及用户且运行异常

错误原因

代码同时混用了f-string和.format()语法,且发送的消息字符串未预留用户提及的插入位置,导致提及功能失效;另外打印语句存在拼写错误。

修正后的代码

@client.command()
async def ping(ctx):
    await ctx.send(f'Pong! 🏓 {ctx.author.mention}\nHost Latency: {round(client.latency * 1000)} ms!')
    print(f'TalkBack attempting: Pong! 🏓\nHost Latency: {round(client.latency * 1000)} ms!')

关键修正点

  • 移除多余的.format(ctx.message.author.mention),改用f-string直接嵌入ctx.author.mention(ctx.author与ctx.message.author等价,写法更简洁)
  • 在消息内容中加入用户提及变量,实现@发起命令的用户
  • 修正打印语句里的拼写错误atemping为attempting

额外注意事项

  • 确保client是正确初始化的命令机器人实例,示例:
from discord.ext import commands
import discord

client = commands.Bot(command_prefix='!', intents=discord.Intents.all())
  • 在Discord开发者后台的Bot页面开启Message Content Intent权限,否则机器人无法读取命令内容和正确提及用户

内容的提问来源于stack exchange,提问作者Mahdiyar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 08:00:28