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

使用Python Discord Intents时机器人触发命令无响应问题排查

问题分析与解决

核心问题:代码事件处理函数定义错误

你写的on_message函数是在MyClient类外部定义的,discord.py的客户端只会执行类内部的事件处理方法,所以这个函数根本不会被触发,自然不会响应$hello命令。

修正后的代码

import discord

intents = discord.Intents.default()
intents.message_content = True

class MyClient(discord.Client):
    async def on_ready(self):
        print('Logged on as {0}!'.format(self.user))

    # 将on_message作为类的方法定义在内部
    async def on_message(self, message):
        if message.author == self.user:
            return

        if message.content.startswith('$hello'):
            await message.channel.send('Hello World!')

client = MyClient(intents=intents)
client.run('My Token')

权限排查(代码修复后仍无响应时检查)

  • 确认机器人所在角色拥有发送消息的权限,在服务器角色设置中开启对应权限
  • 确认机器人能访问你发送命令的频道,频道权限需允许机器人查看频道和发送消息

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 19:12:04