1
Fork 0
mirror of https://github.com/allthingslinux/tux.git synced 2024-10-02 16:43:12 +00:00

refactor: replace Constants with Config in multiple files for better configuration management

The Constants module was replaced with a Config module in multiple files. This change was made to improve the management of configuration variables, making it easier to modify and maintain them. The Config module provides a more flexible and scalable way to handle configuration settings.
This commit is contained in:
kzndotsh 2024-09-11 13:55:45 -04:00
parent 7c7080a018
commit 450c1887fb
9 changed files with 52 additions and 51 deletions

View file

@ -6,13 +6,13 @@ import aiofiles.os
from discord.ext import commands
from loguru import logger
from tux.utils.constants import Constants as CONST
from tux.utils.config import CONFIG
class CogLoader(commands.Cog):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
self.cog_ignore_list: set[str] = CONST.COG_IGNORE_LIST
self.cog_ignore_list: set[str] = CONFIG.COG_IGNORE_LIST
async def is_cog_eligible(self, filepath: Path) -> bool:
"""

View file

@ -5,7 +5,7 @@ from tux.bot import Tux
from tux.ui.buttons import GithubButton
from tux.ui.embeds import EmbedCreator
from tux.utils import checks
from tux.utils.constants import Constants as CONST
from tux.utils.config import CONFIG
from tux.utils.flags import generate_usage
from tux.wrappers.github import GithubService
@ -16,7 +16,7 @@ class Git(commands.Cog):
def __init__(self, bot: Tux) -> None:
self.bot = bot
self.github = GithubService()
self.repo_url = CONST.GITHUB_REPO_URL
self.repo_url = CONFIG.GITHUB_REPO_URL
self.git.usage = generate_usage(self.git)
self.get_repo.usage = generate_usage(self.get_repo)
self.create_issue.usage = generate_usage(self.create_issue)

View file

@ -8,7 +8,7 @@ from loguru import logger
from tux.bot import Tux
from tux.utils import checks
from tux.utils.constants import Constants as CONST
from tux.utils.config import CONFIG
MailboxData = dict[str, str | list[str]]
@ -16,12 +16,12 @@ MailboxData = dict[str, str | list[str]]
class Mail(commands.Cog):
def __init__(self, bot: Tux) -> None:
self.bot = bot
self.api_url = CONST.MAILCOW_API_URL
self.api_url = CONFIG.MAILCOW_API_URL
self.headers = {
"Content-Type": "application/json",
"Accept": "application/json",
"X-API-Key": CONST.MAILCOW_API_KEY,
"Authorization": f"Bearer {CONST.MAILCOW_API_KEY}",
"X-API-Key": CONFIG.MAILCOW_API_KEY,
"Authorization": f"Bearer {CONFIG.MAILCOW_API_KEY}",
}
self.default_options: dict[str, str | list[str]] = {
"active": "1",

View file

@ -8,7 +8,7 @@ from tux.bot import Tux
from tux.database.controllers import DatabaseController
from tux.ui.embeds import EmbedCreator, EmbedType
from tux.ui.views.config import ConfigSetChannels, ConfigSetPrivateLogs, ConfigSetPublicLogs
from tux.utils.constants import CONST
from tux.utils.config import CONFIG
# TODO: Add onboarding setup to ensure all required channels, logs, and roles are set up
# TODO: Figure out how to handle using our custom checks because the current checks would result in a lock out
@ -380,7 +380,7 @@ class Config(commands.GroupCog, group_name="config"):
user_display_avatar=interaction.user.display_avatar.url,
embed_type=EmbedCreator.SUCCESS,
title="Guild Config",
description=f"The prefix was reset to `{CONST.DEFAULT_PREFIX}`",
description=f"The prefix was reset to `{CONFIG.DEFAULT_PREFIX}`",
),
)

View file

@ -2,7 +2,7 @@ import discord
from discord.ext import commands
from tux.bot import Tux
from tux.utils.constants import Constants as CONST
from tux.utils.config import CONFIG
class TempVc(commands.Cog):
@ -32,8 +32,8 @@ class TempVc(commands.Cog):
"""
# Ensure constants are set correctly
temp_channel_id = int(CONST.TEMPVC_CHANNEL_ID or "0")
temp_category_id = int(CONST.TEMPVC_CATEGORY_ID or "0")
temp_channel_id = int(CONFIG.TEMPVC_CHANNEL_ID or "0")
temp_category_id = int(CONFIG.TEMPVC_CATEGORY_ID or "0")
if temp_channel_id == 0 or temp_category_id == 0:
return

