Skip to content

Commit

Permalink
Add an optioonal class builder paraeter cls to `client.get_player()…
Browse files Browse the repository at this point in the history
…`. This allows for the default player to easily be adapted and overwritten. Useful for subclassing and making custom `Player` classes.

Updated version.
  • Loading branch information
EvieePy committed Jan 10, 2019
1 parent d4de540 commit 641b29f
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
requirements.append('sphinxcontrib-websupport')
requirements.append('Pygments')

version = '0.1.80'
version = '0.1.90'

readme = ''
with open('README.rst') as f:
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 (c) EvieePy'
__version__ = '0.1.80a'
__version__ = '0.1.90a'

from .client import Client
from .errors import *
Expand Down
13 changes: 9 additions & 4 deletions wavelink/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,13 +175,16 @@ def get_node_by_shard(self, shard_id: int) -> Optional[Node]:

return sorted(nodes, key=lambda n: len(n.players))[0]

def get_player(self, guild_id: int) -> Optional[Player]:
def get_player(self, guild_id: int, cls=None) -> Optional[Player]:
"""Retrieve a player for the given guild ID. If None, a player will be created and returned.
Parameters
------------
guild_id:
The guild ID to retrieve a player for.
Optional[cls: class]
An optional class to pass to build from, overriding the default :class:`Player` class.
This must be similar to :class:`Player`. E.g a subclass.
Returns
---------
Expand All @@ -195,7 +198,6 @@ def get_player(self, guild_id: int) -> Optional[Player]:
ZeroConnectedNodes
There are no :class:`wavelink.node.Node`'s currently connected.
"""

players = self.players

try:
Expand All @@ -212,6 +214,9 @@ def get_player(self, guild_id: int) -> Optional[Player]:
if not self.nodes:
raise ZeroConnectedNodes('There are not any currently connected nodes.')

if not cls:
cls = Player

shard_options = []
region_options = []
nodes = self.nodes.values()
Expand All @@ -226,7 +231,7 @@ def get_player(self, guild_id: int) -> Optional[Player]:

if not shard_options or region_options:
node = sorted(nodes, key=lambda n: len(n.players))[0]
player = Player(self.bot, guild_id, node)
player = cls(self.bot, guild_id, node)
node.players[guild_id] = player

return player
Expand All @@ -239,7 +244,7 @@ def get_player(self, guild_id: int) -> Optional[Player]:
else:
node = sorted(region_options, key=lambda n: len(n.players))[0]

player = Player(self.bot, guild_id, node)
player = cls(self.bot, guild_id, node)
node.players[guild_id] = player

return player
Expand Down

0 comments on commit 641b29f

Please sign in to comment.