Adding types and changing some messages from class

This commit is contained in:
Rafael Vargas
2022-03-26 21:24:03 -04:00
parent f9b46e13ff
commit b4159c7e86
16 changed files with 154 additions and 359 deletions

View File

@@ -1,81 +0,0 @@
from abc import ABC, abstractproperty, abstractmethod
class IPlaylist(ABC):
@abstractproperty
def looping_one(self):
pass
@abstractproperty
def looping_all(self):
pass
@abstractproperty
def songs_to_preload(self) -> list:
pass
@abstractmethod
def __len__(self):
pass
@abstractmethod
def next_song(self):
pass
@abstractmethod
def add_song(self, identifier: str) -> None:
pass
@abstractmethod
def shuffle(self) -> None:
pass
@abstractmethod
def revert(self) -> None:
pass
@abstractmethod
def clear(self) -> None:
pass
@abstractmethod
def loop_one(self) -> str:
pass
@abstractmethod
def loop_all(self) -> str:
pass
@abstractmethod
def loop_off(self) -> str:
pass
@abstractmethod
def destroy_song(self, song_destroy) -> None:
pass
class ISong(ABC):
@abstractmethod
def finish_down(self, info: dict) -> None:
pass
@abstractmethod
def source(self) -> str:
pass
@abstractmethod
def title(self) -> str:
pass
@abstractmethod
def duration(self) -> str:
pass
@abstractmethod
def identifier(self) -> str:
pass
@abstractmethod
def destroy(self) -> None:
pass

View File

@@ -1,3 +1,4 @@
import asyncio
from discord.ext import commands
from Config.Config import Configs
from discord import Client, Guild, FFmpegPCMAudio, Embed
@@ -18,7 +19,6 @@ class Player(commands.Cog):
self.__timer = Timer(self.__timeout_handler)
self.__playing = False
self.__config = Configs()
# Flag to control if the player should stop totally the playing
self.__force_stop = False
@@ -68,7 +68,7 @@ class Player(commands.Cog):
song = self.__playlist.next_song()
if song != None:
if song is not None:
coro = self.__play_music(ctx, song)
self.__bot.loop.create_task(coro)
else:
@@ -76,15 +76,15 @@ class Player(commands.Cog):
async def __play_music(self, ctx: Context, song: Song) -> None:
try:
source = self.__ensure_source(song)
if source == None:
source = await self.__ensure_source(song)
if source is None:
self.__play_next(None, ctx)
self.__playing = True
player = FFmpegPCMAudio(song.source, **self.FFMPEG_OPTIONS)
self.__guild.voice_client.play(
player, after=lambda e: self.__play_next(e, ctx))
voice = self.__guild.voice_client
voice.play(player, after=lambda e: self.__play_next(e, ctx))
self.__timer.cancel()
self.__timer = Timer(self.__timeout_handler)
@@ -92,46 +92,12 @@ class Player(commands.Cog):
await ctx.invoke(self.__bot.get_command('np'))
songs = self.__playlist.songs_to_preload
await self.__down.preload(songs)
asyncio.create_task(self.__down.preload(songs))
except:
self.__play_next(None, ctx)
def __format_embed(self, info: dict, title='', position='Playing Now') -> Embed:
embedvc = Embed(
title=title,
description=f"[{info['title']}]({info['original_url']})",
color=self.__config.COLOURS['blue']
)
embedvc.add_field(name=self.__config.SONGINFO_UPLOADER,
value=info['uploader'],
inline=False)
embedvc.add_field(name=self.__config.SONGINFO_REQUESTER,
value=info['requester'],
inline=True)
if 'thumbnail' in info.keys():
embedvc.set_thumbnail(url=info['thumbnail'])
if 'duration' in info.keys():
duration = str(timedelta(seconds=info['duration']))
embedvc.add_field(name=self.__config.SONGINFO_DURATION,
value=f"{duration}",
inline=True)
else:
embedvc.add_field(name=self.__config.SONGINFO_DURATION,
value=self.__config.SONGINFO_UNKNOWN_DURATION,
inline=True)
embedvc.add_field(name=self.__config.SONGINFO_POSITION,
value=position,
inline=True)
return embedvc
async def __timeout_handler(self) -> None:
if self.__guild.voice_client == None:
if self.__guild.voice_client is None:
return
if self.__guild.voice_client.is_playing() or self.__guild.voice_client.is_paused():
@@ -142,9 +108,10 @@ class Player(commands.Cog):
self.__playlist.loop_off()
await self.__guild.voice_client.disconnect()
def __ensure_source(self, song: Song) -> str:
async def __ensure_source(self, song: Song) -> str:
while True:
if song.source != None: # If song got downloaded
await asyncio.sleep(0.1)
if song.source is not None: # If song got downloaded
return song.source
if song.problematic: # If song got any error

View File

@@ -1,12 +1,11 @@
from collections import deque
from typing import List
from Config.Config import Configs
from Music.Interfaces import IPlaylist
from Music.Song import Song
import random
class Playlist(IPlaylist):
class Playlist:
def __init__(self) -> None:
self.__config = Configs()
@@ -54,7 +53,7 @@ class Playlist(IPlaylist):
return len(self.__queue)
def next_song(self) -> Song:
if self.__current == None and len(self.__queue) == 0:
if self.__current is None and len(self.__queue) == 0:
return None
played_song = self.__current

View File

@@ -1,13 +1,10 @@
from Music.Interfaces import ISong, IPlaylist
class Song:
class Song(ISong):
def __init__(self, identifier: str, playlist: IPlaylist, requester: str) -> None:
def __init__(self, identifier: str, playlist, requester: str) -> None:
self.__identifier = identifier
self.__info = {'requester': requester}
self.__problematic = False
self.__playlist: IPlaylist = playlist
self.__playlist = playlist
def finish_down(self, info: dict) -> None:
if info is None: