From e36155dd3f39e9bec6c2c48fdba4be4afb364b59 Mon Sep 17 00:00:00 2001 From: Sweetbread Date: Sat, 13 May 2023 00:22:55 +0300 Subject: [PATCH] [style]: Internal use functions to __name --- cogs/music.py | 84 ++++++++++++++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 37 deletions(-) diff --git a/cogs/music.py b/cogs/music.py index d5e969c..3eb6d00 100644 --- a/cogs/music.py +++ b/cogs/music.py @@ -36,37 +36,7 @@ class Music(commands.Cog, name="Музыка"): self.bot = bot self.queue: Dict[int, Channel] = {} - def play_(self, inter, info): - try: - URL = info['url'] - except: - URL = url - logger.debug(URL) - - audio_source = discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS) - inter.guild.voice_client.play(audio_source, after=lambda error: self.next_(inter, error)) - - asyncio.run_coroutine_threadsafe(self.send_embed_(inter, info), self.bot.loop) - - async def send_embed_(self, inter, info): - embed = discord.Embed ( - title=info["title"], - url=info['url'], - description=info["description"] - ) - embed.set_author ( - name=info["uploader"], - url=info["uploader_url"] - ) - embed.set_thumbnail( url=info["thumbnail"] ) - try: - await inter.response.send_message(embed=embed) - except: - await inter.channel.send(embed=embed) - - async def end_of_queue_(self, inter): - await inter.channel.send("В очереди больше не осталось песен") - + @app_commands.command(description="Plays music from popular platforms") @app_commands.describe(url="URL from Youtube/RuTube and other platforms") async def play(self, inter, url: str): @@ -96,8 +66,9 @@ class Music(commands.Cog, name="Музыка"): else: inter.guild.voice_client.stop() - self.play_(inter, info) - + self.__play(inter, info) + + @app_commands.command() async def stop(self, inter): queue = self.queue[inter.user.voice.channel.id] @@ -105,25 +76,30 @@ class Music(commands.Cog, name="Музыка"): inter.guild.voice_client.stop() await inter.response.send_message("Остановлено") + @app_commands.command() async def pause(self, inter): inter.guild.voice_client.pause() await inter.response.send_message("Поставлено на паузу") + @app_commands.command() async def resume(self, inter): inter.guild.voice_client.resume() await inter.response.send_message("Снято с паузы") + @app_commands.command() async def disconnect(self, inter): await inter.guild.voice_client.disconnect() await inter.response.send_message("Отключено") + @app_commands.command() async def next(self, inter): inter.guild.voice_client.stop() + @app_commands.command(name='queue') async def _queue(self, inter): queue = self.queue[inter.user.voice.channel.id] @@ -138,9 +114,43 @@ class Music(commands.Cog, name="Музыка"): text += '\n' await inter.response.send_message(f"```\n{text}\n```") - + - def next_(self, inter, error=None): + def __play(self, inter, info): + try: + URL = info['url'] + except: + URL = url + logger.debug(URL) + + audio_source = discord.FFmpegPCMAudio(URL, **FFMPEG_OPTIONS) + inter.guild.voice_client.play(audio_source, after=lambda error: self.__next(inter, error)) + + asyncio.run_coroutine_threadsafe(self.__send_embed(inter, info), self.bot.loop) + + + async def __send_embed(self, inter, info): + embed = discord.Embed ( + title=info["title"], + url=info['url'], + description=info["description"] + ) + embed.set_author ( + name=info["uploader"], + url=info["uploader_url"] + ) + embed.set_thumbnail( url=info["thumbnail"] ) + try: + await inter.response.send_message(embed=embed) + except: + await inter.channel.send(embed=embed) + + + async def __end_of_queue(self, inter): + await inter.channel.send("В очереди больше не осталось песен") + + + def __next(self, inter, error=None): if error: logger.error(error) return @@ -151,12 +161,12 @@ class Music(commands.Cog, name="Музыка"): queue.cur_pos += 1 logger.debug((len(queue.queue), queue.cur_pos)) if len(queue.queue) == queue.cur_pos: - asyncio.run_coroutine_threadsafe(self.end_of_queue_(inter), self.bot.loop) + asyncio.run_coroutine_threadsafe(self.__end_of_queue(inter), self.bot.loop) return #logger.debug([query["music_pos"]]) info = queue.queue[queue.cur_pos].info - self.play_(inter, info) + self.__play(inter, info) async def setup(bot):