From 88f2e1b2c8d91d40ca668432267b0204d7a54461 Mon Sep 17 00:00:00 2001 From: Nyhilo Date: Mon, 22 Nov 2021 17:45:16 -0700 Subject: [PATCH 1/5] Reformat queue command output It's now more compact and provides more info --- musicbot/commands/music.py | 23 ++++++++++++++++------- musicbot/utils.py | 16 ++++++++++++++++ 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/musicbot/commands/music.py b/musicbot/commands/music.py index 16a33400..bd9a441d 100644 --- a/musicbot/commands/music.py +++ b/musicbot/commands/music.py @@ -132,20 +132,29 @@ async def _queue(self, ctx): playlist = utils.guild_to_audiocontroller[current_guild].playlist - # Embeds are limited to 25 fields + # Embeds are limited to 4096 characters, this is well under that if config.MAX_SONG_PRELOAD > 25: config.MAX_SONG_PRELOAD = 25 - embed = discord.Embed(title=":scroll: Queue [{}]".format( - len(playlist.playque)), color=config.EMBED_COLOR, inline=False) + total_runtime = utils.format_time( + sum([int(song.info.duration if song.info.duration else 0) for song in list(playlist.playque)])) + embed = discord.Embed(title=":scroll: {} songs in queue | {} total length".format( + len(playlist.playque), total_runtime), color=config.EMBED_COLOR, inline=False) + + in_queue_formats = [] for counter, song in enumerate(list(playlist.playque)[:config.MAX_SONG_PRELOAD], start=1): if song.info.title is None: - embed.add_field(name="{}.".format(str(counter)), value="[{}]({})".format( - song.info.webpage_url, song.info.webpage_url), inline=False) + in_queue_formats.append("`{}.` [{}]({}) `{}`".format( + str(counter), song.info.webpage_url, song.info.webpage_url, + utils.format_time(song.info.duration))) else: - embed.add_field(name="{}.".format(str(counter)), value="[{}]({})".format( - song.info.title, song.info.webpage_url), inline=False) + in_queue_formats.append("`{}.` [{}]({}) `{}`".format( + str(counter), song.info.title, song.info.webpage_url, + utils.format_time(song.info.duration))) + + if len(in_queue_formats): + embed.description = '\n'.join(in_queue_formats) await ctx.send(embed=embed) diff --git a/musicbot/utils.py b/musicbot/utils.py index 8544d02c..a810d79a 100644 --- a/musicbot/utils.py +++ b/musicbot/utils.py @@ -78,6 +78,22 @@ async def play_check(ctx): return False +def format_time(duration): + if not duration: + return "00:00" + + hours = duration // 60 // 60 + minutes = duration // 60 % 60 + seconds = duration % 60 + + # Looks like `h:mm:ss` + return "{}{}{:02d}:{:02d}".format( + hours if hours else "", + ":" if hours else "", + minutes, + seconds + ) + class Timer: def __init__(self, callback): self._callback = callback From 25a1c689c820a3fcfab323a7186c24c4a86bacb3 Mon Sep 17 00:00:00 2001 From: Nyhilo Date: Mon, 22 Nov 2021 18:43:41 -0700 Subject: [PATCH 2/5] Announce current song on when new song starts to play (configurable) --- config/config.py | 4 ++-- musicbot/audiocontroller.py | 6 ++++++ musicbot/commands/music.py | 6 +++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/config/config.py b/config/config.py index f7e23539..e7831afe 100644 --- a/config/config.py +++ b/config/config.py @@ -17,7 +17,7 @@ VC_TIMEOUT = 600 #seconds VC_TIMOUT_DEFAULT = True #default template setting for VC timeout true= yes, timeout false= no timeout ALLOW_VC_TIMEOUT_EDIT = True #allow or disallow editing the vc_timeout guild setting - +AUTO_ANOUNCE_TRACK_ON_PLAY = True #anounce currently playing track when a new track starts playing STARTUP_MESSAGE = "Starting Bot..." STARTUP_COMPLETE_MESSAGE = "Startup Complete" @@ -44,7 +44,7 @@ SONGINFO_DISLIKES = "Dislikes: " SONGINFO_NOW_PLAYING = "Now Playing" SONGINFO_QUEUE_ADDED = "Added to queue" -SONGINFO_SONGINFO = "Song info" +SONGINFO_SONGINFO = "Now playing" SONGINFO_UNKNOWN_SITE = "Unknown site :question:" SONGINFO_PLAYLIST_QUEUED = "Queued playlist :page_with_curl:" SONGINFO_UNKNOWN_DURATION = "Unknown" diff --git a/musicbot/audiocontroller.py b/musicbot/audiocontroller.py index 52d9dc24..d3682988 100644 --- a/musicbot/audiocontroller.py +++ b/musicbot/audiocontroller.py @@ -36,6 +36,8 @@ def __init__(self, bot, guild): self.timer = utils.Timer(self.timeout_handler) + self.ctx = None + @property def volume(self): return self._volume @@ -102,6 +104,9 @@ async def play_song(self, song): self.voice_client.play(discord.FFmpegPCMAudio( song.base_url, before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'), after=lambda e: self.next_song(e)) + if config.AUTO_ANOUNCE_TRACK_ON_PLAY: + await self.ctx.invoke(self.bot.get_command('songinfo')) + self.voice_client.source = discord.PCMVolumeTransformer( self.guild.voice_client.source) self.voice_client.source.volume = float(self.volume) / 100.0 @@ -327,6 +332,7 @@ async def timeout_handler(self): await self.udisconnect() async def uconnect(self, ctx): + self.ctx = ctx if not ctx.author.voice: await ctx.send(config.NO_GUILD_MESSAGE) diff --git a/musicbot/commands/music.py b/musicbot/commands/music.py index bd9a441d..c08978ab 100644 --- a/musicbot/commands/music.py +++ b/musicbot/commands/music.py @@ -52,10 +52,10 @@ async def _play_song(self, ctx, *, track: str): if song.origin == linkutils.Origins.Default: - if audiocontroller.current_song != None and len(audiocontroller.playlist.playque) == 0: - await ctx.send(embed=song.info.format_output(config.SONGINFO_NOW_PLAYING)) - else: + if audiocontroller.current_song == None or len(audiocontroller.playlist.playque) != 0: await ctx.send(embed=song.info.format_output(config.SONGINFO_QUEUE_ADDED)) + elif not config.AUTO_ANOUNCE_TRACK_ON_PLAY: + await ctx.send(embed=song.info.format_output(config.SONGINFO_NOW_PLAYING)) elif song.origin == linkutils.Origins.Playlist: await ctx.send(config.SONGINFO_PLAYLIST_QUEUED) From 105bf9e1725ae5f5aa37a84c1d1efef5584e6dba Mon Sep 17 00:00:00 2001 From: Nyhilo Date: Mon, 22 Nov 2021 18:47:52 -0700 Subject: [PATCH 3/5] Fix typo in "Announce" --- config/config.py | 2 +- musicbot/audiocontroller.py | 2 +- musicbot/commands/music.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/config/config.py b/config/config.py index e7831afe..0c2a740b 100644 --- a/config/config.py +++ b/config/config.py @@ -17,7 +17,7 @@ VC_TIMEOUT = 600 #seconds VC_TIMOUT_DEFAULT = True #default template setting for VC timeout true= yes, timeout false= no timeout ALLOW_VC_TIMEOUT_EDIT = True #allow or disallow editing the vc_timeout guild setting -AUTO_ANOUNCE_TRACK_ON_PLAY = True #anounce currently playing track when a new track starts playing +AUTO_ANNOUNCE_TRACK_ON_PLAY = True #announce currently playing track when a new track starts playing STARTUP_MESSAGE = "Starting Bot..." STARTUP_COMPLETE_MESSAGE = "Startup Complete" diff --git a/musicbot/audiocontroller.py b/musicbot/audiocontroller.py index d3682988..66e7272b 100644 --- a/musicbot/audiocontroller.py +++ b/musicbot/audiocontroller.py @@ -104,7 +104,7 @@ async def play_song(self, song): self.voice_client.play(discord.FFmpegPCMAudio( song.base_url, before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'), after=lambda e: self.next_song(e)) - if config.AUTO_ANOUNCE_TRACK_ON_PLAY: + if config.AUTO_ANNOUNCE_TRACK_ON_PLAY: await self.ctx.invoke(self.bot.get_command('songinfo')) self.voice_client.source = discord.PCMVolumeTransformer( diff --git a/musicbot/commands/music.py b/musicbot/commands/music.py index c08978ab..67a43a3c 100644 --- a/musicbot/commands/music.py +++ b/musicbot/commands/music.py @@ -54,7 +54,7 @@ async def _play_song(self, ctx, *, track: str): if audiocontroller.current_song == None or len(audiocontroller.playlist.playque) != 0: await ctx.send(embed=song.info.format_output(config.SONGINFO_QUEUE_ADDED)) - elif not config.AUTO_ANOUNCE_TRACK_ON_PLAY: + elif not config.AUTO_ANNOUNCE_TRACK_ON_PLAY: await ctx.send(embed=song.info.format_output(config.SONGINFO_NOW_PLAYING)) elif song.origin == linkutils.Origins.Playlist: From fab7c74ba6f53c4294f514d72c01d505c033d29b Mon Sep 17 00:00:00 2001 From: Nyhilo Date: Sun, 28 Nov 2021 15:54:29 -0700 Subject: [PATCH 4/5] Move auto-announce config to settings.py --- config/config.py | 1 - musicbot/audiocontroller.py | 4 +++- musicbot/commands/music.py | 4 +++- musicbot/settings.py | 12 +++++++++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/config/config.py b/config/config.py index 0c2a740b..08276cfd 100644 --- a/config/config.py +++ b/config/config.py @@ -17,7 +17,6 @@ VC_TIMEOUT = 600 #seconds VC_TIMOUT_DEFAULT = True #default template setting for VC timeout true= yes, timeout false= no timeout ALLOW_VC_TIMEOUT_EDIT = True #allow or disallow editing the vc_timeout guild setting -AUTO_ANNOUNCE_TRACK_ON_PLAY = True #announce currently playing track when a new track starts playing STARTUP_MESSAGE = "Starting Bot..." STARTUP_COMPLETE_MESSAGE = "Startup Complete" diff --git a/musicbot/audiocontroller.py b/musicbot/audiocontroller.py index 66e7272b..201848b2 100644 --- a/musicbot/audiocontroller.py +++ b/musicbot/audiocontroller.py @@ -103,8 +103,10 @@ async def play_song(self, song): self.voice_client.play(discord.FFmpegPCMAudio( song.base_url, before_options='-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5'), after=lambda e: self.next_song(e)) + + sett = utils.guild_to_settings[self.guild] - if config.AUTO_ANNOUNCE_TRACK_ON_PLAY: + if self.ctx is not None and sett.get("announce_tracks"): await self.ctx.invoke(self.bot.get_command('songinfo')) self.voice_client.source = discord.PCMVolumeTransformer( diff --git a/musicbot/commands/music.py b/musicbot/commands/music.py index 67a43a3c..a8ce7a4b 100644 --- a/musicbot/commands/music.py +++ b/musicbot/commands/music.py @@ -52,9 +52,11 @@ async def _play_song(self, ctx, *, track: str): if song.origin == linkutils.Origins.Default: + sett = utils.guild_to_settings[self.guild] + if audiocontroller.current_song == None or len(audiocontroller.playlist.playque) != 0: await ctx.send(embed=song.info.format_output(config.SONGINFO_QUEUE_ADDED)) - elif not config.AUTO_ANNOUNCE_TRACK_ON_PLAY: + elif not sett.get("announce_tracks"): await ctx.send(embed=song.info.format_output(config.SONGINFO_NOW_PLAYING)) elif song.origin == linkutils.Origins.Playlist: diff --git a/musicbot/settings.py b/musicbot/settings.py index c523e731..19482535 100644 --- a/musicbot/settings.py +++ b/musicbot/settings.py @@ -23,7 +23,8 @@ def __init__(self, guild): "user_must_be_in_vc": True, "button_emote": "", "default_volume": 100, - "vc_timeout": config.VC_TIMOUT_DEFAULT + "vc_timeout": config.VC_TIMOUT_DEFAULT, + "announce_tracks": False } self.reload() @@ -136,6 +137,7 @@ async def process_setting(self, setting, value, ctx): 'button_emote': lambda: self.button_emote(setting, value, ctx), 'default_volume': lambda: self.default_volume(setting, value, ctx), 'vc_timeout': lambda: self.vc_timeout(setting, value, ctx), + 'announce_tracks': lambda: self.announce_tracks(setting, value, ctx), } func = switcher.get(setting) @@ -242,3 +244,11 @@ async def vc_timeout(self, setting, value, ctx): else: await ctx.send("`Error: Value must be True/False`\nUsage: {}set {} True/False".format(config.BOT_PREFIX, setting)) return False + + async def announce_tracks(self, setting, value, ctx): + if value.lower() == "true": + self.config[setting] = True + elif value.lower() == "false": + self.config[setting] = False + else: + await ctx.send("`Error: Value must be True/False`\nUsage: {}set {} True/False".format(config.BOT_PREFIX, setting)) From f3672cb9fdc4c9c52abec4536d09488bfdeed867 Mon Sep 17 00:00:00 2001 From: Nyhilo Date: Wed, 1 Dec 2021 16:15:36 -0700 Subject: [PATCH 5/5] Change self.guild to ctx.guild Co-authored-by: Raptor123471 --- musicbot/commands/music.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/musicbot/commands/music.py b/musicbot/commands/music.py index 89472d37..270a7b61 100644 --- a/musicbot/commands/music.py +++ b/musicbot/commands/music.py @@ -48,7 +48,7 @@ async def _play_song(self, ctx, *, track: str): if song.origin == linkutils.Origins.Default: - sett = utils.guild_to_settings[self.guild] + sett = utils.guild_to_settings[ctx.guild] if audiocontroller.current_song == None or len(audiocontroller.playlist.playque) != 0: await ctx.send(embed=song.info.format_output(config.SONGINFO_QUEUE_ADDED))