Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add debug logging everywhere #239

Draft
wants to merge 20 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
ce0b735
add archive debug logging
MattyTheHacker May 27, 2024
da454e7
add more debug logging
MattyTheHacker May 27, 2024
c3a047f
even more debug messageS
MattyTheHacker May 27, 2024
f91d9b6
ruff deez nuts
MattyTheHacker May 27, 2024
372d1a2
change redundant debug messages
MattyTheHacker Jun 4, 2024
414e68b
Merge remote-tracking branch 'refs/remotes/origin/main' into add-debu…
CarrotManMatt Jun 15, 2024
3816625
Remove repeated code
CarrotManMatt Jun 15, 2024
3c9dea6
Add TODO
CarrotManMatt Jun 15, 2024
7e8e187
Fix duplicated logging messages
CarrotManMatt Jun 15, 2024
89776a0
Remove incorrect TODOs
CarrotManMatt Jun 15, 2024
2e0b074
Merge branch 'main' into add-debug-logging-everywhere
CarrotManMatt Jun 26, 2024
8f8e7f9
Merge branch 'main' into add-debug-logging-everywhere
CarrotManMatt Jul 11, 2024
ad6bb7a
Merge branch 'main' into add-debug-logging-everywhere
CarrotManMatt Jul 15, 2024
a7ebc51
Merge branch 'main' into add-debug-logging-everywhere
CarrotManMatt Jul 21, 2024
164a5eb
Merge branch 'main' into add-debug-logging-everywhere
CarrotManMatt Jul 21, 2024
f8cf646
Merge branch 'main' into add-debug-logging-everywhere
CarrotManMatt Jul 25, 2024
d508630
Merge branch 'main' into add-debug-logging-everywhere
CarrotManMatt Jul 27, 2024
e9ca472
Merge remote-tracking branch 'refs/remotes/origin/main' into add-debu…
CarrotManMatt Jul 29, 2024
b5523b3
Merge remote-tracking branch 'refs/remotes/origin/main' into add-debu…
CarrotManMatt Jul 29, 2024
69cd011
Merge branch 'main' into add-debug-logging-everywhere
CarrotManMatt Aug 1, 2024
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
4 changes: 4 additions & 0 deletions cogs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"ArchiveCommandCog",
"GetTokenAuthorisationCommandCog",
"CommandErrorCog",
"CommandSuccessCog",
"DeleteAllCommandsCog",
"EditMessageCommandCog",
"EnsureMembersInductedCommandCog",
Expand Down Expand Up @@ -49,8 +50,10 @@
AnnualYearChannelsIncrementCommandCog,
CommitteeHandoverCommandCog,
)

from .archive import ArchiveCommandCog
from .command_error import CommandErrorCog
from .command_success import CommandSuccessCog
from .delete_all import DeleteAllCommandsCog
from .edit_message import EditMessageCommandCog
from .get_token_authorisation import GetTokenAuthorisationCommandCog
Expand Down Expand Up @@ -85,6 +88,7 @@ def setup(bot: TeXBot) -> None:
ArchiveCommandCog,
GetTokenAuthorisationCommandCog,
CommandErrorCog,
CommandSuccessCog,
DeleteAllCommandsCog,
EditMessageCommandCog,
EnsureMembersInductedCommandCog,
Expand Down
14 changes: 2 additions & 12 deletions cogs/archive.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ async def archive(self, ctx: TeXBotApplicationContext, str_category_id: str) ->
),
ephemeral=True,
)
self.log_user_error(message=f"Category {category.name} was already archived")
return

channel: AllChannelTypes
Expand Down Expand Up @@ -189,10 +190,6 @@ async def archive(self, ctx: TeXBotApplicationContext, str_category_id: str) ->
ctx,
message=f"Channel {channel.mention} had invalid permissions",
)
logger.error(
"Channel %s had invalid permissions, so could not be archived.",
channel.name,
)
return

except discord.Forbidden:
Expand All @@ -203,14 +200,7 @@ async def archive(self, ctx: TeXBotApplicationContext, str_category_id: str) ->
"the channels in the selected category."
),
)
logger.error( # noqa: TRY400
(
"TeX-Bot did not have access to "
"the channels in the selected category: "
"%s."
),
category.name,
)
return

