From ec4e440b0072bf49328a54f766fa0a21df58c847 Mon Sep 17 00:00:00 2001 From: AndyTempel Date: Tue, 13 Mar 2018 16:44:44 +0100 Subject: [PATCH] Customizations and optimizations --- lavalink/AudioTrack.py | 44 ++++++++++++++++++++++++++++----------- lavalink/Client.py | 11 +++++++++- lavalink/PlayerManager.py | 23 +++++++++++++------- lavalink/Utils.py | 1 - lavalink/WebSocket.py | 4 ++-- lavalink/__init__.py | 7 +++---- 6 files changed, 63 insertions(+), 27 deletions(-) diff --git a/lavalink/AudioTrack.py b/lavalink/AudioTrack.py index a08efc96..d4683e12 100644 --- a/lavalink/AudioTrack.py +++ b/lavalink/AudioTrack.py @@ -4,23 +4,43 @@ def __init__(self, message): class AudioTrack: - def build(self, track, requester): + def __init__(self, track, identifier, can_seek, author, duration, stream, + title, uri, requester, enable_msg: bool = True, is_ad: bool = False, quit_after_empty: bool = False, ): + self.track = track + self.identifier = identifier + self.can_seek = can_seek + self.author = author + self.duration = duration + self.stream = stream + self.title = title + self.uri = uri + self.requester = requester + self.enable_msg = enable_msg + self.is_ad = is_ad + self.quit_after_empty = quit_after_empty + + @classmethod + def build(cls, track, requester, enable_msg: bool = True, is_ad: bool = False, quit_after_empty: bool = False): """ Returns an optional AudioTrack """ try: - self.track = track['track'] - self.identifier = track['info']['identifier'] - self.can_seek = track['info']['isSeekable'] - self.author = track['info']['author'] - self.duration = track['info']['length'] - self.stream = track['info']['isStream'] - self.title = track['info']['title'] - self.uri = track['info']['uri'] - self.requester = requester - - return self + _track = track['track'] + identifier = track['info']['identifier'] + can_seek = track['info']['isSeekable'] + author = track['info']['author'] + duration = track['info']['length'] + stream = track['info']['isStream'] + title = track['info']['title'] + uri = track['info']['uri'] + requester = requester + enable_msg = enable_msg + is_ad = is_ad + quit_after_empty = quit_after_empty except KeyError: raise InvalidTrack('an invalid track was passed') + return cls(_track, identifier, can_seek, author, duration, stream, title, + uri, requester, enable_msg, is_ad, quit_after_empty) + @property def thumbnail(self): """ Returns the video thumbnail. Could be an empty string. """ diff --git a/lavalink/Client.py b/lavalink/Client.py index 072c23c7..e4f49a00 100644 --- a/lavalink/Client.py +++ b/lavalink/Client.py @@ -1,5 +1,6 @@ import asyncio import logging + import aiohttp from .PlayerManager import PlayerManager, DefaultPlayer @@ -34,6 +35,14 @@ def __init__(self, bot, log_level=logging.INFO, loop=asyncio.get_event_loop(), h ) self.players = PlayerManager(self, player=player) + @classmethod + def persistent(cls, bot, *args, **kwargs): + try: + return bot.lavalink + except AttributeError: + bot.lavalink = cls(bot, *args, **kwargs) + return bot.lavalink + def register_hook(self, func): if func not in self.hooks: self.hooks.append(func) @@ -42,7 +51,7 @@ def unregister_hook(self, func): if func in self.hooks: self.hooks.remove(func) - async def dispatch_event(self, event: str, guild_id: str, reason: str= ''): + async def dispatch_event(self, event: str, guild_id: str, reason: str = ''): player = self.players[int(guild_id)] if player: diff --git a/lavalink/PlayerManager.py b/lavalink/PlayerManager.py index f65f5344..173a8160 100644 --- a/lavalink/PlayerManager.py +++ b/lavalink/PlayerManager.py @@ -1,8 +1,8 @@ from abc import ABC, abstractmethod from random import randrange -from .Events import * -from .AudioTrack import * +from .AudioTrack import * +from .Events import * __all__ = ["PlayerManager", "BasePlayer", "DefaultPlayer"] @@ -39,6 +39,7 @@ def __init__(self, lavalink, guild_id: int): self.queue = [] self.current = None + self.previous = None @property def is_playing(self): @@ -81,15 +82,23 @@ def fetch(self, key: object, default=None): """ Retrieves the related value from the stored user data """ return self._user_data.get(key, default) - def add(self, requester: int, track: dict): + def add(self, requester: int, track: dict, is_ad: bool = False, enable_msg: bool = True, + quit_after_empty: bool = False): """ Adds a track to the queue """ - self.queue.append(AudioTrack().build(track, requester)) + self.queue.append( + AudioTrack.build(track, requester, is_ad=is_ad, enable_msg=enable_msg, quit_after_empty=quit_after_empty)) + + def add_next(self, requester: int, track: dict, is_ad: bool = False, enable_msg: bool = True, + quit_after_empty: bool = False): + """ Adds a track to the beginning of the queue """ + self.queue.insert(0, AudioTrack.build(track, requester, is_ad=is_ad, enable_msg=enable_msg, + quit_after_empty=quit_after_empty)) async def play(self): """ Plays the first track in the queue, if any """ if self.repeat and self.current is not None: self.queue.append(self.current) - + self.previous = self.current self.current = None self.position = 0 self.paused = False @@ -132,8 +141,8 @@ async def seek(self, pos: int): async def handle_event(self, event): if isinstance(event, (TrackStartEvent, TrackExceptionEvent)) or \ - isinstance(event, TrackEndEvent) and event.reason == 'FINISHED': - await self.play() + isinstance(event, TrackEndEvent) and event.reason == 'FINISHED': + await self.play() class PlayerManager: diff --git a/lavalink/Utils.py b/lavalink/Utils.py index e71e68af..4599667d 100644 --- a/lavalink/Utils.py +++ b/lavalink/Utils.py @@ -1,5 +1,4 @@ class Utils: - @staticmethod def format_time(time): """ Formats the given time into HH:MM:SS """ diff --git a/lavalink/WebSocket.py b/lavalink/WebSocket.py index 3b259b5a..49a74803 100644 --- a/lavalink/WebSocket.py +++ b/lavalink/WebSocket.py @@ -1,8 +1,9 @@ import asyncio -import websockets import json import logging +import websockets + log = logging.getLogger(__name__) @@ -119,6 +120,5 @@ async def send(self, **data): self._queue.append(data) log.debug('Send called before websocket ready; queueing payload\n\t', str(data)) - def destroy(self): self._shutdown = False diff --git a/lavalink/__init__.py b/lavalink/__init__.py index c9bddbea..b3ce9158 100644 --- a/lavalink/__init__.py +++ b/lavalink/__init__.py @@ -3,16 +3,15 @@ __license__ = 'MIT' __copyright__ = 'Copyright 2018 Luke & William' __version__ = '2.1' +__modified__ = "By: Andraž Korenč" +import sys from .AudioTrack import * from .Client import * +from .Events import * from .PlayerManager import * from .Utils import * from .WebSocket import * -from .Events import * - -import logging -import sys log = logging.getLogger(__name__)