我正在嘗試在 docker 容器上構建我的第一個 discord.py 機器人。但是在運行它時,回溯在 cogs_loader 上給了我以下錯誤:
我使用以下文章嘗試修復錯誤,但嘗試使用他們的解決方案重構我的代碼時失敗了:
Bot base.load_extentstion 從未等待錯誤訊息 discord.py
更新 discord.py 后,“RuntimeWarning:從未等待協程‘BotBase.load_extension’”
這是我的“客戶”檔案:
intents = discord.Intents.default()
bot = Bot(owner_id=env.owner_id, command_prefix='.', intents=intents)
async def load_extensions():
await bot.load_extension('bot.cogs.commands')
await bot.load_extension('bot.cogs.owner')
async def main():
await load_extensions()
await bot.start(env.TOKEN)
if __name__ == '__main__':
asyncio.get_event_loop().run_until_complete(main())
這是我__init__.py
的 cogs 檔案(設定):
intents = discord.Intents.default()
bot = Bot(owner_id=env.owner_id, command_prefix='.', intents=intents)
async def setup(bot):
"""
Init bot's cogs
"""
await bot.add_cog(Commands(bot))
await bot.add_cog(Owner(bot))
這是運行時的回溯__main__.py
:
Traceback (most recent call last):
File "D:/GitHub/Mamey_bot/bot/__main__.py", line 83, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "D:/GitHub/Mamey_bot/bot/__main__.py", line 62, in main
await load_extensions()
File "D:/GitHub/Mamey_bot/bot/__main__.py", line 57, in load_extensions
await bot.load_extension('bot.cogs.commands')
TypeError: object NoneType can't be used in 'await' expression
如果我像第一篇文章main()
中那樣重寫:__main__.py
async def main():
async with bot:
await load_extensions()
await bot.start(env.TOKEN)
我會得到不同的回溯:
Traceback (most recent call last):
File "D:/GitHub/Mamey_bot/bot/__main__.py", line 84, in <module>
asyncio.get_event_loop().run_until_complete(main())
File "C:\Program Files\Python38\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "D:/GitHub/Mamey_bot/bot/__main__.py", line 62, in main
async with bot:
AttributeError: __aexit__
我怎樣才能解決這個問題?
如果我在容器中運行機器人時不使用異步函式,機器人將上線,但命令將不起作用。
uj5u.com熱心網友回復:
問題是我的 Dockerfile 安裝了錯誤的 discord.py 版本。
容器內
在 docker 容器RUN python3 -m pip install -U discord.py
中安裝 Discord.py 2.0.1,這就是為什么回溯給我“從未等待 load_extension”的原因,因為我使用的是 1.7.3 的棄用語法。
在 Dockerfile 中使用正確的命令后:RUN python3 -m pip install discord.py==1.7.3
問題消失了,機器人及其命令完美運行。
在我的真實 PC 和重構代碼上
我真正的 PC/OS(在 docker 容器之外)有相反的問題:它安裝了 discord.py 1.7.3,并且重構的代碼使用 discord.py 2.0.1 語法。在 CMD 中運行后py -3 -m pip install -U py-cord
,我在原始問題中共享的重構代碼完美運行。
tl; dr:需要更加小心我正在匯入的版本。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/508498.html