from logging import Logger from discord import ApplicationContext, Bot, Cog, Embed, Interaction, slash_command from service import PlaybackManager, QueueManager from usecase import Sources class Music(Cog): def __init__( self, bot: Bot, logger: Logger, queueManager: QueueManager, playbackManager: PlaybackManager, sources: Sources, ): self.bot = bot self.logger = logger self.queueManager = queueManager self.playbackManager = playbackManager self.sources = sources @slash_command(name="play") async def play(self, context: ApplicationContext, query: str): if context.author.voice is None: await context.respond("Not connected to a voice channel") return async with self.queueManager(context.guild_id) as queue: interaction = await context.respond(f"Searching {query}...") if not isinstance(interaction, Interaction): return entries = await self.sources.processQuery(interaction, query) if entries is None: await interaction.edit_original_response(content=f"{query} not found") return queue.add(entries) await self.playbackManager.registerQueue( context.guild_id, queue, context.channel, context.author.voice.channel, ) if entries[0].playlist is not None: await interaction.edit_original_response( content=f"{len(entries)} songs added to queue from playlist {entries[0].playlist.name}" ) else: await interaction.edit_original_response( content=f"{entries[0].title.name} was added to queue" ) @slash_command(name="nowplaying") async def nowPlaying(self, context: ApplicationContext): assert self.bot.user async with self.queueManager(context.guild_id) as queue: entry = queue.nowPlaying() if entry is None: await context.respond("No song playing") return requester = await self.bot.get_or_fetch_user(entry.requester) assert requester is not None embed = ( Embed(title=entry.title.name, url=entry.title.url) .add_field( name="Artist", value=f"[{entry.artist.name}]({entry.artist.url})" ) .set_author( name="Now Playing", icon_url=self.bot.user.display_avatar.url ) .set_image(url=entry.thumbnail) .set_footer( text=requester.display_name, icon_url=requester.display_avatar.url ) ) if entry.playlist is not None: embed.add_field( name="Playlist", value=f"[{entry.playlist.name}]({entry.playlist.url})", ) await context.respond(embed=embed)