-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.py
464 lines (420 loc) · 22.1 KB
/
commands.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# commands.py
import discord
from discord.ext import commands
from discord import app_commands
from youtubesearchpython import VideosSearch
import asyncio
from messages import Messages # Ensure this is your custom Messages class
import traceback
import os
import json
# Assume that YTDLSource, SpotifySource, and other necessary classes are imported or defined elsewhere
# If not, please include them accordingly
def setup_commands(tree, bot, sp, genius, music_instances, Music, YTDLSource, SpotifySource):
async def get_music_instance(guild):
"""Retrieve or create a Music instance for the guild."""
if guild.id not in music_instances:
music_instances[guild.id] = Music(bot)
return music_instances[guild.id]
async def search_youtube_video(query):
"""
Search YouTube for a video matching the query.
Uses the synchronous VideosSearch class within an executor to maintain async behavior.
"""
try:
loop = asyncio.get_event_loop()
search = VideosSearch(query, limit=1)
# VideosSearch is synchronous, so run it in an executor
result = await loop.run_in_executor(None, lambda: search.result())
if result['result']:
video_url = result['result'][0]['link']
return video_url
else:
return None
except Exception as e:
print(f"Error in search_youtube_video: {e}")
return None
@tree.command(name='join', description='Join your voice channel.')
async def join(interaction: discord.Interaction):
guild = interaction.guild
if guild.voice_client is not None:
await interaction.response.send_message("I'm already connected to a voice channel.", ephemeral=True)
return
if interaction.user.voice:
channel = interaction.user.voice.channel
try:
await channel.connect()
embed = Messages.success("Joined your voice channel!")
await interaction.response.send_message(embed=embed)
except Exception as e:
embed = Messages.error(f"Failed to join voice channel: {e}")
await interaction.response.send_message(embed=embed, ephemeral=True)
else:
embed = Messages.error("You are not connected to a voice channel.")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='leave', description='Leave the voice channel.')
async def leave(interaction: discord.Interaction):
guild = interaction.guild
voice_client = guild.voice_client
if voice_client is not None:
await voice_client.disconnect()
embed = Messages.success("Left the voice channel.")
await interaction.response.send_message(embed=embed)
else:
embed = Messages.error("I'm not connected to any voice channel.")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='play', description='Play a song from YouTube or Spotify or search by keywords.')
async def play(interaction: discord.Interaction, query: str):
await interaction.response.defer()
guild = interaction.guild
music = await get_music_instance(guild)
music.channel = interaction.channel
# Connect to voice channel if not already connected
if guild.voice_client is None or not guild.voice_client.is_connected():
if interaction.user.voice:
channel = interaction.user.voice.channel
try:
await channel.connect()
except Exception as e:
embed = Messages.error(f"Failed to connect to the voice channel: {e}")
await interaction.followup.send(embed=embed, ephemeral=True)
return
else:
embed = Messages.error("You are not in a voice channel.")
await interaction.followup.send(embed=embed, ephemeral=True)
return
try:
if 'spotify.com' in query:
# Handle Spotify URLs
player = await SpotifySource.from_spotify_url(query, loop=bot.loop)
elif 'youtube.com' in query or 'youtu.be' in query:
# Handle YouTube URLs
player = await YTDLSource.from_url(query, loop=bot.loop, stream=True)
else:
# Search YouTube for the query
search_url = await search_youtube_video(query)
if search_url is not None:
player = await YTDLSource.from_url(search_url, loop=bot.loop, stream=True)
else:
embed = Messages.error("No results found.")
await interaction.followup.send(embed=embed, ephemeral=True)
return
except Exception as e:
traceback.print_exc()
embed = Messages.error(f"An error occurred: {e}")
await interaction.followup.send(embed=embed, ephemeral=True)
return
voice_client = guild.voice_client
if voice_client.is_playing() or voice_client.is_paused():
# Add to queue if something is already playing
music.queue.append(player)
embed = Messages.added_to_queue(player.title)
await interaction.followup.send(embed=embed)
else:
# Play immediately if nothing is playing
music.current = player
# Create a new MusicControls view
view = MusicControls(interaction, music, voice_client)
message = await interaction.followup.send(embed=Messages.now_playing(player.title), view=view)
view.message = message # Reference to the message containing the view
music.view = view # Store the view in the music instance
def after_playing(error):
if error:
print(f"Player error: {error}")
coro = music.play_next(guild)
fut = asyncio.run_coroutine_threadsafe(coro, bot.loop)
try:
fut.result()
except Exception as e:
print(f"Error in after_playing: {e}")
voice_client.play(player, after=after_playing)
@tree.command(name='pause', description='Pause the playback.')
async def pause(interaction: discord.Interaction):
guild = interaction.guild
voice_client = guild.voice_client
if voice_client and voice_client.is_playing():
voice_client.pause()
embed = Messages.success("Playback paused.")
await interaction.response.send_message(embed=embed)
else:
embed = Messages.error("Nothing is playing.")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='resume', description='Resume the playback.')
async def resume(interaction: discord.Interaction):
guild = interaction.guild
voice_client = guild.voice_client
if voice_client and voice_client.is_paused():
voice_client.resume()
embed = Messages.success("Playback resumed.")
await interaction.response.send_message(embed=embed)
else:
embed = Messages.error("Playback is not paused.")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='skip', description='Skip the current song.')
async def skip(interaction: discord.Interaction):
guild = interaction.guild
voice_client = guild.voice_client
if voice_client and voice_client.is_playing():
voice_client.stop()
embed = Messages.success("Skipped the current song.")
await interaction.response.send_message(embed=embed)
else:
embed = Messages.error("Nothing is playing.")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='stop', description='Stop the playback and clear the queue.')
async def stop(interaction: discord.Interaction):
guild = interaction.guild
music = await get_music_instance(guild)
await music.stop(guild)
embed = Messages.success("Playback stopped and queue cleared.")
await interaction.response.send_message(embed=embed)
@tree.command(name='lyrics', description='Fetch the lyrics for the current song.')
async def lyrics(interaction: discord.Interaction):
guild = interaction.guild
music = await get_music_instance(guild)
if music.current:
try:
song_title = music.current.title
lyrics = genius.search_song(song_title)
if lyrics and lyrics.lyrics:
embed = Messages.lyrics(song_title, lyrics.lyrics)
await interaction.response.send_message(embed=embed)
else:
embed = Messages.error("Lyrics not found for this song.")
await interaction.response.send_message(embed=embed, ephemeral=True)
except Exception as e:
traceback.print_exc()
embed = Messages.error(f"An error occurred while fetching lyrics: {e}")
await interaction.followup.send(embed=embed, ephemeral=True)
else:
embed = Messages.error("No song is currently playing.")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='help', description='Show help information.')
async def help_command(interaction: discord.Interaction):
embed = Messages.help()
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='volume', description='Set the playback volume (0-100).')
async def volume(interaction: discord.Interaction, percent: int):
guild = interaction.guild
music = await get_music_instance(guild)
if 0 <= percent <= 100:
if guild.voice_client and guild.voice_client.source:
guild.voice_client.source.volume = percent / 100
embed = Messages.success(f"Volume set to {percent}%.")
await interaction.response.send_message(embed=embed)
else:
embed = Messages.error("Nothing is playing.")
await interaction.response.send_message(embed=embed, ephemeral=True)
else:
embed = Messages.error("Please provide a volume between 0 and 100.")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='loop', description='Enable or disable looping of the current song.')
async def loop_command(interaction: discord.Interaction, state: str):
guild = interaction.guild
music = await get_music_instance(guild)
if state.lower() in ['on', 'off']:
music.loop = True if state.lower() == 'on' else False
status = "enabled" if music.loop else "disabled"
embed = Messages.success(f"Looping {status}.")
await interaction.response.send_message(embed=embed)
else:
embed = Messages.error("Please specify `on` or `off` for the loop command.")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='add_to_playlist', description='Add a song to a custom playlist.')
async def add_to_playlist(interaction: discord.Interaction, name: str, url: str):
guild = interaction.guild
# Implementation depends on how playlists are managed
# Example:
# Add the song to a JSON or database file under the specified playlist name
try:
# Placeholder implementation
# You need to implement the actual storage logic
# For example, using a JSON file:
playlists_file = 'playlists.json'
if os.path.exists(playlists_file):
with open(playlists_file, 'r') as f:
playlists = json.load(f)
else:
playlists = {}
if name not in playlists:
playlists[name] = []
playlists[name].append(url)
with open(playlists_file, 'w') as f:
json.dump(playlists, f, indent=4)
embed = Messages.success(f"Added to playlist `{name}`.")
await interaction.response.send_message(embed=embed)
except Exception as e:
traceback.print_exc()
embed = Messages.error(f"Failed to add to playlist: {e}")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='play_playlist', description='Play songs from a custom playlist.')
async def play_playlist(interaction: discord.Interaction, name: str):
guild = interaction.guild
music = await get_music_instance(guild)
try:
playlists_file = 'playlists.json'
if os.path.exists(playlists_file):
with open(playlists_file, 'r') as f:
playlists = json.load(f)
else:
playlists = {}
if name not in playlists or not playlists[name]:
embed = Messages.error(f"Playlist `{name}` does not exist or is empty.")
await interaction.response.send_message(embed=embed, ephemeral=True)
return
for url in playlists[name]:
# Reuse the /play command logic
if 'spotify.com' in url:
player = await SpotifySource.from_spotify_url(url, loop=bot.loop)
elif 'youtube.com' in url or 'youtu.be' in url:
player = await YTDLSource.from_url(url, loop=bot.loop, stream=True)
else:
search_url = await search_youtube_video(url)
if search_url is not None:
player = await YTDLSource.from_url(search_url, loop=bot.loop, stream=True)
else:
continue # Skip if not found
if guild.voice_client.is_playing() or guild.voice_client.is_paused():
music.queue.append(player)
else:
music.current = player
# Create a new MusicControls view
view = MusicControls(interaction, music, guild.voice_client)
message = await interaction.followup.send(embed=Messages.now_playing(player.title), view=view)
view.message = message # Reference to the message containing the view
music.view = view # Store the view in the music instance
def after_playing(error):
if error:
print(f"Player error: {error}")
coro = music.play_next(guild)
fut = asyncio.run_coroutine_threadsafe(coro, bot.loop)
try:
fut.result()
except Exception as e:
print(f"Error in after_playing: {e}")
guild.voice_client.play(player, after=after_playing)
embed = Messages.success(f"Playing playlist `{name}`.")
await interaction.followup.send(embed=embed)
except Exception as e:
traceback.print_exc()
embed = Messages.error(f"Failed to play playlist: {e}")
await interaction.followup.send(embed=embed, ephemeral=True)
@tree.command(name='vote_skip', description='Vote to skip the current song.')
async def vote_skip(interaction: discord.Interaction):
guild = interaction.guild
music = await get_music_instance(guild)
voice_client = guild.voice_client
if not voice_client or not voice_client.is_playing():
embed = Messages.error("Nothing is playing.")
await interaction.response.send_message(embed=embed, ephemeral=True)
return
user = interaction.user
if user in music.skip_votes:
embed = Messages.error("You have already voted to skip.")
await interaction.response.send_message(embed=embed, ephemeral=True)
return
music.skip_votes.add(user)
total_votes = len(music.skip_votes)
required_votes = max(1, len(voice_client.channel.members) // 2)
if total_votes >= required_votes:
voice_client.stop()
embed = Messages.success("Skip vote passed. Skipping the current song.")
music.skip_votes.clear()
await interaction.response.send_message(embed=embed)
else:
embed = Messages.info(f"Skip vote added. {total_votes}/{required_votes} votes.")
await interaction.response.send_message(embed=embed)
@tree.command(name='clear', description='Clear a number of messages in a channel (admin only).')
async def clear(interaction: discord.Interaction, number: int):
if not interaction.user.guild_permissions.manage_messages:
embed = Messages.error("You do not have permission to use this command.")
await interaction.response.send_message(embed=embed, ephemeral=True)
return
if number < 1 or number > 100:
embed = Messages.error("Please specify a number between 1 and 100.")
await interaction.response.send_message(embed=embed, ephemeral=True)
return
try:
deleted = await interaction.channel.purge(limit=number + 1) # +1 to include the command message
embed = Messages.success(f"Deleted {len(deleted) - 1} messages.")
await interaction.channel.send(embed=embed, delete_after=5)
except Exception as e:
traceback.print_exc()
embed = Messages.error(f"Failed to delete messages: {e}")
await interaction.response.send_message(embed=embed, ephemeral=True)
@tree.command(name='list_playlists', description='List all custom playlists and their creators.')
async def list_playlists(interaction: discord.Interaction):
try:
playlists_file = 'playlists.json'
if os.path.exists(playlists_file):
with open(playlists_file, 'r') as f:
playlists = json.load(f)
else:
playlists = {}
if not playlists:
embed = Messages.info("No playlists found.")
await interaction.response.send_message(embed=embed, ephemeral=True)
return
embed = discord.Embed(title="Custom Playlists", color=discord.Color.blue())
for name, songs in playlists.items():
embed.add_field(name=name, value=f"{len(songs)} songs", inline=False)
await interaction.response.send_message(embed=embed)
except Exception as e:
traceback.print_exc()
embed = Messages.error(f"Failed to list playlists: {e}")
await interaction.response.send_message(embed=embed, ephemeral=True)
# MusicControls View Class
class MusicControls(discord.ui.View):
def __init__(self, interaction, music, voice_client):
super().__init__(timeout=None)
self.interaction = interaction
self.music = music
self.voice_client = voice_client
self.message = None # Will be set when the message is sent
async def interaction_check(self, interaction: discord.Interaction) -> bool:
"""Ensure only users in the same voice channel can interact with the controls."""
if interaction.user.voice and interaction.user.voice.channel == self.voice_client.channel:
return True
else:
await interaction.response.send_message(
"You must be in the same voice channel to use these controls.",
ephemeral=True
)
return False
@discord.ui.button(label='Pause', style=discord.ButtonStyle.primary, emoji='⏸️')
async def pause_button(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.voice_client.is_playing():
self.voice_client.pause()
await interaction.response.send_message("Playback paused.", ephemeral=True)
else:
await interaction.response.send_message("Nothing is playing.", ephemeral=True)
@discord.ui.button(label='Resume', style=discord.ButtonStyle.primary, emoji='▶️')
async def resume_button(self, interaction: discord.Interaction, button: discord.ui.Button):
if self.voice_client.is_paused():
self.voice_client.resume()
await interaction.response.send_message("Playback resumed.", ephemeral=True)
else:
await interaction.response.send_message("Playback is not paused.", ephemeral=True)
@discord.ui.button(label='Skip', style=discord.ButtonStyle.primary, emoji='⏭️')
async def skip_button(self, interaction: discord.Interaction, button: discord.ui.Button):
music = self.music
voice_client = self.voice_client
if voice_client.is_playing():
if music.queue or music.loop:
voice_client.stop()
await interaction.response.send_message("Skipped the current song.", ephemeral=True)
else:
await interaction.response.send_message("No more songs in the queue to skip to.", ephemeral=True)
else:
await interaction.response.send_message("Nothing is playing.", ephemeral=True)
@discord.ui.button(label='Loop', style=discord.ButtonStyle.primary, emoji='🔁')
async def loop_button(self, interaction: discord.Interaction, button: discord.ui.Button):
self.music.loop = not self.music.loop
status = "enabled" if self.music.loop else "disabled"
await interaction.response.send_message(f"Looping {status}.", ephemeral=True)
async def disable_all_items(self):
"""Disable all buttons in the view."""
for item in self.children:
item.disabled = True
if self.message:
await self.message.edit(view=self)