You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 17:15:07