From e85d90fb36b7ab6e2a58ebcb8bc45e41d5b5ad5e Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Tue, 31 Dec 2024 20:03:40 -0500 Subject: [PATCH] feat(commands/voice/queue): add --duration --- commands/bot.py | 24 +++--------------------- commands/voice.py | 27 +++++++++++++++++++++++---- utils.py | 22 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/commands/bot.py b/commands/bot.py index 0180924..f29e03f 100644 --- a/commands/bot.py +++ b/commands/bot.py @@ -33,24 +33,6 @@ async def uptime(message): if args.since: await utils.reply(message, f"{round(start_time)}") else: - format_plural = lambda noun, count: noun if count == 1 else noun + "s" - - segments = [] - duration = int(time.time() - start_time) - - days, duration = divmod(duration, 86400) - if days >= 1: - segments.append(f"{days} {format_plural('day', days)}") - - hours, duration = divmod(duration, 3600) - if hours >= 1: - segments.append(f"{hours} {format_plural('hour', hours)}") - - minutes, duration = divmod(duration, 60) - if minutes >= 1: - segments.append(f"{minutes} {format_plural('minute', minutes)}") - - if duration > 0: - segments.append(f"{duration} {format_plural('second', duration)}") - - await utils.reply(message, f"up {', '.join(segments)}") + await utils.reply( + message, f"up {utils.format_duration(int(time.time() - start_time))}" + ) diff --git a/commands/voice.py b/commands/voice.py index 0898ab3..75b1675 100644 --- a/commands/voice.py +++ b/commands/voice.py @@ -1,9 +1,8 @@ import arguments -import youtubedl -from state import client, players - import commands import utils +import youtubedl +from state import client, players async def queue_or_play(message): @@ -61,10 +60,30 @@ async def queue_or_play(message): type=int, help="remove queued songs by queuer", ) + parser.add_argument( + "-d", + "--duration", + action="store_true", + help="print duration of queued songs", + ) if not (args := await parser.parse_args(message, tokens)): return - if args.clear: + if args.duration: + queued_songs = players[message.guild.id].queue + formatted_duration = utils.format_duration( + sum( + [ + queued.player.duration if queued.player.duration else 0 + for queued in queued_songs + ] + ) + ) + await utils.reply( + message, + f"queue is **{formatted_duration or '0 seconds'}** long (**{len(queued_songs)}** songs queued)", + ) + elif args.clear: players[message.guild.id].queue.clear() await utils.add_check_reaction(message) return diff --git a/utils.py b/utils.py index 9434a78..3e826eb 100644 --- a/utils.py +++ b/utils.py @@ -3,6 +3,28 @@ import disnake import constants +def format_duration(duration: int): + format_plural = lambda noun, count: noun if count == 1 else noun + "s" + segments = [] + + days, duration = divmod(duration, 86400) + if days >= 1: + segments.append(f"{days} {format_plural('day', days)}") + + hours, duration = divmod(duration, 3600) + if hours >= 1: + segments.append(f"{hours} {format_plural('hour', hours)}") + + minutes, duration = divmod(duration, 60) + if minutes >= 1: + segments.append(f"{minutes} {format_plural('minute', minutes)}") + + if duration > 0: + segments.append(f"{duration} {format_plural('second', duration)}") + + return ", ".join(segments) + + async def add_check_reaction(message): await message.add_reaction("✅")