28
28
play_lock = asyncio .Lock ()
29
29
queue_buttons_instances = {}
30
30
31
- # Define lock to prevent multiple instances of play_next_song() from running at the same time
32
-
33
31
34
32
@bot .event
35
33
async def on_ready ():
36
34
print (f'We have logged in as { bot .user } ' )
37
35
try :
38
36
synced = await bot .tree .sync ()
39
37
print (f"Synced { len (synced )} command(s)" )
38
+ await bot .change_presence (activity = discord .Activity (type = discord .ActivityType .listening ,
39
+ name = "/Play" ,))
40
40
except Exception as e :
41
41
print (f"Error syncing commands: { e } " )
42
42
@@ -84,24 +84,29 @@ def __init__(self):
84
84
@bot .event
85
85
async def on_interaction (interaction : discord .Interaction ):
86
86
if interaction .type == discord .InteractionType .component :
87
- if interaction .data ["custom_id" ] == "music_pause_button" :
88
- await pause (interaction )
89
- elif interaction .data ["custom_id" ] == "music_play_button" :
90
- await resume (interaction )
91
- elif interaction .data ["custom_id" ] == "music_skip_button" :
92
- await skip (interaction )
93
- elif interaction .data ["custom_id" ] == "music_shuffle_button" :
94
- await shuffle (interaction )
95
- elif interaction .data ["custom_id" ] == "music_kill_button" :
96
- await kill (interaction )
97
- elif interaction .data ["custom_id" ] == "back_button" :
98
- view = playlist_buttons_instances .get (interaction .guild .id )
99
- if view is not None :
100
- await view .back_button (interaction )
101
- elif interaction .data ["custom_id" ] == "next_button" :
102
- view = playlist_buttons_instances .get (interaction .guild .id )
103
- if view is not None :
104
- await view .next_button (interaction )
87
+ custom_id = interaction .data ["custom_id" ]
88
+ match custom_id :
89
+ case "music_pause_button" :
90
+ await pause (interaction )
91
+ case "music_play_button" :
92
+ await resume (interaction )
93
+ case "music_skip_button" :
94
+ await skip (interaction )
95
+ case "music_shuffle_button" :
96
+ await interaction .response .defer ()
97
+ await shuffle (interaction )
98
+ case "music_kill_button" :
99
+ await kill (interaction )
100
+ case "back_button" :
101
+ view = queue_buttons_instances .get (interaction .guild .id )
102
+ if view is not None :
103
+ await view .back_button (interaction )
104
+ case "next_button" :
105
+ view = queue_buttons_instances .get (interaction .guild .id )
106
+ if view is not None :
107
+ await view .next_button (interaction )
108
+ case _:
109
+ pass # Do nothing for all other cases
105
110
106
111
107
112
@@ -136,13 +141,14 @@ async def next_button(self, interaction: discord.Interaction):
136
141
if self .page < self .num_pages :
137
142
self .page += 1
138
143
await self .refresh () # Refresh the view
139
- await interaction .response .edit_message (embed = await self .send_page (self .page_list , self .page ), view = self )
144
+ await interaction .response .edit_message (embed = await self .generate_embed (self .items , self .page ), view = self )
140
145
141
146
async def back_button (self , interaction : discord .Interaction ):
142
147
if self .page > 1 :
143
148
self .page -= 1
144
149
await self .refresh () # Refresh the view
145
- await interaction .response .edit_message (embed = await self .send_page (self .page_list , self .page ), view = self )
150
+ await interaction .response .edit_message (embed = await self .generate_embed (self .items , self .page ), view = self )
151
+
146
152
147
153
148
154
@@ -172,7 +178,7 @@ async def on_voice_state_update(member, before, after):
172
178
173
179
174
180
175
- async def disconnect_after (interaction , music_queue , duration = 120 ):
181
+ async def disconnect_after (interaction , music_queue , duration = 600 ):
176
182
print (f"disconnect_after was called" )
177
183
await asyncio .sleep (duration )
178
184
voice_client = interaction .guild .voice_client
@@ -296,7 +302,10 @@ async def play_song(interaction, url, song_info, song_duration, track, send_mess
296
302
url , song_title , song_duration , track = song_info
297
303
print (f"Playing: { song_title } " )
298
304
formatted_duration = str (datetime .timedelta (seconds = int (song_duration / 1000 )))
305
+ # Create listening activity with song title
306
+ activity = discord .Activity (type = discord .ActivityType .listening , name = song_title )
299
307
308
+ await bot .change_presence (activity = activity )
300
309
301
310
music_queue .current_song_duration = song_duration
302
311
music_queue .current_song_title = song_info
@@ -310,6 +319,10 @@ def wrapped_play_next(error):
310
319
coro = play_song (interaction , None , None , None , None , send_message = True , music_queue = music_queue , play_called = False , play_next = True )
311
320
task = asyncio .run_coroutine_threadsafe (coro , bot .loop )
312
321
task .add_done_callback (lambda _ : asyncio .run_coroutine_threadsafe (disconnect_after (interaction , music_queue ), bot .loop ))
322
+ if not music_queue .queue : # If there's no next song
323
+ # Reset the bot's presence
324
+ asyncio .run_coroutine_threadsafe (bot .change_presence (activity = discord .Activity (type = discord .ActivityType .listening ,
325
+ name = "/Play" )), bot .loop )
313
326
314
327
voice_client .play (discord .FFmpegPCMAudio (url , ** FFMPEG_OPTIONS ), after = wrapped_play_next )
315
328
0 commit comments