dolfies / discord.py-self

A fork of the popular discord.py for user accounts.
https://discordpy-self.rtfd.io/en/latest/
MIT License
683 stars 162 forks source link

Events are not working #266

Closed theAbdoSabbagh closed 2 years ago

theAbdoSabbagh commented 2 years ago

Summary

For some reason when im importing my bot from it's file to main.py, I'm getting an error related to whatever event there is. If all events were removed, I get no error.

Reproduction Steps

I'm really not sure if it has to do with anything with me import the bot from another file but just construct a bot and put some events, and import it from another file. That's what I did.

However, I think it's related to bot.__dict__, cuz I was using it.

UPDATE: After some testing, it turns out it was because of bot.__dict__ that it somehow raised an error for extra_events and such. After removing all instances of bot.__dict__, I faced no issue.

Code

No response

Expected Results

It should import the bot with no issues

Actual Results

Traceback (most recent call last):
  File "C:\Users\aboud\Desktop\My files\Modern GUI\Version 2\darkend rewrite.py", line 13, in <module>
    from utils.selfbot import bot
  File "C:\Users\aboud\Desktop\My files\Modern GUI\Version 2\utils\selfbot.py", line 108, in <module>
    async def botready(): # The code there is just print('bot is on'), it shows whatever line the first event is at.
  File "C:\Users\aboud\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 508, in decorator
    self.add_listener(func, name)
  File "C:\Users\aboud\AppData\Local\Programs\Python\Python310\lib\site-packages\discord\ext\commands\bot.py", line 452, in add_listener
    if name in self.extra_events:
AttributeError: 'Bot' object has no attribute 'extra_events'

[process exited with code 1]

System Information

doesnt work ._. worked thanks to #241

Checklist

Additional Information

No response

dolfies commented 2 years ago

Overriding Bot.__dict__ leads to a lot of issues.

TheOnlyWayUp commented 2 years ago

@Sxvxgee You might want to setattr(class, name, value) instead of bot.__dict__.

bot = commands.Bot()
setattr(bot, 'test', 'foo')
print(bot.test) # equal to 'foo'
print(getattr(bot, test)) # 'foo'

https://docs.python.org/3/library/functions.html#setattr