From ea6c2baf3c67236427f415719617d4fd9d439e6c Mon Sep 17 00:00:00 2001 From: cloudwithax Date: Sun, 17 Oct 2021 17:21:24 -0400 Subject: [PATCH] Fix examples to better reflect changes in library --- README.md | 2 +- examples/basic.py | 2 +- pomice/filters.py | 41 +++++++++++++++++++++++++++++++++++++---- pomice/pool.py | 30 +++++++++++++++++++++++++++++- 4 files changed, 68 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 2390e98..7274c5c 100644 --- a/README.md +++ b/README.md @@ -91,7 +91,7 @@ class Music(commands.Cog): player = ctx.voice_client - results = await player.get_tracks(query=f'ytsearch:{search}') + results = await player.get_tracks(query=f'{search}') if not results: raise commands.CommandError('No results were found for that search term.') diff --git a/examples/basic.py b/examples/basic.py index 9f0b646..9bd4b46 100644 --- a/examples/basic.py +++ b/examples/basic.py @@ -75,7 +75,7 @@ class Music(commands.Cog): # i.e: player.get_tracks("query", search_type=SearchType.ytmsearch) # will search up any keyword results on Youtube Music - results = player.get_tracks("query") + results = player.get_tracks(f"{search}") if not results: raise commands.CommandError('No results were found for that search term.') diff --git a/pomice/filters.py b/pomice/filters.py index dc98c63..faa8503 100644 --- a/pomice/filters.py +++ b/pomice/filters.py @@ -1,3 +1,4 @@ +import collections from .exceptions import FilterInvalidArgument @@ -6,6 +7,34 @@ class Filter: self.payload = None +class Equalizer: + """ + Filter which represents a 15 band equalizer. + You can adjust the dynamic of the sound using this filter. + i.e: Applying a bass boost filter to emphasize the bass in a song. + The format for the levels is: List[Tuple[int, float]] + """ + def __init__(self, *, levels: list): + super().__init__() + + self.eq = self._factory(self, levels) + self.raw = levels + + self.payload = {'equalizer': {'bands': self.eq}} + + + def _factory(self, levels: list): + _dict = collections.defaultdict(int) + + _dict.update(levels) + _dict = [{"band": i, "gain": _dict[i]} for i in range(15)] + + return _dict + + def __repr__(self) -> str: + return f"" + + class Timescale(Filter): """Filter which changes the speed and pitch of a track. Do be warned that this filter is bugged as of the lastest Lavalink dev version @@ -78,9 +107,11 @@ class Tremolo(Filter): super().__init__() if frequency < 0: - raise FilterInvalidArgument("Tremolo frequency must be more than 0.") + raise FilterInvalidArgument( + "Tremolo frequency must be more than 0.") if depth < 0 or depth > 1: - raise FilterInvalidArgument("Tremolo depth must be between 0 and 1.") + raise FilterInvalidArgument( + "Tremolo depth must be between 0 and 1.") self.frequency = frequency self.depth = depth @@ -101,9 +132,11 @@ class Vibrato(Filter): super().__init__() if frequency < 0 or frequency > 14: - raise FilterInvalidArgument("Vibrato frequency must be between 0 and 14.") + raise FilterInvalidArgument( + "Vibrato frequency must be between 0 and 14.") if depth < 0 or depth > 1: - raise FilterInvalidArgument("Vibrato depth must be between 0 and 1.") + raise FilterInvalidArgument( + "Vibrato depth must be between 0 and 1.") self.frequency = frequency self.depth = depth diff --git a/pomice/pool.py b/pomice/pool.py index ef4d635..b2a73ba 100644 --- a/pomice/pool.py +++ b/pomice/pool.py @@ -8,6 +8,7 @@ import socket import time from typing import Dict, Optional, Type, TYPE_CHECKING from urllib.parse import quote +from base64 import b import aiohttp import discord @@ -253,6 +254,33 @@ class Node: self.available = False self._task.cancel() + async def build_track( + self, + *, + identifier: str, + ctx: Optional[commands.Context] + ): + """ + Builds a track using a valid track identifier + + You can also pass in a discord.py Context object to get a + Context object on the track it builds. + """ + + async with self.session.get(f'{self._rest_uri}/decodetrack?', + headers={'Authorization': self._password}, + params={'track': identifier}) as resp: + + data: dict = await resp.json() + + if not resp.status == 200: + raise TrackLoadError(f'Failed to build track. Status: {data["status"]}, Error: {data["error"]}.' + f'Check the identifier is correct and try again.') + + track = Track(track_id=identifier, ctx=ctx, info=data) + return track + + async def get_tracks( self, query: str, @@ -480,7 +508,7 @@ class NodePool: """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 cls._nodes.items()} + available_nodes = {identifier: node for identifier, node in cls._nodes.items() if node._available} if not available_nodes: raise NoNodesAvailable('There are no nodes available.')