Discord音乐机器人代码报错求助:TypeError问题修复
修复Discord音乐机器人的TypeError错误
你遇到的TypeError: 'function' object is not subscriptable错误,原因是代码里的os.getenv['TOKEN']写法错误——os.getenv是Python的函数,不是字典,不能用方括号[]来获取环境变量值,必须用圆括号()调用它。
修复后的代码
把出错的那一行替换成:
await bot.start(os.getenv('TOKEN'))
完整的修复后代码如下:
import discord from discord.ext import commands import os, asyncio #import all of the cogs from help_cog import help_cog from music_cog import music_cog intents = discord.Intents.all() bot = commands.Bot(command_prefix='/', intents=intents) bot.remove_command('help') #remove the default help command so that we can write our own async def main(): async with bot: await bot.add_cog(help_cog(bot)) await bot.add_cog(music_cog(bot)) await bot.start(os.getenv('TOKEN')) asyncio.run(main())
额外注意
确保你已经在运行环境中配置了名为TOKEN的环境变量,值为你的Discord机器人令牌。如果环境变量未正确设置,机器人会因令牌为空无法启动。
内容的提问来源于stack exchange,提问作者kittinun pattarakwan
相关产品推荐
相关产品推荐

