Discord自机器人开发报错:Intents对象无self_messages属性
问题解决方法
你的错误是因为discord.Intents里根本没有self_messages这个属性,而且混用了discord和selfcord库的组件导致配置错误。以下是修正后的完整代码和关键修改说明:
修正后的代码
import selfcord from selfcord.ext import commands # 使用selfcord专属的Intents配置 intents = selfcord.Intents.default() # 开启消息内容权限(必须开启才能识别命令前缀) intents.message_content = True # 开启消息接收权限 intents.messages = True # 命令前缀设为$,对应你要的$smile指令 bot = commands.Bot(command_prefix="$", self_bot=True, intents=intents) @bot.command(name="smile") async def smile(ctx): await ctx.send("😄") # 仅通过bot.run完成登录,不要重复调用selfcord.login bot.run("MY TOKEN", bot=False)
关键修改点
- 删掉了冗余的
discord库导入,全程使用selfcord的组件,避免库之间的配置冲突 - 把
discord.Intents替换为selfcord.Intents,自机器人的意图配置必须用selfcord的体系 - 移除了不存在的
intents.self_messages = True,换成自机器人处理命令必需的message_content和messages意图 - 命令前缀从"."改成"$",命令名从"troll"改成"smile",完美匹配你要的
$smile触发指令 - 去掉了多余的
selfcord.login("MY TOKEN"),bot.run已经包含登录逻辑,重复调用会导致异常
内容的提问来源于stack exchange,提问作者Myepcischoolstack
相关产品推荐
相关产品推荐