View file

@ -12,7 +12,8 @@ from reactionmenu.abc import Page
from reactionmenu.views_menu import ViewSelect
from tux.ui.embeds import EmbedCreator
from tux.utils.constants import Constants as CONST
from tux.utils.config import CONFIG
from tux.utils.constants import CONST
class TuxHelp(commands.HelpCommand):
@ -36,7 +37,7 @@ class TuxHelp(commands.HelpCommand):
The prefix used to invoke the bot.
"""
return self.context.clean_prefix or CONST.DEFAULT_PREFIX
return self.context.clean_prefix or CONFIG.DEFAULT_PREFIX
def _embed_base(self, title: str, description: str | None = None) -> discord.Embed:
"""

View file

@ -10,9 +10,9 @@ from sentry_sdk.integrations.loguru import LoguruIntegration
from tux.bot import Tux
from tux.database.controllers.guild_config import GuildConfigController
from tux.help import TuxHelp
from tux.utils.config import CONFIG
# from tux.utils.console import Console
from tux.utils.constants import Constants as CONST
async def get_prefix(bot: Tux, message: discord.Message) -> list[str]:
@ -21,19 +21,19 @@ async def get_prefix(bot: Tux, message: discord.Message) -> list[str]:
if message.guild:
prefix = await GuildConfigController().get_guild_prefix(message.guild.id)
return commands.when_mentioned_or(prefix or CONST.DEFAULT_PREFIX)(bot, message)
return commands.when_mentioned_or(prefix or CONFIG.DEFAULT_PREFIX)(bot, message)
async def main() -> None:
if not CONST.TOKEN:
if not CONFIG.TUX_TOKEN:
logger.critical("No token provided, exiting.")
return
logger.info("Setting up Sentry...")
sentry_sdk.init(
dsn=CONST.SENTRY_URL,
environment="dev" if CONST.DEV == "True" else "prod",
dsn=CONFIG.SENTRY_URL,
environment="dev" if CONFIG.TUX_ENV == "dev" else "prod",
traces_sample_rate=1.0,
profiles_sample_rate=1.0,
enable_tracing=True,
@ -47,7 +47,7 @@ async def main() -> None:
strip_after_prefix=True,
case_insensitive=True,
intents=discord.Intents.all(),
owner_ids=[*CONST.SYSADMIN_IDS, CONST.BOT_OWNER_ID],
owner_ids=[*CONFIG.SYSADMIN_IDS, CONFIG.BOT_OWNER_ID],
allowed_mentions=discord.AllowedMentions(everyone=False),
help_command=TuxHelp(),
)
@ -59,7 +59,7 @@ async def main() -> None:
# console = Console(bot)
# console_task = asyncio.create_task(console.run_console())
await bot.start(token=CONST.TOKEN, reconnect=True)
await bot.start(token=CONFIG.TUX_TOKEN, reconnect=True)
except KeyboardInterrupt:
logger.info("KeyboardInterrupt received, shutting down.")

View file

