Skip to content

Commit d13016d

Browse files
authored
added feature requests and fixed next button bug
Added feature requests: Bot will now wait 10 minutes before disconnecting Now Playing activity presence has been added Bug fixes: fixed the issue causing the queue's next and back buttons to not work.
1 parent ab633aa commit d13016d

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

plex_music_bot.py

+36-23
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,15 @@
2828
play_lock = asyncio.Lock()
2929
queue_buttons_instances = {}
3030

31-
# Define lock to prevent multiple instances of play_next_song() from running at the same time
32-
3331

3432
@bot.event
3533
async def on_ready():
3634
print(f'We have logged in as {bot.user}')
3735
try:
3836
synced = await bot.tree.sync()
3937
print(f"Synced {len(synced)} command(s)")
38+
await bot.change_presence(activity=discord.Activity(type=discord.ActivityType.listening,
39+
name="/Play",))
4040
except Exception as e:
4141
print(f"Error syncing commands: {e}")
4242

@@ -84,24 +84,29 @@ def __init__(self):
8484
@bot.event
8585
async def on_interaction(interaction: discord.Interaction):
8686
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
105110

106111

107112

@@ -136,13 +141,14 @@ async def next_button(self, interaction: discord.Interaction):
136141
if self.page < self.num_pages:
137142
self.page += 1
138143
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)
140145

141146
async def back_button(self, interaction: discord.Interaction):
142147
if self.page > 1:
143148
self.page -= 1
144149
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+
146152

147153

148154

@@ -172,7 +178,7 @@ async def on_voice_state_update(member, before, after):
172178

173179

174180

175-
async def disconnect_after(interaction, music_queue, duration=120):
181+
async def disconnect_after(interaction, music_queue, duration=600):
176182
print(f"disconnect_after was called")
177183
await asyncio.sleep(duration)
178184
voice_client = interaction.guild.voice_client
@@ -296,7 +302,10 @@ async def play_song(interaction, url, song_info, song_duration, track, send_mess
296302
url, song_title, song_duration, track = song_info
297303
print(f"Playing: {song_title}")
298304
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)
299307

308+
await bot.change_presence(activity=activity)
300309

301310
music_queue.current_song_duration = song_duration
302311
music_queue.current_song_title = song_info
@@ -310,6 +319,10 @@ def wrapped_play_next(error):
310319
coro = play_song(interaction, None, None, None, None, send_message=True, music_queue=music_queue, play_called=False, play_next=True)
311320
task = asyncio.run_coroutine_threadsafe(coro, bot.loop)
312321
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)
313326

314327
voice_client.play(discord.FFmpegPCMAudio(url, **FFMPEG_OPTIONS), after=wrapped_play_next)
315328

0 commit comments

Comments
 (0)