Changing some methods signature in Playlist to be compatible in Shared Memory, now finishing process after some time

This commit is contained in:
Rafael Vargas
2022-07-22 18:07:44 -03:00
parent fc7de9cb4f
commit 19ae59c5b8
14 changed files with 116 additions and 103 deletions

View File

@@ -10,7 +10,7 @@ class HistoryController(AbstractController):
super().__init__(ctx, bot)
async def run(self) -> ControllerResponse:
history = self.player.playlist.songs_history
history = self.player.playlist.getSongsHistory()
if len(history) == 0:
text = self.messages.HISTORY_EMPTY

View File

@@ -16,7 +16,7 @@ class LoopController(AbstractController):
return ControllerResponse(self.ctx, embed)
args = args.lower()
if self.player.playlist.current is None:
if self.player.playlist.getCurrentSong() is None:
embed = self.embeds.NOT_PLAYING()
error = BadCommandUsage()
return ControllerResponse(self.ctx, embed, error)

View File

@@ -33,7 +33,7 @@ class MoveController(AbstractController):
try:
song = self.player.playlist.move_songs(pos1, pos2)
songs = self.player.playlist.songs_to_preload
songs = self.player.playlist.getSongsToPreload()
await self.__down.preload(songs)
song_name = song.title if song.title else song.identifier

View File

@@ -15,12 +15,12 @@ class NowPlayingController(AbstractController):
embed = self.embeds.NOT_PLAYING()
return ControllerResponse(self.ctx, embed)
if self.player.playlist.looping_one:
if self.player.playlist.isLoopingOne():
title = self.messages.ONE_SONG_LOOPING
else:
title = self.messages.SONG_PLAYING
await self.__cleaner.clean_messages(self.ctx, self.config.CLEANER_MESSAGES_QUANT)
info = self.player.playlist.current.info
info = self.player.playlist.getCurrentSong().info
embed = self.embeds.SONG_INFO(info, title)
return ControllerResponse(self.ctx, embed)

View File

@@ -21,7 +21,7 @@ class PlayController(AbstractController):
track = " ".join(args)
requester = self.ctx.author.name
if not self.__user_connected():
if not self.__isUserConnected():
error = ImpossibleMove()
embed = self.embeds.NO_CHANNEL()
return ControllerResponse(self.ctx, embed, error)
@@ -36,7 +36,7 @@ class PlayController(AbstractController):
self.player.playlist.add_song(song)
quant = len(musics)
songs_preload = self.player.playlist.songs_to_preload
songs_preload = self.player.playlist.getSongsToPreload()
await self.__down.preload(songs_preload)
if quant == 1:
@@ -73,8 +73,6 @@ class PlayController(AbstractController):
queue.put(command)
else:
# Start the process
command = VCommands(VCommandsType.CONTEXT, self.ctx)
queue.put(command)
process.start()
return response
@@ -91,27 +89,8 @@ class PlayController(AbstractController):
return ControllerResponse(self.ctx, embed, error)
def __user_connected(self) -> bool:
def __isUserConnected(self) -> bool:
if self.ctx.author.voice:
return True
else:
return False
def __is_connected(self) -> bool:
try:
voice_channel = self.guild.voice_client.channel
if not self.guild.voice_client.is_connected():
return False
else:
return True
except:
return False
async def __connect(self) -> bool:
# if self.guild.voice_client is None:
try:
await self.ctx.author.voice.channel.connect(reconnect=True, timeout=None)
return True
except:
return False

View File

@@ -27,7 +27,7 @@ class PrevController(AbstractController):
embed = self.embeds.UNKNOWN_ERROR()
return ControllerResponse(self.ctx, embed, error)
if self.player.playlist.looping_all or self.player.playlist.looping_one:
if self.player.playlist.isLoopingAll() or self.player.playlist.isLoopingOne():
error = BadCommandUsage()
embed = self.embeds.FAIL_DUE_TO_LOOP_ON()
return ControllerResponse(self.ctx, embed, error)

