Discord机器人运行报错‘bot not defined’问题求助
bot not defined的原因及解决方法 问题概况
开发Discord机器人时,运行后频繁出现error bot not defined错误,已在bot文件中定义了bot实例,相关代码如下:
问题代码(help_cog.py)
import discord from discord.ext import commands async def setup(bot): await bot.load_extension(help_cog(bot)) await bot.load_extension(music_cog(bot)) class help_cog(commands.Cog): def __init__(self, bot): self.bot = bot self.help_message = '''
通用命令:
/help - 显示所有可用命令,帮助各位Blookteers
/p - 查找并播放Blookteers选择的歌曲
/q - 显示当前音乐队列
/skip - 跳过当前播放的歌曲
/clear - 停止音乐并清空队列
/leave - 强制机器人离开语音频道
/pause - 暂停或恢复当前歌曲
/resume - 恢复播放当前歌曲
self.text_channel_text = [] async def on_ready(self): for guild in self.bot.guilds: for channel in guild.text_channels: self.text_channel_text.append(channel) await self.send_to_all(self.help_message) async def send_to_all(self, msg): for text_channel in self.text_channel_text: await text_channel.send(msg) @command.command(name='help', help='Displays all the avalable commands to help out all of you blooketeers!') async def help(self, ctx): await ctx.send(self.help_message)
Bot文件代码(bot.py)
import hikari import lightbulb import discord from discord.ext import commands import os from help_cog import help_cog from music_cog import music_cog intents=discord.Intents.all() TOKEN = os.getenv('DISCORD_TOKEN') bot = lightbulb.BotApp(token=TOKEN, intents=intents, prefix="!") bot.remove_command('help') async def setup(bot): await bot.add_cog(help_cog(bot)) await bot.add_cog(music_cog(bot)) bot.run()
报错核心原因
- 框架混用冲突:同时使用
discord.py和lightbulb(基于hikari)两个不兼容的Discord开发框架,两者的Bot实例、Cog/Plugin体系完全不同,导致方法调用不匹配。 - 语法与逻辑错误:
- help_cog文件中
@command.command拼写错误,应为@commands.command; on_ready方法缩进错误,被嵌套在__init__函数内部,无法被识别为Cog事件;self.help_message字符串未正确闭合,缺失结尾的''';setup函数中错误地直接传入实例化的Cog对象给load_extension,该方法需要传入模块路径而非对象。
- help_cog文件中
- 初始化逻辑缺失:bot文件中的
setup函数未被调用,Cog没有被正确加载到Bot实例中。
解决方法
方案一:改用纯discord.py框架(推荐,适合熟悉discord.py的开发)
修改bot.py文件
import discord from discord.ext import commands import os from help_cog import help_cog from music_cog import music_cog # 必须开启消息内容意图,否则无法响应命令 intents = discord.Intents.all() intents.message_content = True TOKEN = os.getenv('DISCORD_TOKEN') # 使用discord.py原生的Bot类创建实例 bot = commands.Bot(command_prefix="!", intents=intents) bot.remove_command('help') @bot.event async def on_ready(): # 在机器人就绪后加载Cog await bot.add_cog(help_cog(bot)) await bot.add_cog(music_cog(bot)) print(f"已登录为:{bot.user}") bot.run(TOKEN)
修改help_cog.py文件
import discord from discord.ext import commands class help_cog(commands.Cog): def __init__(self, bot): self.bot = bot # 修复字符串闭合问题 self.help_message = '''
通用命令:
/help - 显示所有可用命令,帮助各位Blookteers
/p - 查找并播放Blookteers选择的歌曲
/q - 显示当前音乐队列
/skip - 跳过当前播放的歌曲
/clear - 停止音乐并清空队列
/leave - 强制机器人离开语音频道
/pause - 暂停或恢复当前歌曲
/resume - 恢复播放当前歌曲
''' self.text_channel_text = [] # 给on_ready添加Cog监听器装饰器,修正缩进 @commands.Cog.listener() async def on_ready(self): for guild in self.bot.guilds: for channel in guild.text_channels: self.text_channel_text.append(channel) await self.send_to_all(self.help_message) async def send_to_all(self, msg): for text_channel in self.text_channel_text: # 捕获无权限异常,避免程序崩溃 try: await text_channel.send(msg) except discord.Forbidden: pass # 修正装饰器拼写错误 @commands.command(name='help', help='Displays all the available commands to help out all of you blooketeers!') async def help(self, ctx): await ctx.send(self.help_message) # 支持discord.py的扩展加载方式 async def setup(bot): await bot.add_cog(help_cog(bot))
方案二:改用lightbulb框架(适合基于hikari的开发)
lightbulb的插件体系与discord.py完全不同,需重构代码:
修改bot.py文件
import hikari import lightbulb import os TOKEN = os.getenv('DISCORD_TOKEN') # 使用lightbulb的BotApp创建实例 bot = lightbulb.BotApp(token=TOKEN, intents=hikari.Intents.ALL, prefix="!") # 加载当前目录下的所有lightbulb插件 bot.load_extensions_from("./") bot.run()
修改help_cog.py(改为lightbulb插件格式)
import hikari import lightbulb # 创建lightbulb插件实例 plugin = lightbulb.Plugin("HelpPlugin") # 定义命令,同时支持斜杠命令和前缀命令 @plugin.command @lightbulb.command("help", "显示所有可用命令") @lightbulb.implements(lightbulb.SlashCommand, lightbulb.PrefixCommand) async def help_command(ctx: lightbulb.Context) -> None: help_message = '''
通用命令:
/help - 显示所有可用命令,帮助各位Blookteers
/p - 查找并播放Blookteers选择的歌曲
/q - 显示当前音乐队列
/skip - 跳过当前播放的歌曲
/clear - 停止音乐并清空队列
/leave - 强制机器人离开语音频道
/pause - 暂停或恢复当前歌曲
/resume - 恢复播放当前歌曲
''' await ctx.respond(help_message) # 机器人就绪事件监听 @plugin.listener(hikari.StartedEvent) async def on_started(event: hikari.StartedEvent) -> None: bot = event.app for guild in bot.cache.get_guilds(): for channel in bot.cache.get_guild_channels(guild): if isinstance(channel, hikari.TextChannel): try: await channel.send('''
通用命令:
/help - 显示所有可用命令,帮助各位Blookteers
/p - 查找并播放Blookteers选择的歌曲
/q - 显示当前音乐队列
/skip - 跳过当前播放的歌曲
/clear - 停止音乐并清空队列
/leave - 强制机器人离开语音频道
/pause - 暂停或恢复当前歌曲
/resume - 恢复播放当前歌曲
''') except hikari.ForbiddenError: pass # 插件加载入口 def load(bot: lightbulb.BotApp) -> None: bot.add_plugin(plugin)
内容的提问来源于stack exchange,提问作者James_Hawkes

