Discord音乐机器人报RuntimeWarning:BotBase.load_extension协程未被等待
解决Discord机器人加载扩展时的RuntimeWarning问题
问题原因
你遇到的RuntimeWarning是因为Discord.py v2.0及以上版本中,BotBase.load_extension已改为异步协程函数。直接调用该函数而不使用await关键字,会导致协程从未被执行,触发警告。
修复方案
在on_ready异步函数中,为load_extension调用添加await关键字,确保协程被正确执行。修改后的代码如下:
import discord from discord.ext import commands intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='>', intents=intents) bot.lava_nodes = [ { 'host': 'lava.link', 'port': 80, 'rest_uri': f'http://lava.link:80', 'indetifier': 'MAIN', 'password': 'anything', 'region': 'singapore' } ] @bot.event async def on_ready(): print('Bot is ready') # 为异步加载扩展添加await await bot.load_extension('dismusic') await bot.load_extension('dch') bot.run("OTg5OTI5Mjkxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
额外说明
如果你的代码参考了旧版Discord.py教程,注意v2.0版本后很多同步API都已异步化,涉及I/O的操作基本都需要使用await来处理,避免类似的协程未执行警告。
内容的提问来源于stack exchange,提问作者Tomáš T.
相关产品推荐
相关产品推荐

