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

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

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

问题

  1. 我一直在为一个个人服务器制作一个Discord机器人。我想使用cogs来将音乐播放功能与主文件分开。
  2. 当我加载我的`main.py`文件时,会出现以下错误:
  3. discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.cog' 引发了一个错误:TypeError: 对象NoneType不能在'await'表达式中使用
  4. 我的`main.py`文件中与cogs相关的代码如下:
  5. # Cogs
  6. async def load_extensions():
  7. for filename in os.listdir("./cogs"):
  8. if filename.endswith(".py"):
  9. # 剪掉文件名的.py后缀
  10. await bot.load_extension(f"cogs.{filename[:-3]}")
  11. async def main():
  12. async with bot:
  13. await load_extensions()
  14. await bot.start(os.getenv('TOKEN'))
  15. asyncio.run(main())
  1. 在我的cogs.py文件中:
  2. import os, discord
  3. from discord.ext import commands
  4. class Test(commands.Cog):
  5. def __init__(self, client):
  6. self.client = client # 设置client变量以便在cogs中使用
  7. self.intents = discord.Intents.default()
  8. self.intents.message_content = True
  9. @commands.command()
  10. async def command(self, ctx):
  11. await ctx.send("是的?")
  12. def setup(client):
  13. client.add_cog(Test(client, ))
  1. 起初我遇到了一个有关意图的错误,通过添加`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:

  1. 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:

  1. # Cogs
  2. async def load_extensions():
  3. for filename in os.listdir("./cogs"):
  4. if filename.endswith(".py"):
  5. # cut off the .py from the file name
  6. await bot.load_extension(f"cogs.{filename[:-3]}")
  7. async def main():
  8. async with bot:
  9. await load_extensions()
  10. await bot.start(os.getenv('TOKEN'))
  11. asyncio.run(main())

In my cogs.py file:

  1. import os, discord
  2. from discord.ext import commands
  3. class Test(commands.Cog):
  4. def __init__(self, client):
  5. self.client = client # sets the client variable so we can use it in cogs
  6. self.intents = discord.Intents.default()
  7. self.intents.message_content = True
  8. @commands.command()
  9. async def command(self, ctx):
  10. await ctx.send("Yes?")
  11. def setup(client):
  12. 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:

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

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

The corrected code is:

  1. async def setup(client):
  2. 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:

确定