await ctx.respond("Category successfully archived", ephemeral=True)
logger.debug("Category %s has been successfully archived.", category.name)
21 changes: 21 additions & 0 deletions cogs/command_success.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""Contains cog classes for any command_success interactions."""

from collections.abc import Sequence

__all__: Sequence[str] = ("CommandSuccessCog",)


import logging
from logging import Logger

from utils import TeXBotApplicationContext, TeXBotBaseCog

logger: Logger = logging.getLogger("TeX-Bot")


class CommandSuccessCog(TeXBotBaseCog):
"""Cog class that defines additional code to execute upon a command success."""

@TeXBotBaseCog.listener()
async def on_application_command_completion(self, ctx: TeXBotApplicationContext) -> None:
logger.debug("Command execution complete.") # TODO: Pass command name to logger's extra
6 changes: 6 additions & 0 deletions cogs/delete_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
__all__: Sequence[str] = ("DeleteAllCommandsCog",)


import logging
from logging import Logger

import discord

from db.core.models import DiscordReminder, GroupMadeMember
from db.core.models.utils import AsyncBaseModel
from utils import CommandChecks, TeXBotApplicationContext, TeXBotBaseCog

logger: Logger = logging.getLogger("TeX-Bot")


class DeleteAllCommandsCog(TeXBotBaseCog):
"""Cog class that defines the "/delete-all" command group and command call-back methods."""
Expand Down Expand Up @@ -38,6 +43,7 @@ async def _delete_all(ctx: TeXBotApplicationContext, delete_model: type[AsyncBas
f"All {delete_model_instances_name_plural} deleted successfully.",
ephemeral=True,
)
logger.debug("All %s have been deleted.", delete_model_instances_name_plural)

@delete_all.command(
name="reminders",
Expand Down
5 changes: 5 additions & 0 deletions cogs/edit_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
__all__: Sequence[str] = ("EditMessageCommandCog",)


import logging
import re
from collections.abc import Set
from logging import Logger

import discord

Expand All @@ -19,6 +21,8 @@
TeXBotBaseCog,
)

logger: Logger = logging.getLogger("TeX-Bot")


class EditMessageCommandCog(TeXBotBaseCog):
# noinspection SpellCheckingInspection
Expand Down Expand Up @@ -136,3 +140,4 @@ async def edit_message(self, ctx: TeXBotApplicationContext, str_channel_id: str,
return
else:
await ctx.respond("Message edited successfully.", ephemeral=True)
logger.debug("Message ID %s has been edited successfully.", message_id)
25 changes: 25 additions & 0 deletions cogs/induct.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ async def on_member_update(self, before: discord.Member, after: discord.Member)
"(You can do this by right-clicking your name in the members-list "
"to the right & selecting \"Edit Server Profile\").",
)
logger.debug("Sent welcome message to %s", after)
if user_type != "member":
await after.send(
f"You can also get yourself an annual membership "
Expand All @@ -122,6 +123,7 @@ async def on_member_update(self, before: discord.Member, after: discord.Member)
f"the {self.bot.group_short_name} Discord server:green_square:! "
f"Checkout all the perks at {settings["MEMBERSHIP_PERKS_URL"]}",
)
logger.debug("Sent member message to %s", after)
except discord.Forbidden:
logger.info(
"Failed to open DM channel to user %s so no welcome message was sent.",
Expand Down Expand Up @@ -213,6 +215,12 @@ async def _perform_induction(self, ctx: TeXBotApplicationContext, induction_memb
"User has already been inducted. :information_source:"
),
)
self.log_user_error(
message=(
f"User {induction_member} was not inducted "
f"because they already have the guest role."
),
)
return

if not silent:
Expand All @@ -231,6 +239,10 @@ async def _perform_induction(self, ctx: TeXBotApplicationContext, induction_memb
and "grab your roles" in message.content # noqa: COM812
)
if message_already_sent:
logger.debug(
"Welcome message not sent to %s because it's already been sent!",
induction_member,
)
break

if not message_already_sent:
Expand All @@ -239,11 +251,13 @@ async def _perform_induction(self, ctx: TeXBotApplicationContext, induction_memb
f"Remember to grab your roles in {roles_channel_mention} "
"and say hello to everyone here! :wave:",
)
logger.debug("General induction message for user %s has been sent.")

await induction_member.add_roles(
guest_role,
reason=INDUCT_AUDIT_MESSAGE,
)
logger.debug("Added guest role to %s", induction_member)

# noinspection PyUnusedLocal
applicant_role: discord.Role | None = None
Expand All @@ -255,10 +269,12 @@ async def _perform_induction(self, ctx: TeXBotApplicationContext, induction_memb
applicant_role,
reason=INDUCT_AUDIT_MESSAGE,
)
logger.debug("Removed Applicant role from %s", induction_member)

tex_emoji: discord.Emoji | None = self.bot.get_emoji(743218410409820213)
if not tex_emoji:
tex_emoji = discord.utils.get(main_guild.emojis, name="TeX")
logger.debug("Could not find the TeX emoji!")

if intro_channel:
recent_message: discord.Message
Expand All @@ -281,6 +297,10 @@ async def _perform_induction(self, ctx: TeXBotApplicationContext, induction_memb
break

await initial_response.edit(content=":white_check_mark: User inducted successfully.")
logger.debug(
"Induction completed successfully for user %s",
induction_member,
)


class InductSlashCommandCog(BaseInductCog):
Expand Down Expand Up @@ -501,3 +521,8 @@ async def ensure_members_inducted(self, ctx: TeXBotApplicationContext) -> None:
),
ephemeral=True,
)
logger.debug(
"Successfully inducted members"
if changes_made
else "No members have been inducted" # noqa: COM812
)
3 changes: 3 additions & 0 deletions cogs/kill.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ async def kill(self, ctx: TeXBotApplicationContext) -> None:
),
view=ConfirmKillView(),
)
logger.debug("Sent kill confirmation message.")
confirmation_message: discord.Message = (
response
if isinstance(response, discord.Message)
Expand All @@ -89,6 +90,8 @@ async def kill(self, ctx: TeXBotApplicationContext) -> None:
),
)

logger.debug("Kill confirmation received: %s", button_interaction)

if button_interaction.data["custom_id"] == "shutdown_confirm": # type: ignore[index, typeddict-item]
await confirmation_message.edit(
content="My battery is low and it's getting dark...",
Expand Down
19 changes: 19 additions & 0 deletions cogs/make_member.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ async def make_member(self, ctx: TeXBotApplicationContext, group_member_id: str)
),
ephemeral=True,
)
self.log_user_error(
message=f"User {interaction_member} already had the member role!",
)
return

if not re.fullmatch(r"\A\d{7}\Z", group_member_id):
Expand Down Expand Up @@ -161,6 +164,7 @@ async def make_member(self, ctx: TeXBotApplicationContext, group_member_id: str)
),
ephemeral=True,
)
self.log_user_error(message=f"Student ID {group_member_id} has already been used.")
return

guild_member_ids: set[str] = set()
Expand Down Expand Up @@ -249,6 +253,10 @@ async def make_member(self, ctx: TeXBotApplicationContext, group_member_id: str)
raise

await ctx.respond("Successfully made you a member!", ephemeral=True)
logger.debug(
"User %s used the make member command successfully.",
interaction_member,
)

try:
guest_role: discord.Role = await self.bot.guest_role
Expand All @@ -264,6 +272,11 @@ async def make_member(self, ctx: TeXBotApplicationContext, group_member_id: str)
guest_role,
reason="TeX Bot slash-command: \"/makemember\"",
)
logger.debug(
"User %s has been given the Guest role as well as the "
"member role as they had not yet been inducted.",
interaction_member,
)

# noinspection PyUnusedLocal
applicant_role: discord.Role | None = None
Expand All @@ -275,3 +288,9 @@ async def make_member(self, ctx: TeXBotApplicationContext, group_member_id: str)
applicant_role,
reason="TeX Bot slash-command: \"/makemember\"",
)
logger.debug(
(
"Removed Applicant role from user %s after successful make-member command"
),
interaction_member,
)
5 changes: 5 additions & 0 deletions cogs/remind_me.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,11 @@ async def remind_me(self, ctx: TeXBotApplicationContext, delay: str, message: st
return

await ctx.respond("Reminder set!", ephemeral=True)
logger.debug(
"Reminder '%s' set for user %s",
f"{message[:15]}..." if len(message) > 15 else message,
ctx.interaction.user,
)

await discord.utils.sleep_until(reminder.send_datetime)

Expand Down
1 change: 1 addition & 0 deletions cogs/send_get_roles_reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ async def send_get_roles_reminders(self) -> None:
"and click on the icons to get optional roles like pronouns "
"and year group identifiers.",
)
logger.debug("Role reminder sent to %s", member)
except discord.Forbidden:
logger.info(
"Failed to open DM channel to user, %s, so no role reminder was sent.",
Expand Down
2 changes: 2 additions & 0 deletions cogs/send_introduction_reminders.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ async def send_introduction_reminders(self) -> None:
else None # type: ignore[arg-type]
),
)
logger.debug("Sent introduction reminder to %s", member)
except discord.Forbidden:
logger.info(
"Failed to open DM channel with user, %s, "
Expand Down Expand Up @@ -274,6 +275,7 @@ async def opt_out_introduction_reminders_button_callback(self, button: discord.B
await IntroductionReminderOptOutMember.objects.acreate(
discord_id=interaction_member.id,
)
logger.debug("Created opt out object for user %s", interaction_member)
except ValidationError as create_introduction_reminder_opt_out_member_error:
error_is_already_exists: bool = (
"hashed_member_id" in create_introduction_reminder_opt_out_member_error.message_dict # noqa: E501
Expand Down
Loading