aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/music
diff options
context:
space:
mode:
authorSamlonDev <stankiewiczgracjan2019@gmail.com>2025-05-20 05:45:23 +0200
committerSamlonDev <stankiewiczgracjan2019@gmail.com>2025-05-20 05:45:23 +0200
commitfcc3635b9f24051ae07ed597632597e20372c889 (patch)
tree20a29a1a784f218b3623ff03ceb8084839ff606a /cogs/music
downloadsenator-sperma-fcc3635b9f24051ae07ed597632597e20372c889.tar.gz
senator-sperma-fcc3635b9f24051ae07ed597632597e20372c889.zip
initial push
Diffstat (limited to 'cogs/music')
-rw-r--r--cogs/music/join.py13
-rw-r--r--cogs/music/leave.py13
-rw-r--r--cogs/music/loop.py22
-rw-r--r--cogs/music/play.py53
-rw-r--r--cogs/music/play_compressed.py88
-rw-r--r--cogs/music/queue.py22
-rw-r--r--cogs/music/skip.py47
7 files changed, 258 insertions, 0 deletions
diff --git a/cogs/music/join.py b/cogs/music/join.py
new file mode 100644
index 0000000..91a0b18
--- /dev/null
+++ b/cogs/music/join.py
@@ -0,0 +1,13 @@
1import discord
2from discord.ext import commands
3
4class Join(commands.Cog):
5 def __init__(self, bot):
6 self.bot = bot
7
8 @commands.slash_command(name="join", description="dołącza do kanalu")
9 async def join(self, ctx):
10 await ctx.voice_client.connect()
11
12def setup(bot):
13 bot.add_cog(Join(bot)) \ No newline at end of file
diff --git a/cogs/music/leave.py b/cogs/music/leave.py
new file mode 100644
index 0000000..0be2068
--- /dev/null
+++ b/cogs/music/leave.py
@@ -0,0 +1,13 @@
1import discord
2from discord.ext import commands
3
4class Leave(commands.Cog):
5 def __init__(self, bot):
6 self.bot = bot
7
8 @commands.slash_command(name="kick", description="kickuje bota z kanalu")
9 async def kick(self, ctx):
10 await ctx.voice_client.disconnect()
11
12def setup(bot):
13 bot.add_cog(Leave(bot)) \ No newline at end of file
diff --git a/cogs/music/loop.py b/cogs/music/loop.py
new file mode 100644
index 0000000..5f51c7e
--- /dev/null
+++ b/cogs/music/loop.py
@@ -0,0 +1,22 @@
1from discord.ext import commands
2
3class Loop(commands.Cog):
4 def __init__(self, bot):
5 self.bot = bot
6 self.looping = {}
7
8 def is_looping(self, guild_id):
9 return self.looping.get(guild_id, False)
10
11 def set_looping(self, guild_id, value):
12 self.looping[guild_id] = value
13
14 @commands.slash_command(name="loop", description="włącz/wyłącz loopowanie dla bieżącej melodyji")
15 async def loop(self, ctx):
16 guild_id = ctx.guild.id
17 looping = not self.is_looping(guild_id)
18 self.set_looping(guild_id, looping)
19 await ctx.respond(f"looping jest teraz {'włączony' if looping else 'wyłączony'}.")
20
21def setup(bot):
22 bot.add_cog(Loop(bot)) \ No newline at end of file
diff --git a/cogs/music/play.py b/cogs/music/play.py
new file mode 100644
index 0000000..1eafbca
--- /dev/null
+++ b/cogs/music/play.py
@@ -0,0 +1,53 @@
1import discord
2from discord.ext import commands
3import asyncio
4from youtubesearchpython import VideosSearch
5
6class Play(commands.Cog):
7 def __init__(self, bot):
8 self.bot = bot
9
10 @commands.slash_command(name="grajcuj", description="grajcuj melodyje")
11 async def play(self, ctx, *, search: str):
12 await ctx.defer()
13 if not (ctx.author.voice and ctx.author.voice.channel):
14 await ctx.followup.send("nie jesteś w kanale głosowym")
15 return
16 channel = ctx.author.voice.channel
17 if ctx.voice_client is not None:
18 vc = ctx.voice_client
19 await vc.move_to(channel)
20 else:
21 vc = await channel.connect()
22 videos_search = VideosSearch(search, limit=1)
23 result = videos_search.result()
24 if not result['result']:
25 await ctx.followup.send("nie znaleziono wideo dla twojego wyszukiwania.", ephemeral=True)
26 return
27 video = result['result'][0]
28 video_id = video['id']
29 title = video['title']
30 url = video['link']
31 proc = await asyncio.create_subprocess_exec(
32 "yt-dlp", "-f", "bestaudio", "-g", "--no-cache-dir", url,
33 stdout=asyncio.subprocess.PIPE
34 )
35 stdout, _ = await proc.communicate()
36 stream_url = stdout.decode().strip()
37 FFMPEG_OPTIONS = {
38 'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
39 'options': '-vn'
40 }
41 audio = discord.FFmpegPCMAudio(
42 stream_url,
43 before_options=FFMPEG_OPTIONS['before_options'],
44 options=FFMPEG_OPTIONS['options']
45 )
46 if not vc.is_playing():
47 vc.play(audio)
48 await ctx.followup.send(f"gra: {title}")
49 else:
50 await ctx.followup.send(f"dodano do koleyjki: {title}")
51
52def setup(bot):
53 bot.add_cog(Play(bot)) \ No newline at end of file
diff --git a/cogs/music/play_compressed.py b/cogs/music/play_compressed.py
new file mode 100644
index 0000000..3297110
--- /dev/null
+++ b/cogs/music/play_compressed.py
@@ -0,0 +1,88 @@
1import discord
2from discord.ext import commands
3import asyncio
4import os
5import subprocess
6from youtubesearchpython import VideosSearch
7
8class GrajcujZkompresowane(commands.Cog):
9 def __init__(self, bot):
10 self.bot = bot
11
12 def prepend_title_to_file(self, file_path, title):
13 try:
14 with open(file_path, 'rb') as original:
15 data = original.read()
16 with open(file_path, 'wb') as modified:
17 modified.write(f"{title}\n".encode('utf-8'))
18 modified.write(data)
19 except Exception as e:
20 print(f"Failed to prepend title: {e}")
21
22 def compress_audio(self, input_path):
23 base, ext = os.path.splitext(input_path)
24 output_path = f"{base}_compressed{ext}"
25 ffmpeg_command = f"ffmpeg -i {input_path} -ar 8000 -ac 1 -b:a 500 -acodec libmp3lame -q:a 9 {output_path}"
26 subprocess.run(ffmpeg_command, check=True, shell=True)
27 return output_path
28
29 @commands.slash_command(name="grajcuj_zkompresowane", description="grajcuj zkompresowane melodyje")
30 async def grajcuj_zkompresowane(self, ctx, *, search: str):
31 await ctx.defer()
32 if not (ctx.author.voice and ctx.author.voice.channel):
33 await ctx.followup.send("nie jesteś w kanale głosowym")
34 return
35 channel = ctx.author.voice.channel
36 if ctx.voice_client is not None:
37 vc = ctx.voice_client
38 await vc.move_to(channel)
39 else:
40 vc = await channel.connect()
41 bot_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
42 music_dir = os.path.join(bot_dir, "temp", "music")
43 if not os.path.exists(music_dir):
44 os.makedirs(music_dir)
45 videos_search = VideosSearch(search, limit=1)
46 result = videos_search.result()
47 if not result['result']:
48 await ctx.followup.send("Could not find a video for your search.", ephemeral=True)
49 return
50 video = result['result'][0]
51 video_id = video['id']
52 title = video['title']
53 url = video['link']
54 filename = f"{video_id}.mp3"
55 file_path = os.path.join(music_dir, filename)
56 compressed_file_path = os.path.join(music_dir, f"{video_id}_compressed.mp3")
57 if os.path.exists(compressed_file_path):
58 found_file = compressed_file_path
59 found_title = title
60 else:
61 if not os.path.exists(file_path):
62 cmd = [
63 "yt-dlp",
64 "-x",
65 "--audio-format", "mp3",
66 "-o", file_path,
67 url
68 ]
69 process = await asyncio.create_subprocess_exec(*cmd)
70 await process.communicate()
71 self.prepend_title_to_file(file_path, title)
72 if not os.path.exists(compressed_file_path):
73 try:
74 compressed_file_path = self.compress_audio(file_path)
75 except subprocess.CalledProcessError as e:
76 await ctx.followup.send(f"skompresowanie nie powiodło się: {str(e)}", ephemeral=True)
77 return
78 found_file = compressed_file_path
79 found_title = title
80 title = found_title
81 if not vc.is_playing():
82 vc.play(discord.FFmpegPCMAudio(found_file))
83 await ctx.followup.send(f"gra (zkompresowane): {title}")
84 else:
85 await ctx.followup.send(f"dodano do koleyjki (zkompresowane): {title}")
86
87def setup(bot):
88 bot.add_cog(GrajcujZkompresowane(bot)) \ No newline at end of file
diff --git a/cogs/music/queue.py b/cogs/music/queue.py
new file mode 100644
index 0000000..29f0c36
--- /dev/null
+++ b/cogs/music/queue.py
@@ -0,0 +1,22 @@
1from discord.ext import commands
2
3class Queue(commands.Cog):
4 def __init__(self, bot):
5 self.bot = bot
6 self.queues = {}
7
8 def get_queue(self, guild_id):
9 return self.queues.setdefault(guild_id, [])
10
11 @commands.slash_command(name="koleyjka", description="pokazuje koleyjke")
12 async def queue(self, ctx):
13 guild_id = ctx.guild.id
14 queue = self.get_queue(guild_id)
15 if queue:
16 queue_str = "\n".join(f"{i+1}. {title}" for i, (_, title, _) in enumerate(queue))
17 await ctx.respond(f"koleyjka:\n{queue_str}")
18 else:
19 await ctx.respond("koleyjka jest pustawa")
20
21def setup(bot):
22 bot.add_cog(Queue(bot)) \ No newline at end of file
diff --git a/cogs/music/skip.py b/cogs/music/skip.py
new file mode 100644
index 0000000..f48218b
--- /dev/null
+++ b/cogs/music/skip.py
@@ -0,0 +1,47 @@
1from discord.ext import commands
2import discord
3
4class Skip(commands.Cog):
5 def __init__(self, bot):
6 self.bot = bot
7 self.queues = {}
8
9 def get_queue(self, guild_id):
10 return self.queues.setdefault(guild_id, [])
11
12 def play_next(self, ctx, vc, guild_id):
13 queue = self.get_queue(guild_id)
14 if queue:
15 _, _, file_path = queue[0]
16 def after_playing(error=None):
17 if queue:
18 queue.pop(0)
19 if queue:
20 _, _, next_file = queue[0]
21 if not vc.is_playing():
22 vc.play(discord.FFmpegPCMAudio(next_file), after=after_playing)
23 if not vc.is_playing():
24 vc.play(discord.FFmpegPCMAudio(file_path), after=after_playing)
25
26 @commands.slash_command(name="pomiń", description="przeskocz do nastepnej melodyji")
27 async def pomin(self, ctx):
28 guild_id = ctx.guild.id
29 vc = ctx.voice_client
30 if vc and vc.is_playing():
31 queue = self.get_queue(guild_id)
32 if queue:
33 queue.pop(0)
34 if queue:
35 vc.stop()
36 self.play_next(ctx, vc, guild_id)
37 await ctx.respond("przeskoczono do nastepnej melodyji")
38 else:
39 await ctx.respond("koleyjka jest pustawa")
40 await vc.disconnect()
41 else:
42 await ctx.respond("nic nie gra")
43 else:
44 await ctx.respond("nic nie gra")
45
46def setup(bot):
47 bot.add_cog(Skip(bot)) \ No newline at end of file