【问题标题】:DIscord.py CogsDIScord.py 齿轮
【发布时间】:2021-04-12 23:31:38
【问题描述】:

我在一个 main.py 中编写了我的机器人。但是,当我查看代码来更改某些内容时,这非常困难。我尝试搜索并发现有关 cogs。我正在尝试使用 cogs 组织我的 discord.py 机器人并收到以下错误

    Traceback (most recent call last):
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 607, in _load_from_module_spec
    spec.loader.exec_module(lib)
  File "<frozen importlib._bootstrap_external>", line 783, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/home/runner/Commander/cogs/Cats.py", line 6, in <module>
    class Images(commands.cog):
TypeError: module() takes at most 2 arguments (3 given)

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

Traceback (most recent call last):
  File "main.py", line 58, in <module>
    Client.load_extension(f'cogs.Cats')
  File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 664, 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 610, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.Cats' raised an error: TypeError: module() takes at most 2 arguments (3 given)

我的 Cats.py 是

import aiohttp
import discord
import asyncio
from discord.ext import commands

class Images(commands.cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.command()
    async def meow(self, ctx):
        async with ctx.channel.typing():
            async with aiohttp.ClientSession() as cs:
                async with cs.get("http://aws.random.cat/meow") as r:
                    data = await r.json()

                    em = discord.Embed(title="Meow")
                    em.set_image(url=data['file'])

                    await ctx.send(embed=em)


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

我正在向 main.py 添加 cogs 使用

# Cogs Start

Client.load_extension(f'cogs.tictactoe')
Client.load_extension(f'cogs.gamble')
Client.load_extension(f'cogs.Cats')

# Cogs End

请帮忙提前谢谢

【问题讨论】:

    标签: python discord discord.py-rewrite discogs-api


    【解决方案1】:

    您的错误来自这一行:

    class Images(commands.cog):
    

    您的类必须继承自 commands.Cog,而不是 commands.cog

    class Images(commands.Cog):
    

    【讨论】:

      猜你喜欢
      • 2020-10-10
      • 1970-01-01
      • 2022-01-05
      • 2020-04-14
      • 2023-03-14
      • 1970-01-01
      • 2023-01-26
      • 2010-11-10
      • 2013-07-10
      相关资源
      最近更新 更多