refactor: use from imports for constants

This commit is contained in:
Ryan 2025-01-08 09:26:02 -05:00
parent 07b3bde22d
commit 7c2e17e0d3
Signed by: ErrorNoInternet
GPG Key ID: 2486BFB7B1E6A4A3
4 changed files with 23 additions and 20 deletions

View File

@ -1,8 +1,9 @@
import disnake_paginator
import arguments import arguments
import commands import commands
import constants
import disnake_paginator
import utils import utils
from constants import EMBED_COLOR
from state import players from state import players
from .utils import command_allowed from .utils import command_allowed
@ -30,7 +31,7 @@ async def playing(message):
if description := source.description: if description := source.description:
paginator = disnake_paginator.ButtonPaginator( paginator = disnake_paginator.ButtonPaginator(
invalid_user_function=utils.invalid_user_handler, invalid_user_function=utils.invalid_user_handler,
color=constants.EMBED_COLOR, color=EMBED_COLOR,
title=source.title, title=source.title,
segments=disnake_paginator.split(description), segments=disnake_paginator.split(description),
) )

View File

@ -5,9 +5,9 @@ import disnake_paginator
import arguments import arguments
import commands import commands
import constants
import utils import utils
import youtubedl import youtubedl
from constants import EMBED_COLOR
from state import client, players from state import client, players
from .playback import resume from .playback import resume
@ -197,7 +197,7 @@ async def queue_or_play(message, edited=False):
def embed(description): def embed(description):
e = disnake.Embed( e = disnake.Embed(
description=description, description=description,
color=constants.EMBED_COLOR, color=EMBED_COLOR,
) )
if formatted_duration and len(players[message.guild.id].queue) > 1: if formatted_duration and len(players[message.guild.id].queue) > 1:
e.set_footer(text=f"{formatted_duration} in total") 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( await disnake_paginator.ButtonPaginator(
invalid_user_function=utils.invalid_user_handler, invalid_user_function=utils.invalid_user_handler,
color=constants.EMBED_COLOR, color=EMBED_COLOR,
segments=list( segments=list(
map( map(
embed, embed,

20
core.py
View File

@ -12,14 +12,14 @@ import disnake
import disnake_paginator import disnake_paginator
import commands import commands
import constants
import utils import utils
from commands import Command as C from commands import Command as C
from constants import EMBED_COLOR, OWNERS, PREFIX, RELOADABLE_MODULES
from state import client, command_locks, idle_tracker from state import client, command_locks, idle_tracker
async def on_message(message, edited=False): 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 return
tokens = commands.tokenize(message.content) tokens = commands.tokenize(message.content)
@ -46,7 +46,7 @@ async def on_message(message, edited=False):
try: try:
match matched[0]: 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() reloaded_modules = set()
start = time.time() start = time.time()
@ -54,17 +54,19 @@ async def on_message(message, edited=False):
rreload(reloaded_modules, __import__("extra")) rreload(reloaded_modules, __import__("extra"))
for module in filter( for module in filter(
lambda v: inspect.ismodule(v) lambda v: inspect.ismodule(v)
and v.__name__ in constants.RELOADABLE_MODULES, and v.__name__ in RELOADABLE_MODULES,
globals().values(), globals().values(),
): ):
rreload(reloaded_modules, module) rreload(reloaded_modules, module)
end = time.time() end = time.time()
if __debug__: 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) 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("`") code = message.content[len(tokens[0]) + 1 :].strip().strip("`")
for replacement in ["python", "py"]: for replacement in ["python", "py"]:
if code.startswith(replacement): if code.startswith(replacement):
@ -99,14 +101,14 @@ async def on_message(message, edited=False):
prefix="```\n", prefix="```\n",
suffix="```", suffix="```",
invalid_user_function=utils.invalid_user_handler, invalid_user_function=utils.invalid_user_handler,
color=constants.EMBED_COLOR, color=EMBED_COLOR,
segments=disnake_paginator.split(output), segments=disnake_paginator.split(output),
).start(utils.MessageInteractionWrapper(message)) ).start(utils.MessageInteractionWrapper(message))
elif len(output.strip()) == 0: elif len(output.strip()) == 0:
await utils.add_check_reaction(message) await utils.add_check_reaction(message)
else: else:
await utils.reply(message, output) 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) await commands.tools.clear(message)
case C.JOIN: case C.JOIN:
await commands.voice.join(message) await commands.voice.join(message)
@ -160,7 +162,7 @@ def rreload(reloaded_modules, module):
for submodule in filter( for submodule in filter(
lambda v: inspect.ismodule(v) 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, and v.__name__ not in reloaded_modules,
vars(module).values(), vars(module).values(),
): ):

View File

@ -6,9 +6,9 @@ from typing import Any, Optional
import disnake import disnake
import yt_dlp 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): class CustomAudioSource(disnake.AudioSource):
@ -109,12 +109,12 @@ class QueuedSong:
progress = self.player.original.progress / self.player.duration progress = self.player.original.progress / self.player.duration
embed = disnake.Embed( embed = disnake.Embed(
color=constants.EMBED_COLOR, color=EMBED_COLOR,
title=self.player.title, title=self.player.title,
url=self.player.original_url, url=self.player.original_url,
description=( description=(
f"{'⏸️ ' if is_paused else ''}" 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)}%**)" f"**{format_duration(int(self.player.original.progress))}** / **{format_duration(self.player.duration)}** (**{round(progress * 100)}%**)"
if self.player.duration if self.player.duration
@ -179,4 +179,4 @@ def format_duration(duration: int | float) -> str:
def __reload_module__(): def __reload_module__():
global ytdl global ytdl
ytdl = yt_dlp.YoutubeDL(constants.YTDL_OPTIONS) ytdl = yt_dlp.YoutubeDL(YTDL_OPTIONS)