Pycord-Development / pycord

Pycord is a modern, easy to use, feature-rich, and async ready API wrapper for Discord written in Python
https://docs.pycord.dev
MIT License
2.71k stars 457 forks source link

@commands.Cog.listener() raises errors #290

Closed KosmicAnomaly closed 2 years ago

KosmicAnomaly commented 2 years ago

Summary

We currently are unable to setup listeners in Cogs

Reproduction Steps

Try to setup a listener in a Cog

Minimal Reproducible Code

import discord
from discord.ext import commands

class Palindrome(commands.Cog):
    def __init__(self, bot):
        self.BlackHole = bot

    @commands.Cog.listener()
    async def on_message(self, message: discord.Message):
        print(message.content)

def setup(bot):
    bot.add_cog(Palindrome(bot))

Expected Results

Have a functioning listener

Actual Results

Traceback (most recent call last): File "C:\Users\foobar\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\cog.py", line 668, in _load_from_module_spec setup(self) File "C:\Users\foobar\Desktop\Code\DiscordBots\BlackHole\Modules\Fun\Palindrome.py", line 36, in setup bot.add_cog(Palindrome(bot)) File "C:\Users\foobar\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\cog.py", line 550, in add_cog cog = cog._inject(self) File "C:\Users\foobar\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\cog.py", line 474, in _inject bot.add_listener(getattr(self, method_name), name) AttributeError: 'Bot' object has no attribute 'add_listener'

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Users\foobar\Desktop\Code\DiscordBots\BlackHole\Bot.py", line 89, in BlackHole.load_extension(fullpath[:-3]) File "C:\Users\foobar\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\cog.py", line 728, in load_extension self._load_from_module_spec(spec, name) File "C:\Users\foobar\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\cog.py", line 671, in _load_from_module_spec self._remove_module_references(lib.name) File "C:\Users\foobar\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\cog.py", line 617, in _remove_module_references for cmd in self.all_commands.copy().values(): AttributeError: 'Bot' object has no attribute 'all_commands'

Intents

discord.Intents.all()

System Information

Checklist

Additional Context

No response

BobDotCom commented 2 years ago

Can you try with discord.Cog? Also, are you using commands.Bot or discord.Bot?

TheGamerX20 commented 2 years ago

Can you try with discord.Cog? Also, are you using commands.Bot or discord.Bot?

Seems like this only happens when using discord.Bot, as it does not implement add_listener. commands.Bot however does implement add_listener and it works without issues.

should they be implemented into discord.Bot?

ToxicKidz commented 2 years ago

Seems like this only happens when using discord.Bot, as it does not implement add_listener. commands.Bot however does implement add_listener and it works without issues.

should they be implemented into discord.Bot?

This is more of a bug, considering discord.Cog.listener is implemented but only works with commands.Bot.

TheGamerX20 commented 2 years ago

Seems like this only happens when using discord.Bot, as it does not implement add_listener. commands.Bot however does implement add_listener and it works without issues. should they be implemented into discord.Bot?

This is more of a bug, considering discord.Cog.listener is implemented but only works with commands.Bot.

I agree, just didn't want to work on something that may not be merged. cause how was this not caught in discord.py originally lol

ToxicKidz commented 2 years ago

discord.Cog does not exist in discord.py