From 7c2e17e0d34b6d12ffdfeba99ef8571fd265c10d Mon Sep 17 00:00:00 2001 From: ErrorNoInternet Date: Wed, 8 Jan 2025 09:26:02 -0500 Subject: [PATCH] refactor: use from imports for constants --- commands/voice/playback.py | 7 ++++--- commands/voice/queue.py | 6 +++--- core.py | 20 +++++++++++--------- youtubedl.py | 10 +++++----- 4 files changed, 23 insertions(+), 20 deletions(-) diff --git a/commands/voice/playback.py b/commands/voice/playback.py index 34218bf..6bd34bc 100644 --- a/commands/voice/playback.py +++ b/commands/voice/playback.py @@ -1,8 +1,9 @@ +import disnake_paginator + import arguments import commands -import constants -import disnake_paginator import utils +from constants import EMBED_COLOR from state import players from .utils import command_allowed @@ -30,7 +31,7 @@ async def playing(message): if description := source.description: paginator = disnake_paginator.ButtonPaginator( invalid_user_function=utils.invalid_user_handler, - color=constants.EMBED_COLOR, + color=EMBED_COLOR, title=source.title, segments=disnake_paginator.split(description), ) diff --git a/commands/voice/queue.py b/commands/voice/queue.py index 7251a8e..275920b 100644 --- a/commands/voice/queue.py +++ b/commands/voice/queue.py @@ -5,9 +5,9 @@ import disnake_paginator import arguments import commands -import constants import utils import youtubedl +from constants import EMBED_COLOR from state import client, players from .playback import resume @@ -197,7 +197,7 @@ async def queue_or_play(message, edited=False): def embed(description): e = disnake.Embed( description=description, - color=constants.EMBED_COLOR, + color=EMBED_COLOR, ) if formatted_duration and len(players[message.guild.id].queue) > 1: e.set_footer(text=f"{formatted_duration} in total") @@ -205,7 +205,7 @@ async def queue_or_play(message, edited=False): await disnake_paginator.ButtonPaginator( invalid_user_function=utils.invalid_user_handler, - color=constants.EMBED_COLOR, + color=EMBED_COLOR, segments=list( map( embed, diff --git a/core.py b/core.py index da2b29d..6949f55 100644 --- a/core.py +++ b/core.py @@ -12,14 +12,14 @@ import disnake import disnake_paginator import commands -import constants import utils from commands import Command as C +from constants import EMBED_COLOR, OWNERS, PREFIX, RELOADABLE_MODULES from state import client, command_locks, idle_tracker async def on_message(message, edited=False): - if not message.content.startswith(constants.PREFIX) or message.author.bot: + if not message.content.startswith(PREFIX) or message.author.bot: return tokens = commands.tokenize(message.content) @@ -46,7 +46,7 @@ async def on_message(message, edited=False): try: match matched[0]: - case C.RELOAD if message.author.id in constants.OWNERS: + case C.RELOAD if message.author.id in OWNERS: reloaded_modules = set() start = time.time() @@ -54,17 +54,19 @@ async def on_message(message, edited=False): rreload(reloaded_modules, __import__("extra")) for module in filter( lambda v: inspect.ismodule(v) - and v.__name__ in constants.RELOADABLE_MODULES, + and v.__name__ in RELOADABLE_MODULES, globals().values(), ): rreload(reloaded_modules, module) end = time.time() if __debug__: - debug(f"reloaded {len(reloaded_modules)} modules in {round(end-start, 2)}s") + debug( + f"reloaded {len(reloaded_modules)} modules in {round(end-start, 2)}s" + ) await utils.add_check_reaction(message) - case C.EXECUTE if message.author.id in constants.OWNERS: + case C.EXECUTE if message.author.id in OWNERS: code = message.content[len(tokens[0]) + 1 :].strip().strip("`") for replacement in ["python", "py"]: if code.startswith(replacement): @@ -99,14 +101,14 @@ async def on_message(message, edited=False): prefix="```\n", suffix="```", invalid_user_function=utils.invalid_user_handler, - color=constants.EMBED_COLOR, + color=EMBED_COLOR, segments=disnake_paginator.split(output), ).start(utils.MessageInteractionWrapper(message)) elif len(output.strip()) == 0: await utils.add_check_reaction(message) else: await utils.reply(message, output) - case C.CLEAR | C.PURGE if message.author.id in constants.OWNERS: + case C.CLEAR | C.PURGE if message.author.id in OWNERS: await commands.tools.clear(message) case C.JOIN: await commands.voice.join(message) @@ -160,7 +162,7 @@ def rreload(reloaded_modules, module): for submodule in filter( lambda v: inspect.ismodule(v) - and v.__name__ in constants.RELOADABLE_MODULES + and v.__name__ in RELOADABLE_MODULES and v.__name__ not in reloaded_modules, vars(module).values(), ): diff --git a/youtubedl.py b/youtubedl.py index 5734bbb..5ebb704 100644 --- a/youtubedl.py +++ b/youtubedl.py @@ -6,9 +6,9 @@ from typing import Any, Optional import disnake import yt_dlp -import constants +from constants import BAR_LENGTH, EMBED_COLOR, YTDL_OPTIONS -ytdl = yt_dlp.YoutubeDL(constants.YTDL_OPTIONS) +ytdl = yt_dlp.YoutubeDL(YTDL_OPTIONS) class CustomAudioSource(disnake.AudioSource): @@ -109,12 +109,12 @@ class QueuedSong: progress = self.player.original.progress / self.player.duration embed = disnake.Embed( - color=constants.EMBED_COLOR, + color=EMBED_COLOR, title=self.player.title, url=self.player.original_url, description=( f"{'⏸️ ' if is_paused else ''}" - f"`[{'#'*int(progress * constants.BAR_LENGTH)}{'-'*int((1 - progress) * constants.BAR_LENGTH)}]` " + f"`[{'#'*int(progress * BAR_LENGTH)}{'-'*int((1 - progress) * BAR_LENGTH)}]` " + ( f"**{format_duration(int(self.player.original.progress))}** / **{format_duration(self.player.duration)}** (**{round(progress * 100)}%**)" if self.player.duration @@ -179,4 +179,4 @@ def format_duration(duration: int | float) -> str: def __reload_module__(): global ytdl - ytdl = yt_dlp.YoutubeDL(constants.YTDL_OPTIONS) + ytdl = yt_dlp.YoutubeDL(YTDL_OPTIONS)