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

Discord.py机器人无响应且不发送消息问题求助

Discord.py 基础机器人不回复消息问题解决

你的代码存在三个关键问题,导致机器人无法正常响应消息:

  1. 未开启message_content意图
    Discord.py 2.0及以上版本,必须显式开启message_content意图才能读取用户消息的内容,否则msg.content会为空,自然无法触发判断逻辑。

  2. 重写on_message未处理命令
    使用commands.Bot时,重写on_message事件后必须调用await bot.process_commands(msg),否则会阻断机器人的命令处理流程,甚至影响事件的正常触发。

  3. 提前获取频道对象无效
    bot.get_channel(1069982959458848801)在机器人未完成初始化(on_ready事件触发前)调用会返回None,这段代码没有实际作用,可以删除。

修正后的代码如下:

import discord
from discord.ext import commands

# 开启必要的意图
intents = discord.Intents.default()
intents.members = True
intents.message_content = True  # 新增:开启消息内容读取权限

bot = commands.Bot(command_prefix='!', intents=intents)

@bot.event
async def on_ready():
    print("BOT IS ONLINE")

@bot.event
async def on_message(msg):
    # 避免机器人回复自己的消息
    if msg.author == bot.user:
        return
    if msg.content == "hello":
        await msg.channel.send("hello")
    # 必须添加这一行,保证命令系统正常工作
    await bot.process_commands(msg)

bot.run('你的TOKEN')

额外说明:

  • 确保你在Discord开发者门户的机器人页面,已经开启了Message Content Intent权限。
  • 增加了判断msg.author == bot.user,防止机器人无限回复自己的消息。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 03:01:01