使用discord.py开发Discord机器人时!hello命令无法被识别的问题求助
discord.py开发Discord机器人时!hello命令无法被识别的问题求助
我刚看了你的代码和终端报错信息,发现核心问题大概率是Python代码的缩进错误——Python是严格依赖缩进区分代码块的语言,你的事件处理函数和命令函数内部的代码都没有正确缩进,导致机器人无法识别到!hello这个命令。
先看你遇到的报错:
"ERROR discord.ext.commands.bot Ignoring exception in command None
discord.ext.commands.errors.CommandNotFound: Command "hello" is not found"
这个报错说明机器人根本没注册到hello命令,根源就是代码缩进不对。下面是修正后的完整代码,重点注意函数内部代码的缩进:
import discord from discord.ext import commands import config intents = discord.Intents.all() bot = commands.Bot(command_prefix='!', intents=intents) @bot.event async def on_ready(): # 这里必须缩进,属于on_ready函数的内容 print("-----") print("Bot is online!") print("-----") @bot.command() async def hello(ctx): # 这里也必须缩进,属于hello命令函数的内容 await ctx.send("Hello, I am Synth. The bot for this server") bot.run(config.TOKEN)
除了缩进问题,你还可以检查这几个点确保没问题:
- 确认终端已经输出了
Bot is online!,说明机器人成功连接到Discord了 - 检查机器人在你的服务器里是否拥有发送消息的权限(不过当前报错是命令未找到,这个是次要检查项)
- 确认
config.py里的TOKEN是正确的机器人令牌,没有拼写错误
之前你尝试把bot改成client或者去掉@bot.command()的括号,这些操作都没触及真正的问题,修正缩进后应该就能正常触发!hello命令了。
备注:内容来源于stack exchange,提问作者CloudZ
相关产品推荐
相关产品推荐

