from discord import TextChannel, VoiceChannel from entity import Queue from framework import DiscordPlayer class ActiveQueue: def __init__( self, queue: Queue, textChannel: TextChannel, voiceChannel: VoiceChannel ) -> None: self.queue = queue self.textChannel = textChannel self.voiceChannel = voiceChannel class PlaybackManager: def __init__(self, player: DiscordPlayer) -> None: self.player = player self.queues: dict[int, ActiveQueue] = {} async def nextTrack(self, error: Exception, guildId: int): queue = self.queues[guildId].queue textChannel = self.queues[guildId].textChannel queue.incrementCursor() if queue.cursor < len(queue): self.startPlayback(guildId) await textChannel.send(f"Playing {queue[queue.cursor].title.name}") else: self.stopPlayback(guildId) def startPlayback(self, guildId: int, seekTime=0): queue = self.queues[guildId].queue if len(queue) > queue.cursor and not queue.playing: queue.startPlaying() self.player.play( guildId, queue[queue.cursor].source, seekTime, lambda error: self.nextTrack(error, guildId), ) def stopPlayback(self, guildId: int): queue = self.queues[guildId].queue if queue.playing: queue.stopPlaying() self.player.stop(guildId) async def registerQueue( self, guildId: int, queue: Queue, textChannel: TextChannel, voiceChannel: VoiceChannel, ): if guildId not in self.queues: self.queues[guildId] = ActiveQueue(queue, textChannel, voiceChannel) await self.player.connect(guildId, voiceChannel) self.startPlayback(guildId)