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
相关产品推荐
相关产品推荐

