'discord.ext.commands.cog'没有属性'listener'

问题描述 投票:0回答:1

我正在使用 python 制作一个不和谐的机器人,但仍然出现错误。

我的代码在齿轮中

import discord
from discord.ext import commands
from discord import Member

class ping(commands.cog):
    def __init__(self, client):
        self.client = client
        self._last_member = None

    @commands.cog.listener()
    async def on_ready(self):
        print("Ping.py is ready")

    @commands.command()
    async def ping(self, ctx, member:discord.Member):
        bot_latency = round(self.client.latency * 1000)
        member = member or ctx.author
        await ctx.send(f"Pong! {bot_latency}ms {member.mention}")

async def setup(client):
    await client.add_cog(ping(client))

我的主代码中

import discord
from discord.ext import commands
import os
import asyncio

client = commands.Bot(description="Discord Bot", command_prefix="!", intents = discord.Intents.all())

@client.event
async def on_ready():
    print("The bot is ready for testing!")
    print("-----------------------------")

async def load():
    for filename in os.listdir("./cogs"):
        if filename.endswith(".py"):
            await client.load_extension(f"cogs.{filename[:-3]}")
            print(f"{filename[:-3]} is loaded!")

async def main():
    async with client:
        await load()
        await client.start('BOTTOKEN')

asyncio.run(main())

出现的错误

Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ext\commands\bot.py", line 935, in _load_from_module_spec
    spec.loader.exec_module(lib)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap_external>", line 995, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "c:\Users\user\vsPythonWorkspace\cogs\ping.py", line 5, in <module>
    class ping(commands.cog):
  File "c:\Users\user\vsPythonWorkspace\cogs\ping.py", line 10, in ping
    @commands.cog.listener()
     ^^^^^^^^^^^^^^^^^^^^^
AttributeError: module 'discord.ext.commands.cog' has no attribute 'listener'

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

Traceback (most recent call last):
  File "c:\Users\user\vsPythonWorkspace\main.py", line 25, in <module>
    asyncio.run(main())
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 687, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "c:\Users\user\vsPythonWorkspace\main.py", line 22, in main
    await load()
  File "c:\Users\user\vsPythonWorkspace\main.py", line 17, in load
    await client.load_extension(f"cogs.{filename[:-3]}")
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ext\commands\bot.py", line 1013, in load_extension
    await self._load_from_module_spec(spec, name)
  File "C:\Users\user\AppData\Local\Programs\Python\Python312\Lib\site-packages\discord\ext\commands\bot.py", line 938, in _load_from_module_spec
    raise errors.ExtensionFailed(key, e) from e
discord.ext.commands.errors.ExtensionFailed: Extension 'cogs.ping' raised an error: AttributeError: module 'discord.ext.commands.cog' has no attribute 'listener'

我尝试重新安装不和谐的 pip 文件,并从头开始完全重做所有内容,希望找到语法错误或未写入的变量,并在网上查找类似的问题,但无法找到任何匹配的内容。如果有人可以帮助我,我将不胜感激,我在过去几年前用 python 编写过代码,但不太记得了。谢谢你。

python discord.py bots attributeerror
1个回答
0
投票

不是

commands.cog.listener()
commands.cog
是文件,你需要
commands.Cog
类。所以,
commands.Cog.listener()

© www.soinside.com 2019 - 2024. All rights reserved.