Skip to content

Commit

Permalink
refactor(player): support unconnected players
Browse files Browse the repository at this point in the history
  • Loading branch information
ooliver1 committed Oct 9, 2022
1 parent 6637a90 commit 6556139
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
11 changes: 8 additions & 3 deletions mafic/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,18 @@ async def fetch_tracks(
self, query: str, search_type: SearchType | str = SearchType.YOUTUBE
) -> list[Track] | Playlist | None:
if self._node is None:
# TODO: raise proper error
raise RuntimeError("No node found.")
_log.warning(
"Unable to use best node, player not connected, finding random node.",
extra={"guild": self._guild_id},
)
node = NodePool.get_random_node()
else:
node = self._node

raw_type: str
if isinstance(search_type, SearchType):
raw_type = search_type.value
else:
raw_type = search_type

return await self._node.fetch_tracks(query, search_type=raw_type)
return await node.fetch_tracks(query, search_type=raw_type)
4 changes: 4 additions & 0 deletions mafic/pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,7 @@ def get_node(cls, *, guild_id: str | int, endpoint: str | None) -> Node:
# TODO: use guild id, endpoint and other stuff like usage to determine node

return choice(list(cls._nodes.values()))

@classmethod
def get_random_node(cls) -> Node:
return choice(list(cls._nodes.values()))

0 comments on commit 6556139

Please sign in to comment.