diff options
| author | SamlonDev <stankiewiczgracjan2019@gmail.com> | 2025-06-14 18:54:02 +0200 |
|---|---|---|
| committer | SamlonDev <stankiewiczgracjan2019@gmail.com> | 2025-06-14 18:54:02 +0200 |
| commit | 791e75cb2beeca45a6edf52b65f7e299e03b5eaf (patch) | |
| tree | cfee93211b7f8ac1c985325aaf8d9399b7a357ca /cogs/music/play.py | |
| parent | temp downloads the files instead (diff) | |
| download | senator-sperma-791e75cb2beeca45a6edf52b65f7e299e03b5eaf.tar.gz senator-sperma-791e75cb2beeca45a6edf52b65f7e299e03b5eaf.zip | |
code-style issues
Diffstat (limited to '')
| -rw-r--r-- | cogs/music/play.py | 55 |
1 files changed, 29 insertions, 26 deletions
diff --git a/cogs/music/play.py b/cogs/music/play.py index bf9d081..b47de03 100644 --- a/cogs/music/play.py +++ b/cogs/music/play.py | |||
| @@ -1,12 +1,14 @@ | |||
| 1 | import discord | ||
| 2 | from discord.ext import commands | ||
| 3 | import asyncio | 1 | import asyncio |
| 2 | |||
| 3 | import discord | ||
| 4 | import yt_dlp | 4 | import yt_dlp |
| 5 | from discord.ext import commands | ||
| 5 | from youtubesearchpython import VideosSearch | 6 | from youtubesearchpython import VideosSearch |
| 6 | 7 | ||
| 7 | # Suppress noise about console usage from yt-dlp | 8 | # Suppress noise about console usage from yt-dlp |
| 8 | yt_dlp.utils.bug_reports_message = lambda: '' | 9 | yt_dlp.utils.bug_reports_message = lambda: '' |
| 9 | 10 | ||
| 11 | |||
| 10 | class Play(commands.Cog): | 12 | class Play(commands.Cog): |
| 11 | def __init__(self, bot): | 13 | def __init__(self, bot): |
| 12 | self.bot = bot | 14 | self.bot = bot |
| @@ -32,15 +34,15 @@ class Play(commands.Cog): | |||
| 32 | queue = self.get_queue(guild_id) | 34 | queue = self.get_queue(guild_id) |
| 33 | if queue: | 35 | if queue: |
| 34 | title, url = queue[0] | 36 | title, url = queue[0] |
| 35 | 37 | ||
| 36 | def after_playing(error): | 38 | def after_playing(): |
| 37 | if queue: | 39 | if queue: |
| 38 | queue.pop(0) | 40 | queue.pop(0) |
| 39 | if queue: | 41 | if queue: |
| 40 | next_title, next_url = queue[0] | 42 | next_title, next_url = queue[0] |
| 41 | if not vc.is_playing() and not vc.is_paused(): | 43 | if not vc.is_playing() and not vc.is_paused(): |
| 42 | asyncio.run_coroutine_threadsafe( | 44 | asyncio.run_coroutine_threadsafe( |
| 43 | self.play_audio(ctx, vc, next_url, next_title, guild_id), | 45 | self.play_audio(ctx, vc, next_url, next_title), |
| 44 | self.bot.loop | 46 | self.bot.loop |
| 45 | ) | 47 | ) |
| 46 | else: | 48 | else: |
| @@ -48,11 +50,11 @@ class Play(commands.Cog): | |||
| 48 | vc.disconnect(), | 50 | vc.disconnect(), |
| 49 | self.bot.loop | 51 | self.bot.loop |
| 50 | ) | 52 | ) |
| 51 | 53 | ||
| 52 | if not vc.is_playing() and not vc.is_paused(): | 54 | if not vc.is_playing() and not vc.is_paused(): |
| 53 | await self.play_audio(ctx, vc, url, title, guild_id, after=after_playing) | 55 | await self.play_audio(ctx, vc, url, title, after=after_playing) |
| 54 | 56 | ||
| 55 | async def play_audio(self, ctx, vc, url, title, guild_id, after=None): | 57 | async def play_audio(self, ctx, vc, url, title, after=None): |
| 56 | try: | 58 | try: |
| 57 | with yt_dlp.YoutubeDL(self.ydl_opts) as ydl: | 59 | with yt_dlp.YoutubeDL(self.ydl_opts) as ydl: |
| 58 | info = await self.bot.loop.run_in_executor( | 60 | info = await self.bot.loop.run_in_executor( |
| @@ -61,59 +63,59 @@ class Play(commands.Cog): | |||
| 61 | ) | 63 | ) |
| 62 | if 'entries' in info: | 64 | if 'entries' in info: |
| 63 | info = info['entries'][0] | 65 | info = info['entries'][0] |
| 64 | 66 | ||
| 65 | url2 = info['url'] | 67 | url2 = info['url'] |
| 66 | 68 | ||
| 67 | FFMPEG_OPTIONS = { | 69 | ffmpeg_options = { |
| 68 | 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', | 70 | 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', |
| 69 | 'options': '-vn' | 71 | 'options': '-vn' |
| 70 | } | 72 | } |
| 71 | 73 | ||
| 72 | source = discord.FFmpegPCMAudio(url2, **FFMPEG_OPTIONS) | 74 | source = discord.FFmpegPCMAudio(url2, **ffmpeg_options) |
| 73 | vc.play(source, after=after) | 75 | vc.play(source, after=after) |
| 74 | await ctx.followup.send(f"gra: {title}") | 76 | await ctx.followup.send(f"gra: {title}") |
| 75 | except Exception as e: | 77 | except Exception as e: |
| 76 | await ctx.followup.send(f"Wystąpił błąd podczas odtwarzania: {str(e)}") | 78 | await ctx.followup.send(f"Wystąpił błąd podczas odtwarzania: {str(e)}") |
| 77 | # Try to play next in queue if there's an error | 79 | # Try to play next in the queue if there's an error |
| 78 | if after: | 80 | if after: |
| 79 | after(None) | 81 | after(None) |
| 80 | 82 | ||
| 81 | @commands.slash_command(name="grajcuj", description="grajcuj melodyje") | 83 | @commands.slash_command(name="grajcuj", description="grajcuj melodyje") |
| 82 | async def play(self, ctx, *, search: str): | 84 | async def play(self, ctx, *, search: str): |
| 83 | await ctx.defer() | 85 | await ctx.defer() |
| 84 | 86 | ||
| 85 | if not (ctx.author.voice and ctx.author.voice.channel): | 87 | if not (ctx.author.voice and ctx.author.voice.channel): |
| 86 | await ctx.followup.send("nie jesteś w kanale głosowym") | 88 | await ctx.followup.send("nie jesteś w kanale głosowym") |
| 87 | return | 89 | return |
| 88 | 90 | ||
| 89 | channel = ctx.author.voice.channel | 91 | channel = ctx.author.voice.channel |
| 90 | vc = ctx.voice_client or await channel.connect() | 92 | vc = ctx.voice_client or await channel.connect() |
| 91 | 93 | ||
| 92 | if vc.channel != channel: | 94 | if vc.channel != channel: |
| 93 | await vc.move_to(channel) | 95 | await vc.move_to(channel) |
| 94 | 96 | ||
| 95 | # Search for the video | 97 | # Search for the video |
| 96 | videos_search = VideosSearch(search, limit=1) | 98 | videos_search = VideosSearch(search, limit=1) |
| 97 | result = videos_search.result() | 99 | result = videos_search.result() |
| 98 | 100 | ||
| 99 | if not result['result']: | 101 | if not result['result']: |
| 100 | await ctx.followup.send("nie znaleziono wideo dla twojego wyszukiwania.", ephemeral=True) | 102 | await ctx.followup.send("nie znaleziono wideo dla twojego wyszukiwania.", ephemeral=True) |
| 101 | return | 103 | return |
| 102 | 104 | ||
| 103 | video = result['result'][0] | 105 | video = result['result'][0] |
| 104 | title = video['title'] | 106 | title = video['title'] |
| 105 | url = video['link'] | 107 | url = video['link'] |
| 106 | 108 | ||
| 107 | # Add to queue | 109 | # Add to queue |
| 108 | queue = self.get_queue(ctx.guild.id) | 110 | queue = self.get_queue(ctx.guild.id) |
| 109 | queue.append((title, url)) | 111 | queue.append((title, url)) |
| 110 | 112 | ||
| 111 | # If nothing is playing, start playing | 113 | # If nothing is playing, start playing |
| 112 | if not vc.is_playing() and not vc.is_paused(): | 114 | if not vc.is_playing() and not vc.is_paused(): |
| 113 | await self.play_next(ctx, vc, ctx.guild.id) | 115 | await self.play_next(ctx, vc, ctx.guild.id) |
| 114 | else: | 116 | else: |
| 115 | await ctx.followup.send(f"dodano do kolejki: {title}") | 117 | await ctx.followup.send(f"dodano do kolejki: {title}") |
| 116 | 118 | ||
| 117 | @commands.slash_command(name="stop", description="zatrzymaj odtwarzanie i wyczyść kolejkę") | 119 | @commands.slash_command(name="stop", description="zatrzymaj odtwarzanie i wyczyść kolejkę") |
| 118 | async def stop(self, ctx): | 120 | async def stop(self, ctx): |
| 119 | if ctx.voice_client: | 121 | if ctx.voice_client: |
| @@ -123,7 +125,7 @@ class Play(commands.Cog): | |||
| 123 | await ctx.respond("Zatrzymano odtwarzanie i wyczyszczono kolejkę") | 125 | await ctx.respond("Zatrzymano odtwarzanie i wyczyszczono kolejkę") |
| 124 | else: | 126 | else: |
| 125 | await ctx.respond("Nie jestem połączony z kanałem głosowym") | 127 | await ctx.respond("Nie jestem połączony z kanałem głosowym") |
| 126 | 128 | ||
| 127 | @commands.slash_command(name="pauza", description="wstrzymaj odtwarzanie") | 129 | @commands.slash_command(name="pauza", description="wstrzymaj odtwarzanie") |
| 128 | async def pause(self, ctx): | 130 | async def pause(self, ctx): |
| 129 | if ctx.voice_client and ctx.voice_client.is_playing(): | 131 | if ctx.voice_client and ctx.voice_client.is_playing(): |
| @@ -131,7 +133,7 @@ class Play(commands.Cog): | |||
| 131 | await ctx.respond("Wstrzymano odtwarzanie") | 133 | await ctx.respond("Wstrzymano odtwarzanie") |
| 132 | else: | 134 | else: |
| 133 | await ctx.respond("Nic nie jest teraz odtwarzane") | 135 | await ctx.respond("Nic nie jest teraz odtwarzane") |
| 134 | 136 | ||
| 135 | @commands.slash_command(name="wznow", description="wznów odtwarzanie") | 137 | @commands.slash_command(name="wznow", description="wznów odtwarzanie") |
| 136 | async def resume(self, ctx): | 138 | async def resume(self, ctx): |
| 137 | if ctx.voice_client and ctx.voice_client.is_paused(): | 139 | if ctx.voice_client and ctx.voice_client.is_paused(): |
| @@ -140,5 +142,6 @@ class Play(commands.Cog): | |||
| 140 | else: | 142 | else: |
| 141 | await ctx.respond("Nic nie jest teraz wstrzymane") | 143 | await ctx.respond("Nic nie jest teraz wstrzymane") |
| 142 | 144 | ||
| 145 | |||
| 143 | def setup(bot): | 146 | def setup(bot): |
| 144 | bot.add_cog(Play(bot)) \ No newline at end of file | 147 | bot.add_cog(Play(bot)) |
