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

Discord.py机器人服务器无响应问题求助及解决说明

问题解决:Discord.py机器人服务器内无响应(私信正常)

问题根源

编写on_message消息处理事件时,遗漏了*@bot.event*装饰器,导致该事件未被Discord.py正确注册,因此机器人无法在服务器频道中监听并响应消息(私信不受影响是因为测试场景下的消息触发逻辑未受该缺失影响)。补回装饰器后,事件被正常注册,机器人可在服务器内正常工作。

修正后的完整代码

import discord.ext
from discord.ext import commands

intents = discord.Intents(guilds=True, messages=True)
bot = commands.Bot(command_prefix='!', intents=intents)

# 机器人就绪通知事件
@bot.event
async def on_ready():
    print('Bot is ready.')

# 消息响应事件
@bot.event
async def on_message(message):
    if message.content == 'hello':
        await message.channel.send('Hi!')
    await bot.process_commands(message)

bot.run(My_Token)

关键修复说明

*@bot.event*是Discord.py注册事件处理函数的必要标识,缺失会导致对应的事件逻辑无法被触发。确保所有自定义事件处理函数上方都添加该装饰器,才能让机器人正常监听并处理对应事件。

内容的提问来源于stack exchange,提问作者unreal-wall-e

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 08:12:13