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

Discord.py机器人仅私信回复,频道内无响应问题求助

解决方案:Discord机器人无法响应频道消息的问题

你的机器人仅能响应私信但无法处理频道消息,核心问题有两个:变量名错误和缺少必要的消息内容权限配置,以下是具体修复步骤:

1. 修正变量名错误

代码中判断消息发送者是否为机器人自身时,使用了未定义的client.user,但你实例化的客户端对象名为bot,需改为bot.user,否则会导致逻辑错误。

2. 启用Message Content Intent

Discord API要求明确开启消息内容意图才能读取频道内的消息文本,默认的Intents.default()不包含该权限,需手动完成两项配置:

  • 在代码中初始化Intents时添加message_content = True
  • 在Discord开发者门户的机器人应用页面,找到「Privileged Gateway Intents」板块,开启「Message Content Intent」并保存设置

修改后的完整代码

import discord

# 初始化权限,开启消息内容读取权限
intents = discord.Intents.default()
intents.message_content = True

bot = discord.Client(intents=intents)


@bot.event
async def on_message(message):
    # 跳过机器人自身发送的消息
    if message.author == bot.user:
        return

    if message.content.startswith('hi'):
        await message.channel.send('#your response here')


bot.run("TOKEN")

内容的提问来源于stack exchange,提问作者Mr.DOT

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 09:30:54