Discord机器人开发遇Redeclared 'bot' defined above without usage报错求助
问题原因
- 你在代码第二行执行了
from discord.ext import bot导入,已经将bot作为变量名占用,后续你又自定义了同名的bot = commands.Bot(...)变量,且此前导入的bot模块全程没有被使用,IDE因此抛出重复声明提示。 - 代码中存在隐含错误:使用了
discord.Intents.default()但没有导入discord基础模块,运行时会触发名称错误。
修复方案
- 直接删除无用导入行
from discord.ext import bot即可消除重复声明提示,常规的discord.py机器人开发无需单独导入该模块,使用commands.Bot即可满足需求。 - 在导入区域新增
import discord,解决discord.Intents的名称引用问题。
修复后代码如下:
import discord from discord.ext import commands from discord_slash import SlashCommand, SlashContext from discord.utils import get from discord_slash import cog_ext, SlashContext from discord_slash.utils.manage_commands import create_choice, create_option import pymongo from pymongo import MongoClient import random intents = discord.Intents.default() intents.members = True bot = commands.Bot(command_prefix='!', case_insensitive=True, intents=intents)
内容的提问来源于stack exchange,提问作者BitOfaLegend
相关产品推荐
相关产品推荐

