代码之家  ›  专栏  ›  技术社区  ›  thatwillbeallfolks

“discord.ext.commands.cog”没有属性“listener”

  •  1
  • thatwillbeallfolks  · 技术社区  · 1 年前

    我正在使用python制作一个discordbot,并不断出现错误。

    我的代码在齿轮中

    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'
    

    我尝试重新安装discord-pip文件,同时从头开始完全重做所有内容,希望能找到语法错误或未写入的变量,同时在线查找类似问题,但一直找不到任何匹配的内容。如果有人能帮助我,我将不胜感激,我在过去的几年里用python编码过,不记得太多了。非常感谢。

    1 回复  |  直到 1 年前
        1
  •  0
  •   Blitz    1 年前

    不是 commands.cog.listener() , commands.cog 是文件,您需要 commands.Cog 班所以 commands.Cog.listener() .