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

使用discord.py开发的Discord Bot在线但不响应命令问题排查

问题排查与修复方案

你的Bot无法响应消息命令有两个核心问题:

1. 代码缩进错误

on_message函数内,判断消息内容的代码块缩进层级错误,被嵌套在return语句的作用域后,导致这部分逻辑永远不会被执行。修复后的on_message函数:

@client.event
async def on_message(message):
  if message.author == client.user:
    return

  # 调整缩进至与return同级
  if message.content.startswith('$hello'):
    channel = message.channel
    await channel.send('Hello!')

2. 未开启消息内容Intent权限

Discord.py v2.0及以上版本,默认的discord.Intents.default()不包含读取消息内容的权限,需要显式开启:

# 初始化Intent并开启message_content权限
intents = discord.Intents.default()
intents.message_content = True

client = discord.Client(intents=intents)

同时需要登录Discord开发者后台,在Bot页面的Privileged Gateway Intents区域,开启Message Content Intent开关。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.09 19:50:21