diff --git a/pomice/__init__.py b/pomice/__init__.py index f6e0e41..a54f64a 100644 --- a/pomice/__init__.py +++ b/pomice/__init__.py @@ -8,6 +8,5 @@ from .exceptions import * from .events import * from .filters import * from .objects import * -from .pool import NodePool -from .node import Node +from .node import Node, NodePool from .player import Player diff --git a/pomice/node.py b/pomice/node.py index 66d2574..c6e5f35 100644 --- a/pomice/node.py +++ b/pomice/node.py @@ -1,19 +1,23 @@ import asyncio import json +import random import re import socket -import aiohttp import time from typing import Optional, Type from urllib.parse import quote -from discord.ext import commands +import aiohttp +import discord +from discord.ext import commands from . import __version__, objects, spotify, NodePool from .exceptions import ( InvalidSpotifyClientAuthorization, NodeConnectionFailure, + NodeCreationError, NodeNotAvailable, + NoNodesAvailable, SpotifyAlbumLoadFailed, SpotifyPlaylistLoadFailed, SpotifyTrackLoadFailed, @@ -398,3 +402,64 @@ class Node: ) for track in data["tracks"] ] + + +class NodePool: + """The base class for the node pool. + This holds all the nodes that are to be used by the bot. + """ + + _nodes: dict = {} + + def __repr__(self): + return f"" + + @property + def nodes(self): + """Property which returns a dict with the node identifier and the Node object.""" + return self._nodes + + @property + def node_count(self): + return len(self._nodes.values()) + + @classmethod + def get_node(self, *, identifier: str = None) -> Node: + """Fetches a node from the node pool using it's identifier. + If no identifier is provided, it will choose a node at random. + """ + available_nodes = {identifier: node for identifier, node in self._nodes.items()} + if not available_nodes: + raise NoNodesAvailable('There are no nodes available.') + + if identifier is None: + return random.choice(list(available_nodes.values())) + + return available_nodes.get(identifier, None) + + @classmethod + async def create_node( + self, + bot: Type[discord.Client], + host: str, + port: str, + password: str, + identifier: str, + spotify_client_id: Optional[str] = None, + spotify_client_secret: Optional[str] = None + ) -> Node: + """Creates a Node object to be then added into the node pool. + For Spotify searching capabilites, pass in valid Spotify API credentials. + """ + if identifier in self._nodes.keys(): + raise NodeCreationError(f"A node with identifier '{identifier}' already exists.") + + node = Node( + pool=self, bot=bot, host=host, port=port, password=password, + identifier=identifier, spotify_client_id=spotify_client_id, + spotify_client_secret=spotify_client_secret + ) + + await node.connect() + self._nodes[node._identifier] = node + return node diff --git a/pomice/player.py b/pomice/player.py index 8ff6489..e8cc7ca 100644 --- a/pomice/player.py +++ b/pomice/player.py @@ -8,7 +8,7 @@ from discord.ext import commands from . import events, filters, objects from .exceptions import TrackInvalidPosition -from .pool import NodePool +from .node import NodePool class Player(VoiceProtocol): diff --git a/pomice/pool.py b/pomice/pool.py deleted file mode 100644 index 2ff306f..0000000 --- a/pomice/pool.py +++ /dev/null @@ -1,68 +0,0 @@ -import random -from typing import Optional, Type - -import discord - -from .exceptions import NodeCreationError, NoNodesAvailable -from .node import Node - - -class NodePool: - """The base class for the node pool. - This holds all the nodes that are to be used by the bot. - """ - - _nodes: dict = {} - - def __repr__(self): - return f"" - - @property - def nodes(self): - """Property which returns a dict with the node identifier and the Node object.""" - return self._nodes - - @property - def node_count(self): - return len(self._nodes.values()) - - @classmethod - def get_node(self, *, identifier: str = None) -> Node: - """Fetches a node from the node pool using it's identifier. - If no identifier is provided, it will choose a node at random. - """ - available_nodes = {identifier: node for identifier, node in self._nodes.items()} - if not available_nodes: - raise NoNodesAvailable('There are no nodes available.') - - if identifier is None: - return random.choice(list(available_nodes.values())) - - return available_nodes.get(identifier, None) - - @classmethod - async def create_node( - self, - bot: Type[discord.Client], - host: str, - port: str, - password: str, - identifier: str, - spotify_client_id: Optional[str] = None, - spotify_client_secret: Optional[str] = None - ) -> Node: - """Creates a Node object to be then added into the node pool. - For Spotify searching capabilites, pass in valid Spotify API credentials. - """ - if identifier in self._nodes.keys(): - raise NodeCreationError(f"A node with identifier '{identifier}' already exists.") - - node = Node( - pool=self, bot=bot, host=host, port=port, password=password, - identifier=identifier, spotify_client_id=spotify_client_id, - spotify_client_secret=spotify_client_secret - ) - - await node.connect() - self._nodes[node._identifier] = node - return node