我的 cogs 文件没有加载到我的主文件中 (discord.py)

My cogs file didn't load into my main file (discord.py)

当我尝试 运行 我的 discord 机器人时,cogs 文件似乎不起作用。我什至尝试在 Discord 中使用负载。我已经看过很多关于齿轮的视频并阅读了文档,但我找不到任何解决方案。任何人都可以发现错误吗? 这是我的齿轮文件

import discord
from discord.ext import commands, tasks
from itertools import cycle


class UpdateCode(commands.Cog) :

  def _init_(self, client) :
    self.client = client

  @ commands.Cogs.listener()

  async def on_ready(self):
    print("We have logged in as {0.user}".format(self.client))
    self.status_swap.start()
    self.status = cycle([
      " Unanswered Question of Life", 
    " Self - Referential Paradox", 
    " Near-infinite density?", 
    " Dark matter ?", 
    " Measurement of the speed of light in one straight line", 
    " Schrodinger's cat ???"
    "436c69636b2074686973206c696e6b20666f72206672656520766275636b7320212121212121203a200a68747470733a2f2f7777772e796f75747562652e636f6d2f77617463683f763d6451773477395767586351 (try to decrypt this)",
    "The light side of Discord is the path of many unnatural abilities"
    ])

  @ tasks.loop(minutes = 5)
  async def status_swap(self):
    await self.client.change_presence(activity = discord.Game(next(self.status)))

def setup(client) :
  client.add_cog(UpdateCode(client))

这是我的主文件

import discord
import os
from discord.ext import commands, tasks
from online import keep_alive

client = commands.Bot(command_prefix = "!")

@ client.command()
async def load(ctx,extension) :
  client.load_extension(f"cogs.{extension}")

@ client.command()
async def unload(ctx,extension) :
  client.unload_extension(f"cogs.{extension}")

for filename in os.listdir('./cogs') :
  if filename.endswith('.py') :
    client.load_extension(f'cogs.{filename[:-3]}')

keep_alive()
client.run(os.getenv('MATH_VAR'))

这是错误

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 606, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/runner/Math-Bot/cogs/update.py", line 6, in <module>
    class UpdateCode(commands.Cog) :
  File "/home/runner/Math-Bot/cogs/update.py", line 11, in UpdateCode
    @ commands.Cogs.listener()
AttributeError: module 'discord.ext.commands' has no attribute 'Cogs'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "main.py", line 18, in <module>
    client.load_extension(f'cogs.{filename[:-3]}')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 678, in load_extension
    self._load_from_module_spec(spec, name)
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 609, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.update' raised an error: AttributeError: module 'discord.ext.commands' has no attribute 'Cogs'

如果你知道如何解决这个错误,请评论我的代码。谢谢

分析错误

分解错误的第一部分:

Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 606, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 848, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/runner/Math-Bot/cogs/update.py", line 6, in <module>
    class UpdateCode(commands.Cog) :
  File "/home/runner/Math-Bot/cogs/update.py", line 11, in UpdateCode

    # First hint which points to your code.
    @ commands.Cogs.listener()

# Second hint which points the problem in your code.
AttributeError: module 'discord.ext.commands' has no attribute 'Cogs'

引用现有代码

其次,你可以参考UpdateCode Cog的定义方式,它是基于command.Cog而不是command.Cogsclass UpdateCode(commands.Cog) 注意缺少的 's'.

结论

根据我提出的前两点,我们可以得出结论,当您通过包含 's'[=37= 来定义 @ commands.Cogs.listener() 装饰器时存在一个小错误]在单词'Cog'.

参考资料