Discord机器人开发报错求助:TypeError缺失intents参数
Discord机器人报错解决及代码修正
核心问题与修复步骤
缺少Intents参数导致初始化失败
Discord.py v2.0+版本要求创建commands.Bot实例时必须指定intents参数,这是Discord平台的权限机制要求,需要声明机器人需要接收的事件类型。命令装饰器缺失
test命令的bot.command()前面漏了@符号,导致命令无法被机器人注册识别。拼写错误
ctx.messege.delete()里的messege拼写错误,应为message。Token赋值语法错误
原代码中token(notshowingtoken)是无效语法,需要改为正确的变量赋值格式。
修正后的完整代码
import discord from discord.ext import commands # 配置intents,启用消息相关权限 intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_connect(): print("Bot is online") @bot.command() async def test(ctx): await ctx.message.delete() await ctx.send("Hello!") # 替换为你的机器人令牌 token = "你的机器人令牌" bot.run(token)
额外说明
- 除了代码修改,你还需要在Discord开发者门户的机器人页面中,开启Message Content Intent权限,否则机器人无法读取消息内容,
test命令也无法触发。
内容的提问来源于stack exchange,提问作者D3rwin
相关产品推荐
相关产品推荐