View File

@@ -5,6 +5,7 @@ from Controllers.AbstractController import AbstractController
from Controllers.ControllerResponse import ControllerResponse
from Music.Downloader import Downloader
from Utils.Utils import Utils
from Parallelism.ProcessManager import ProcessManager
class QueueController(AbstractController):
@@ -13,32 +14,43 @@ class QueueController(AbstractController):
self.__down = Downloader()
async def run(self) -> ControllerResponse:
if self.player.playlist.looping_one:
song = self.player.playlist.current
embed = self.embeds.ONE_SONG_LOOPING(song.info)
return ControllerResponse(self.ctx, embed)
songs_preload = self.player.playlist.songs_to_preload
if len(songs_preload) == 0:
# Retrieve the process of the guild
process = ProcessManager()
processContext = process.getRunningPlayerContext(self.guild)
if not processContext: # If no process return empty list
embed = self.embeds.EMPTY_QUEUE()
return ControllerResponse(self.ctx, embed)
asyncio.create_task(self.__down.preload(songs_preload))
# Acquire the Lock to manipulate the playlist
with processContext.getLock():
playlist = processContext.getPlaylist()
if self.player.playlist.looping_all:
title = self.messages.ALL_SONGS_LOOPING
else:
title = self.messages.QUEUE_TITLE
if playlist.isLoopingOne():
song = playlist.getCurrentSong()
embed = self.embeds.ONE_SONG_LOOPING(song.info)
return ControllerResponse(self.ctx, embed)
total_time = Utils.format_time(sum([int(song.duration if song.duration else 0)
for song in songs_preload]))
total_songs = len(self.player.playlist)
songs_preload = playlist.getSongsToPreload()
if len(songs_preload) == 0:
embed = self.embeds.EMPTY_QUEUE()
return ControllerResponse(self.ctx, embed)
text = f'📜 Queue length: {total_songs} | ⌛ Duration: `{total_time}` downloaded \n\n'
asyncio.create_task(self.__down.preload(songs_preload))
for pos, song in enumerate(songs_preload, start=1):
song_name = song.title if song.title else self.messages.SONG_DOWNLOADING
text += f"**`{pos}` - ** {song_name} - `{Utils.format_time(song.duration)}`\n"
if playlist.isLoopingAll():
title = self.messages.ALL_SONGS_LOOPING
else:
title = self.messages.QUEUE_TITLE
embed = self.embeds.QUEUE(title, text)
return ControllerResponse(self.ctx, embed)
total_time = Utils.format_time(sum([int(song.duration if song.duration else 0)
for song in songs_preload]))
total_songs = len(playlist.getSongs())
text = f'📜 Queue length: {total_songs} | ⌛ Duration: `{total_time}` downloaded \n\n'
for pos, song in enumerate(songs_preload, start=1):
song_name = song.title if song.title else self.messages.SONG_DOWNLOADING
text += f"**`{pos}` - ** {song_name} - `{Utils.format_time(song.duration)}`\n"
embed = self.embeds.QUEUE(title, text)
return ControllerResponse(self.ctx, embed)

View File

@@ -15,7 +15,7 @@ class ShuffleController(AbstractController):
async def run(self) -> ControllerResponse:
try:
self.player.playlist.shuffle()
songs = self.player.playlist.songs_to_preload
songs = self.player.playlist.getSongsToPreload()
asyncio.create_task(self.__down.preload(songs))
embed = self.embeds.SONGS_SHUFFLED()

View File

@@ -10,7 +10,7 @@ class SkipController(AbstractController):
super().__init__(ctx, bot)
async def run(self) -> ControllerResponse:
if self.player.playlist.looping_one:
if self.player.playlist.isLoopingOne():
embed = self.embeds.ERROR_DUE_LOOP_ONE_ON()
error = BadCommandUsage()
return ControllerResponse(self.ctx, embed, error)