Cogs TypeError: object NoneType can’t be used in ‘await’ expression in discord.py

huangapple go评论72阅读模式
英文:

Cogs TypeError: object NoneType can't be used in 'await' expression in discord.py

问题

我一直在为一个个人服务器制作一个Discord机器人。我想使用cogs来将音乐播放功能与主文件分开。
当我加载我的`main.py`文件时,会出现以下错误:
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.cog' 引发了一个错误:TypeError: 对象NoneType不能在'await'表达式中使用

我的`main.py`文件中与cogs相关的代码如下:

#   Cogs
async def load_extensions():
     for filename in os.listdir("./cogs"):
        if filename.endswith(".py"):
            # 剪掉文件名的.py后缀
            await bot.load_extension(f"cogs.{filename[:-3]}")
async def main():
    async with bot:
        await load_extensions()
        await bot.start(os.getenv('TOKEN'))

asyncio.run(main())     
在我的cogs.py文件中:

import os, discord
from discord.ext import commands

class Test(commands.Cog):
    def __init__(self, client):
        self.client = client # 设置client变量以便在cogs中使用
        self.intents = discord.Intents.default()
        self.intents.message_content = True
    
    @commands.command()
    async def command(self, ctx):
        await ctx.send("是的?")


def setup(client):
    client.add_cog(Test(client, ))
起初我遇到了一个有关意图的错误,通过添加`self.intents`来解决了这个问题,但我还没有能够解决这个问题。我在StackOverflow上查找了一些信息,但没有找到与我的问题特别相关的内容。
英文:

I've been working on a discord bot for a personal server. I want to use cogs to separate the music player functionality from the main file.
I am raising this error when I load my main.py file:

discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.cog' raised an error: TypeError: object NoneType can't be used in 'await' expression      

My main.py file code related to cogs is this:

#   Cogs
async def load_extensions():
     for filename in os.listdir("./cogs"):
        if filename.endswith(".py"):
            # cut off the .py from the file name
            await bot.load_extension(f"cogs.{filename[:-3]}")
async def main():
    async with bot:
        await load_extensions()
        await bot.start(os.getenv('TOKEN'))

asyncio.run(main())     

In my cogs.py file:

import os, discord
from discord.ext import commands

class Test(commands.Cog):
	def __init__(self, client):
		self.client = client # sets the client variable so we can use it in cogs
		self.intents = discord.Intents.default()
		self.intents.message_content = True
	
	@commands.command()
	async def command(self, ctx):
		await ctx.send("Yes?")


def setup(client):
	client.add_cog(Test(client, ))

I initially had an error about intents, which was solved by adding self.intents, but I haven't been able to solve this issue. I have utilized StackOverflow, but haven't found anything specific to my issue.

答案1

得分: 0

The function def setup(client) must utilize async/await as it is a coroutine function.

The corrected code is:

    async def setup(client):
        await client.add(Test(bot))
英文:

The function def setup(client) must utilize async/await as it is a coroutine function.

The corrected code is:

    async def setup(client):
        await client.add(Test(bot)

huangapple
  • 本文由 发表于 2023年2月18日 00:23:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定