Discord机器人on_ready正常但!hello命令无响应且无终端输出求助
问题排查:Discord机器人!hello命令无响应
我正在开发一个Discord机器人,先测试基础功能:用户发送!hello命令时,机器人回复“Hello”并在终端打印日志。目前on_ready函数正常运行,能输出机器人名称,但执行!hello后,机器人既不发消息也不打印日志。Discord开发者门户中机器人权限已勾选"Send Messages"、"Read Message History"、"Read Messages/View Channels",且已启用服务器成员意图。
我的代码
import discord from discord.ext import commands intents = discord.Intents.default() intents.members = True bot = commands.Bot(command_prefix='!', intents=intents) TOKEN = "TOKEN" @bot.event async def on_ready(): print(f'Bot connected as {bot.user}') @bot.command(name='hello') async def dosomething(ctx): print(f'Hello command made') await ctx.send("Hello!") bot.run(TOKEN)
解决方法
- 开启消息内容意图:Discord.py v2.x及以上版本需要显式开启
message_content意图才能读取用户消息,否则机器人无法识别命令。修改intents配置:
同时需在Discord开发者门户的"Privileged Gateway Intents"中开启"Message Content Intent"。intents = discord.Intents.default() intents.members = True intents.message_content = True # 新增此行 - 修正代码缩进:原代码中
bot.run(TOKEN)前有多余缩进,需改为顶格,否则代码不会执行启动逻辑。 - 确认权限生效:若修改后仍无响应,重新生成机器人邀请链接(确保包含所需权限),并重新邀请机器人到服务器。
内容的提问来源于stack exchange,提问作者Squidley
相关产品推荐
相关产品推荐

