使用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
相关产品推荐
相关产品推荐

