Python开发Discord Bot遇intents参数缺失错误,求修复方案
问题
我正在开发一个基于Python的Discord Bot项目,使用discord.ext.commands模块时出现初始化错误。当前代码仅通过commands.Bot(command_prefix="!")创建Bot实例,运行后抛出报错:TypeError: BotBase.__init__() missing 1 required keyword-only argument: 'intents'。
完整代码
import discord from discord.ext import commands TOKEN = "mytoken" bot = commands.Bot(command_prefix="!") @bot.event async def on_ready(): print(f'{bot.user} succesfully logged in!') @bot.event async def on_message(message): if message.author == bot.user: return if message.content == 'hello': await message.channel.send(f'Hi {message.author}') if message.content == 'bye': await message.channel.send(f'Goodbye {message.author}') await bot.process_commands(message) # Start each command with the @bot.command decorater @bot.command() async def square(ctx, arg): # The name of the function is the name of the command print(arg) # this is the text that follows the command await ctx.send(int(arg) ** 2) # ctx.send sends text in chat @bot.command() async def scrabblepoints(ctx, arg): # Key for point values of each letter score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10} points = 0 # Sum the points for each letter for c in arg: points += score[c] await ctx.send(points) bot.run(TOKEN)
调试输出
% python3 main.py Traceback (most recent call last): File "/Users/jamiemorrissey/Downloads/discord_bot/main.py", line 5, in <module> bot = commands.Bot(command_prefix="!") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ TypeError: BotBase.__init__() missing 1 required keyword-only argument: 'intents'
解决方法
这个错误源于Discord.py v2.0及以上版本的强制要求:初始化commands.Bot时必须传入**intents(权限意图)**参数,用于控制Bot能接收的事件数据范围。
修复步骤
- 创建Intents实例:先用
discord.Intents.default()获取默认权限集,再手动启用message_content意图(默认不开启,但你的代码需要读取消息内容)。 - 传入Intents参数:在创建
commands.Bot实例时,添加intents参数。
修改后的完整代码
import discord from discord.ext import commands TOKEN = "mytoken" # 初始化权限意图,启用消息内容读取权限 intents = discord.Intents.default() intents.message_content = True # 创建Bot实例时传入intents参数 bot = commands.Bot(command_prefix="!", intents=intents) @bot.event async def on_ready(): print(f'{bot.user} successfully logged in!') @bot.event async def on_message(message): if message.author == bot.user: return if message.content == 'hello': await message.channel.send(f'Hi {message.author}') if message.content == 'bye': await message.channel.send(f'Goodbye {message.author}') await bot.process_commands(message) @bot.command() async def square(ctx, arg): print(arg) await ctx.send(int(arg) ** 2) @bot.command() async def scrabblepoints(ctx, arg): score = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 10, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10} points = 0 # 处理大写字母+兼容未知字符,避免报错 for c in arg.lower(): points += score.get(c, 0) await ctx.send(points) bot.run(TOKEN)
额外说明
- 如果Bot需要其他权限(如获取成员列表、服务器变更通知),可以手动开启对应属性,例如
intents.members = True,同时需要在Discord开发者后台的Bot页面开启相应的特权网关意图。 - 代码中额外优化了
scrabblepoints命令:加入arg.lower()处理大写输入,用score.get(c, 0)避免未知字符引发的KeyError,提升稳定性。
内容的提问来源于stack exchange,提问作者Jamie Morrissey
相关产品推荐
相关产品推荐

