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

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配置:
    intents = discord.Intents.default()
    intents.members = True
    intents.message_content = True  # 新增此行
    
    同时需在Discord开发者门户的"Privileged Gateway Intents"中开启"Message Content Intent"。
  • 修正代码缩进:原代码中bot.run(TOKEN)前有多余缩进,需改为顶格,否则代码不会执行启动逻辑。
  • 确认权限生效:若修改后仍无响应,重新生成机器人邀请链接(确保包含所需权限),并重新邀请机器人到服务器。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.01 01:15:31