Skip to content

Commit

Permalink
Cleanup stale bot listeners on Client reinitialisation.
Browse files Browse the repository at this point in the history
`Client()` now takes a kwarg only parameter, `bot`, which is an instance of <commands.Bot, commands.AutoShardedBot>.

Fix examples inline with new changes.

Bump version (0, 7, 0)
  • Loading branch information
EvieePy committed May 2, 2020
1 parent 78b0601 commit d8c8a6c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion examples/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ def __init__(self, bot: commands.Bot):
self.bot = bot

if not hasattr(bot, 'wavelink'):
bot.wavelink = wavelink.Client(bot)
bot.wavelink = wavelink.Client(bot=bot)

bot.loop.create_task(self.start_nodes())

Expand Down
2 changes: 1 addition & 1 deletion examples/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __init__(self, bot):
self.bot = bot

if not hasattr(bot, 'wavelink'):
self.bot.wavelink = wavelink.Client(self.bot)
self.bot.wavelink = wavelink.Client(bot=self.bot)

self.bot.loop.create_task(self.start_nodes())

Expand Down
2 changes: 1 addition & 1 deletion examples/playlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def __init__(self, bot):
self.controllers = {}

if not hasattr(bot, 'wavelink'):
self.bot.wavelink = wavelink.Client(self.bot)
self.bot.wavelink = wavelink.Client(bot=self.bot)

self.bot.loop.create_task(self.start_nodes())

Expand Down
2 changes: 1 addition & 1 deletion wavelink/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
__author__ = 'EvieePy'
__license__ = 'MIT'
__copyright__ = 'Copyright 2019-2020 (c) PythonistaGuild'
__version__ = '0.6.3'
__version__ = '0.7.0'

from .client import Client
from .errors import *
Expand Down
22 changes: 22 additions & 0 deletions wavelink/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@
class Client:
"""The main WaveLink client."""

def __new__(cls, *args, **kwargs):
try:
bot = kwargs['bot']
except KeyError:
msg = 'wavelink.Client: bot is a required keyword only argument which is missing.'
raise WavelinkException(msg)

if not isinstance(bot, (commands.Bot, commands.AutoShardedBot)):
msg = f'wavelink.Client expected type <commands.Bot or commands.AutoShardedBot> not {type(bot)}'
raise TypeError(msg)

try:
update_handlers = bot.extra_events['on_socket_response']
except KeyError:
return super().__new__(cls)

for handler in update_handlers:
if isinstance(handler.__self__.__class__, cls.__class__):
bot.remove_listener(handler, 'on_socket_response')

return super().__new__(cls)

def __init__(self, bot: Union[commands.Bot, commands.AutoShardedBot]):
self.bot = bot
self.loop = bot.loop or asyncio.get_event_loop()
Expand Down

0 comments on commit d8c8a6c

Please sign in to comment.