Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
bc380ba
Add error logging for config setup
MattyTheHacker Mar 1, 2025
b9e57ac
Ignore ruff errors
MattyTheHacker Mar 1, 2025
f8e3cee
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 1, 2025
e763f92
test webhook
MattyTheHacker Mar 1, 2025
b0ae227
Merge branch 'config-error-notifications' of github.com:CSSUoB/TeX-Bo…
MattyTheHacker Mar 1, 2025
4215b25
Fix dev container fork check
MattyTheHacker Mar 1, 2025
a47057e
only one way to find out...
MattyTheHacker Mar 1, 2025
e12c35c
Merge remote-tracking branch 'origin/dev-fork-check' into config-erro…
MattyTheHacker Mar 1, 2025
0b8e38e
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 1, 2025
4e85010
Add discord logging setup to settings
MattyTheHacker Mar 1, 2025
7a927f1
fuck off ruff
MattyTheHacker Mar 1, 2025
755ac64
Fix logging level
MattyTheHacker Mar 1, 2025
a6a8eb1
Merge remote-tracking branch 'origin/main' into config-error-notifica…
MattyTheHacker Mar 3, 2025
f95b32f
Ruff format
MattyTheHacker Mar 3, 2025
fe01710
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 4, 2025
75acd86
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 6, 2025
732e46e
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 17, 2025
a7e9ba5
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 18, 2025
f16fdb4
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 31, 2025
41bd350
re-introduce logger dynamically
MattyTheHacker Mar 31, 2025
e101f0b
Improve handler logic
MattyTheHacker Mar 31, 2025
0c0f06e
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 31, 2025
75a9c5b
fix whitespace
MattyTheHacker Mar 31, 2025
221d2b6
Merge branch 'main' into config-error-notifications
MattyTheHacker Mar 31, 2025
32480b1
Fixes
MattyTheHacker Apr 1, 2025
7d0c4de
Merge branch 'main' into config-error-notifications
MattyTheHacker Apr 1, 2025
7bf94ea
fuck off
MattyTheHacker Apr 1, 2025
f46103d
Merge branch 'main' into config-error-notifications
MattyTheHacker Apr 9, 2025
c5e9cb8
Merge branch 'main' into config-error-notifications
CarrotManMatt Apr 12, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions cogs/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,25 @@ async def on_ready(self) -> None:
"""
if settings["DISCORD_LOG_CHANNEL_WEBHOOK_URL"]:
discord_logging_handler: logging.Handler = DiscordHandler(
self.bot.user.name if self.bot.user else "TeXBot",
settings["DISCORD_LOG_CHANNEL_WEBHOOK_URL"],
service_name=self.bot.user.name if self.bot.user else "TeX-Bot",
webhook_url=settings["DISCORD_LOG_CHANNEL_WEBHOOK_URL"],
avatar_url=(
self.bot.user.avatar.url
if self.bot.user and self.bot.user.avatar
else None
),
)
discord_logging_handler.setLevel(logging.WARNING)
# noinspection SpellCheckingInspection
discord_logging_handler.setFormatter(
logging.Formatter("{levelname} | {message}", style="{"),
logging.Formatter("{levelname} | {message}", style="{")
)

logger.addHandler(discord_logging_handler)

Comment thread
MattyTheHacker marked this conversation as resolved.
else:
logger.warning(
"DISCORD_LOG_CHANNEL_WEBHOOK_URL was not set, "
"so error logs will not be sent to the Discord log channel.",
"so error logs will not be sent to the Discord log channel."
)

try:
Expand Down
92 changes: 55 additions & 37 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import dotenv
import validators
from discord_logging.handler import DiscordHandler

from exceptions import (
ImproperlyConfiguredError,
Expand Down Expand Up @@ -177,30 +178,42 @@ def _setup_discord_bot_token(cls) -> None:
cls._settings["DISCORD_BOT_TOKEN"] = raw_discord_bot_token

@classmethod
def _setup_discord_log_channel_webhook_url(cls) -> None:
def _setup_discord_log_channel_webhook(cls) -> "Logger":
raw_discord_log_channel_webhook_url: str = os.getenv(
"DISCORD_LOG_CHANNEL_WEBHOOK_URL",
"",
"DISCORD_LOG_CHANNEL_WEBHOOK_URL", ""
)

DISCORD_LOG_CHANNEL_WEBHOOK_URL_IS_VALID: Final[bool] = bool(
not raw_discord_log_channel_webhook_url
or (
validators.url(raw_discord_log_channel_webhook_url)
and raw_discord_log_channel_webhook_url.startswith(
"https://discord.com/api/webhooks/",
)
),
)
if not DISCORD_LOG_CHANNEL_WEBHOOK_URL_IS_VALID:
if raw_discord_log_channel_webhook_url and (
not validators.url(raw_discord_log_channel_webhook_url)
or not raw_discord_log_channel_webhook_url.startswith(
"https://discord.com/api/webhooks/"
)
):
INVALID_DISCORD_LOG_CHANNEL_WEBHOOK_URL_MESSAGE: Final[str] = (
"DISCORD_LOG_CHANNEL_WEBHOOK_URL must be a valid webhook URL "
"that points to a discord channel where logs should be displayed."
)
raise ImproperlyConfiguredError(INVALID_DISCORD_LOG_CHANNEL_WEBHOOK_URL_MESSAGE)

webhook_config_logger: Logger = logging.getLogger("_temp_webhook_config")

if raw_discord_log_channel_webhook_url:
discord_logging_handler: logging.Handler = DiscordHandler(
service_name="TeX-Bot", webhook_url=raw_discord_log_channel_webhook_url
)

discord_logging_handler.setLevel(logging.WARNING)

discord_logging_handler.setFormatter(
logging.Formatter("{levelname} | {message}", style="{")
)

webhook_config_logger.addHandler(discord_logging_handler)

cls._settings["DISCORD_LOG_CHANNEL_WEBHOOK_URL"] = raw_discord_log_channel_webhook_url

return webhook_config_logger

@classmethod
def _setup_discord_guild_id(cls) -> None:
raw_discord_guild_id: str | None = os.getenv("DISCORD_GUILD_ID")
Expand Down Expand Up @@ -708,30 +721,35 @@ def _setup_env_variables(cls) -> None:

dotenv.load_dotenv()

cls._setup_logging()
cls._setup_discord_bot_token()
cls._setup_discord_log_channel_webhook_url()
cls._setup_discord_guild_id()
cls._setup_group_full_name()
cls._setup_group_short_name()
cls._setup_ping_command_easter_egg_probability()
cls._setup_welcome_messages()
cls._setup_roles_messages()
cls._setup_organisation_id()
cls._setup_members_list_auth_session_cookie()
cls._setup_membership_perks_url()
cls._setup_purchase_membership_url()
cls._setup_send_introduction_reminders()
cls._setup_send_introduction_reminders_delay()
cls._setup_send_introduction_reminders_interval()
cls._setup_send_get_roles_reminders()
cls._setup_send_get_roles_reminders_delay()
cls._setup_advanced_send_get_roles_reminders_interval()
cls._setup_statistics_days()
cls._setup_statistics_roles()
cls._setup_moderation_document_url()
cls._setup_strike_performed_manually_warning_location()
cls._setup_auto_add_committee_to_threads()
webhook_config_logger: Logger = cls._setup_discord_log_channel_webhook()

try:
cls._setup_logging()
cls._setup_discord_bot_token()
cls._setup_discord_guild_id()
cls._setup_group_full_name()
cls._setup_group_short_name()
cls._setup_ping_command_easter_egg_probability()
cls._setup_welcome_messages()
cls._setup_roles_messages()
cls._setup_organisation_id()
cls._setup_members_list_auth_session_cookie()
cls._setup_membership_perks_url()
cls._setup_purchase_membership_url()
cls._setup_send_introduction_reminders()
cls._setup_send_introduction_reminders_delay()
cls._setup_send_introduction_reminders_interval()
cls._setup_send_get_roles_reminders()
cls._setup_send_get_roles_reminders_delay()
cls._setup_advanced_send_get_roles_reminders_interval()
cls._setup_statistics_days()
cls._setup_statistics_roles()
cls._setup_moderation_document_url()
cls._setup_strike_performed_manually_warning_location()
cls._setup_auto_add_committee_to_threads()
except ImproperlyConfiguredError as improper_config_error:
webhook_config_logger.error(improper_config_error.message) # noqa: TRY400
raise improper_config_error from improper_config_error

cls._is_env_variables_setup = True

Expand Down