我正在尝试创建一个不和谐机器人,但我陷入了一个无休止的问题循环。在我看过的每个视频中,建议你这样写齿轮加载函数:
async def load_auto():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
但是每次我使用这种形式的齿轮加载时,它都会给我这个错误:
C:UsersgalanAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandsbot.py:618: RuntimeWarning: coroutine 'setup' was never awaited setup(self)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Traceback (most recent call last): File "c:/Users/galan/Desktop/new sambot/main.py", line 118, in <module> asyncio.get_event_loop().run_until_complete(main())
File "C:UsersgalanAppDataLocalProgramsPythonPython38-32libasynciobase_events.py", line 616, in run_until_completereturn future.result()
File "c:/Users/galan/Desktop/new sambot/main.py", line 115, in main await load_auto() File "c:/Users/galan/Desktop/new sambot/main.py", line 16, in load_auto await bot.load_extension(f'cogs.{filename[:-3]}')
TypeError: object NoneType can't be used in 'await' expression
我已经试过不等待命令了。Load_extension导致它给出一个
C:UsersgalanAppDataLocalProgramsPythonPython38-32libsite-packagesdiscordextcommandsbot.py:618: RuntimeWarning: coroutine 'setup' was never awaited
setup(self)
虽然这看起来更好,但它仍然不加载齿轮。它不遵循别人的代码,它似乎是工作的。
这是main.py文件的一部分:
from discord.ext import commands
import discord
import os
import asyncio
intents = discord.Intents.all()
intents.members= True
sambot_var = ('sambot', 'sambot!', 'sambot?')
bot = commands.Bot(command_prefix='$', intents=intents)
async def load_auto():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
bot.load_extension(f'cogs.{filename[:-3]}')
async def main():
await load_auto()
await bot.start('token')
asyncio.get_event_loop().run_until_complete(main())
asyncio.run(main())
和我的一个齿轮:
from discord.ext
import commands
import sys
import discord
import random
sys.path.append("..")
import datetime
import pytz
import re
import asyncio
class Personality(commands.Cog):
def init(self, client): self.client = client
...
async def setup(bot): await bot.add_cog(Personality(bot))
我的问题是:
- 等待bot.load_extension(cogs)实际上不需要等待吗?
- 我哪里出错了?
- 解决方案是什么?
编辑:问题是我有旧的不和谐包ffs。我的代码工作得很好,只是在我的设备上不能正常工作。await bot.load_extension(cog)
的问题是由于我的软件包过时造成的。
这总是最简单的答案。不管怎样,谢谢你回答我的问题。
你的意图是什么再intents.members= True
,虽然你已经使用所有的意图是intents = discord.Intents.all()
。那么,这行代码可以像这样输入:
#main.py
...
bot = commands.Bot(command_prefix='$', intents=discord.Intents.all())
...
#main.py
async def load_cogs():
for filename in os.listdir('./cogs'):
if filename.endswith('.py'):
await bot.load_extension(f'cogs.{filename[:-3]}')
async def main():
await load_cogs()
await bot.start('token')
if __name__ == '__main__':
asyncio.run(main())
# in some cog
class MyCog(commands.Cog):
def __init__(self, bot):
self.bot = bot
async def setup(bot):
await bot.add_cog(MyCog(bot))
重要!在插入别人的代码之前,检查它是否有缩进和空格,这样你以后就不会带着愚蠢的问题去论坛了。
add_cog不是异步函数或协程。这是一个正常的函数。这很容易通过删除await
语句来修复。
前
await bot.add_cog(Personality(bot))
>bot.add_cog(Personality(bot))
编辑.对不起,我忘了回答这个问题,Does await bot.load_extension(cogs) actually not need to be awaited?
这个问题的答案是,从discord.py 2
开始,load_extension被更改为async
函数,因为他们将来可能需要它。所以,现在,你必须等待它。