@ -8,7 +8,7 @@ from loguru import logger
from tux.bot import Tux
from tux.database.controllers import DatabaseController
from tux.utils.constants import CONST
from tux.utils.config import CONFIG
from tux.utils.exceptions import AppCommandPermissionLevelError, PermissionLevelError
db = DatabaseController().guild_config
@ -42,8 +42,8 @@ async def has_permission(
if isinstance(author, discord.Member) and any(role in [r.id for r in author.roles] for role in roles):
return True
return (8 in range(lower_bound, higher_bound + 1) and author.id in CONST.SYSADMIN_IDS) or (
9 in range(lower_bound, higher_bound + 1) and author.id == CONST.BOT_OWNER_ID
return (8 in range(lower_bound, higher_bound + 1) and author.id in CONFIG.SYSADMIN_IDS) or (
9 in range(lower_bound, higher_bound + 1) and author.id == CONFIG.BOT_OWNER_ID
)

View file

@ -8,26 +8,26 @@ from githubkit.versions.latest.models import (
)
from loguru import logger
from tux.utils.constants import Constants as CONST
from tux.utils.config import CONFIG
class GithubService:
def __init__(self) -> None:
self.github = GitHub(
AppInstallationAuthStrategy(
CONST.GITHUB_APP_ID,
CONST.GITHUB_PRIVATE_KEY,
int(CONST.GITHUB_INSTALLATION_ID),
CONST.GITHUB_CLIENT_ID,
CONST.GITHUB_CLIENT_SECRET,
CONFIG.GITHUB_APP_ID,
CONFIG.GITHUB_PRIVATE_KEY,
int(CONFIG.GITHUB_INSTALLATION_ID),
CONFIG.GITHUB_CLIENT_ID,
CONFIG.GITHUB_CLIENT_SECRET,
),
)
async def get_repo(self) -> FullRepository:
try:
response: Response[FullRepository] = await self.github.rest.repos.async_get(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
)
repo: FullRepository = response.parsed_data
@ -42,8 +42,8 @@ class GithubService:
async def create_issue(self, title: str, body: str) -> Issue:
try:
response: Response[Issue] = await self.github.rest.issues.async_create(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
title=title,
body=body,
)
@ -60,8 +60,8 @@ class GithubService:
async def create_issue_comment(self, issue_number: int, body: str) -> IssueComment:
try:
response: Response[IssueComment] = await self.github.rest.issues.async_create_comment(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
issue_number,
body=body,
)
@ -78,8 +78,8 @@ class GithubService:
async def close_issue(self, issue_number: int) -> Issue:
try:
response: Response[Issue] = await self.github.rest.issues.async_update(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
issue_number,
state="closed",
)
@ -96,8 +96,8 @@ class GithubService:
async def get_issue(self, issue_number: int) -> Issue:
try:
response: Response[Issue] = await self.github.rest.issues.async_get(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
issue_number,
)
@ -113,8 +113,8 @@ class GithubService:
async def get_open_issues(self) -> list[Issue]:
try:
response: Response[list[Issue]] = await self.github.rest.issues.async_list_for_repo(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
state="open",
)
@ -130,8 +130,8 @@ class GithubService:
async def get_closed_issues(self) -> list[Issue]:
try:
response: Response[list[Issue]] = await self.github.rest.issues.async_list_for_repo(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
state="closed",
)
@ -147,8 +147,8 @@ class GithubService:
async def get_open_pulls(self) -> list[PullRequestSimple]:
try:
response: Response[list[PullRequestSimple]] = await self.github.rest.pulls.async_list(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
state="open",
)
@ -164,8 +164,8 @@ class GithubService:
async def get_closed_pulls(self) -> list[PullRequestSimple]:
try:
response: Response[list[PullRequestSimple]] = await self.github.rest.pulls.async_list(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
state="closed",
)
@ -181,8 +181,8 @@ class GithubService:
async def get_pull(self, pr_number: int) -> PullRequest:
try:
response: Response[PullRequest] = await self.github.rest.pulls.async_get(
CONST.GITHUB_REPO_OWNER,
CONST.GITHUB_REPO,
CONFIG.GITHUB_REPO_OWNER,
CONFIG.GITHUB_REPO,
pr_number,
)