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

refactor(audit.py): rename tux_log_channel_id to dev_log_channel_id for better clarity

feat(audit.py): add docstrings to all methods to improve code readability and maintainability
fix(audit.py): change last_pin type from discord.Message to datetime.datetime in on_guild_channel_pins_update method to correctly represent the last pin time

docs(audit.py): add docstrings to on_stage_instance_update, on_thread_create, on_thread_delete, on_thread_update methods to improve code readability and understanding
refactor(audit.py): add explicit return type 'None' to on_stage_instance_update, on_thread_create, on_thread_delete, on_thread_update methods for better type hinting

refactor(logging): rename log channel from 'TUX' to 'DEV' for better clarity
feat(logging): add logging of command completions to 'DEV' channel for better tracking
feat(logging): add docstrings to event listeners for better code understanding
style(logging): remove unnecessary logger.info calls in member update event
refactor(logging): rename 'AUDIT' log channel to 'DEV' in GuildLogging for consistency
feat(logging): add logging of non-poll and poll message deletions to 'DEV' channel for better tracking
This commit is contained in:
kzndotsh 2024-05-01 06:19:48 +00:00
parent 644eeaae48
commit 47822f9844
5 changed files with 342 additions and 46 deletions

View file

@ -1,3 +1,4 @@
import datetime
from typing import Any
import discord
@ -14,22 +15,31 @@ class AuditLogging(commands.Cog):
self.bot = bot
self.db_controller = DatabaseController()
self.audit_log_channel_id: int = CONST.LOG_CHANNELS["AUDIT"]
self.tux_log_channel_id: int = CONST.LOG_CHANNELS["TUX"]
self.dev_log_channel_id: int = CONST.LOG_CHANNELS["DEV"]
async def send_to_audit_log(self, embed: discord.Embed) -> None:
channel = self.bot.get_channel(self.audit_log_channel_id)
if isinstance(channel, discord.TextChannel):
await channel.send(embed=embed)
async def send_to_tux_log(self, embed: discord.Embed) -> None:
channel = self.bot.get_channel(self.tux_log_channel_id)
async def send_to_dev_log(self, embed: discord.Embed) -> None:
channel = self.bot.get_channel(self.dev_log_channel_id)
if isinstance(channel, discord.TextChannel):
await channel.send(embed=embed)
"""Audit Logging - Channel"""
@commands.Cog.listener()
async def on_guild_channel_create(self, channel: discord.abc.GuildChannel):
async def on_guild_channel_create(self, channel: discord.abc.GuildChannel) -> None:
"""
Logs when a channel is created in a guild.
Parameters
----------
channel : discord.abc.GuildChannel
The channel that was created.
"""
audit_embed = EmbedCreator.create_log_embed(
title="Channel Created",
description=f"A new channel was created in {channel.guild.name}",
@ -42,7 +52,16 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(audit_embed)
@commands.Cog.listener()
async def on_guild_channel_delete(self, channel: discord.abc.GuildChannel):
async def on_guild_channel_delete(self, channel: discord.abc.GuildChannel) -> None:
"""
Logs when a channel is deleted in a guild.
Parameters
----------
channel : discord.abc.GuildChannel
The channel that was deleted.
"""
audit_embed = EmbedCreator.create_log_embed(
title="Channel Deleted",
description=f"A channel was deleted in {channel.guild.name}",
@ -57,7 +76,18 @@ class AuditLogging(commands.Cog):
@commands.Cog.listener()
async def on_guild_channel_update(
self, before: discord.abc.GuildChannel, after: discord.abc.GuildChannel
):
) -> None:
"""
Logs when a channel is updated in a guild.
Parameters
----------
before : discord.abc.GuildChannel
The channel before the update.
after : discord.abc.GuildChannel
The channel after the update.
"""
audit_embed = EmbedCreator.create_log_embed(
title="Channel Updated",
description=f"A channel was updated in {before.guild.name}",
@ -74,8 +104,19 @@ class AuditLogging(commands.Cog):
async def on_guild_channel_pins_update(
self,
channel: discord.abc.GuildChannel | discord.Thread | discord.abc.PrivateChannel,
last_pin: discord.Message | None,
):
last_pin: datetime.datetime | None,
) -> None:
"""
Logs when a message is pinned or unpinned in a channel.
Parameters
----------
channel : discord.abc.GuildChannel | discord.Thread | discord.abc.PrivateChannel
The channel in which the message was pinned/unpinned.
last_pin : datetime.datetime | None
The time of the last pin.
"""
if isinstance(channel, discord.abc.GuildChannel | discord.Thread):
channel_name = channel.mention
else:
@ -99,7 +140,18 @@ class AuditLogging(commands.Cog):
"""Audit logging - Guild"""
@commands.Cog.listener()
async def on_guild_update(self, before: discord.Guild, after: discord.Guild):
async def on_guild_update(self, before: discord.Guild, after: discord.Guild) -> None:
"""
Logs when a guild is updated.
Parameters
----------
before : discord.Guild
The guild before the update.
after : discord.Guild
The guild after the update.
"""
before_attrs: dict[str, Any] = extract_guild_attrs(before)
after_attrs: dict[str, Any] = extract_guild_attrs(after)
@ -115,7 +167,20 @@ class AuditLogging(commands.Cog):
@commands.Cog.listener()
async def on_guild_emojis_update(
self, guild: discord.Guild, before: list[discord.Emoji], after: list[discord.Emoji]
):
) -> None:
"""
Logs when emojis are updated in a guild.
Parameters
----------
guild : discord.Guild
The guild where the emojis were updated.
before : list[discord.Emoji]
The list of emojis before the update.
after : list[discord.Emoji]
The list of emojis after the update.
"""
embed = EmbedCreator.create_log_embed(
title="Guild Emojis Updated",
description=f"Emojis: {len(before)} -> {len(after)}",
@ -140,7 +205,20 @@ class AuditLogging(commands.Cog):
@commands.Cog.listener()
async def on_guild_stickers_update(
self, guild: discord.Guild, before: list[discord.Sticker], after: list[discord.Sticker]
):
) -> None:
"""
Logs when stickers are updated in a guild.
Parameters
----------
guild : discord.Guild
The guild where the stickers were updated.
before : list[discord.Sticker]
The list of stickers before the update.
after : list[discord.Sticker]
The list of stickers after the update.
"""
embed = discord.Embed(
title="Guild Stickers Updated",
description=f"Stickers: {len(before)} -> {len(after)}",
@ -166,7 +244,16 @@ class AuditLogging(commands.Cog):
"""Audit Logging - Integrations"""
@commands.Cog.listener()
async def on_integration_create(self, integration: discord.Integration):
async def on_integration_create(self, integration: discord.Integration) -> None:
"""
Logs when an integration is created.
Parameters
----------
integration : discord.Integration
The integration that was created.
"""
embed = EmbedCreator.create_log_embed(
title="Integration Created",
description=f"Integration: {integration.name}",
@ -175,7 +262,16 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_integration_update(self, integration: discord.Integration):
async def on_integration_update(self, integration: discord.Integration) -> None:
"""
Logs when an integration is updated.
Parameters
----------
integration : discord.Integration
The integration that was updated.
"""
embed = EmbedCreator.create_log_embed(
title="Integration Updated",
description=f"Integration: {integration.name}",
@ -184,7 +280,16 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_integration_delete(self, integration: discord.Integration):
async def on_integration_delete(self, integration: discord.Integration) -> None:
"""
Logs when an integration is deleted.
Parameters
----------
integration : discord.Integration
The integration that was deleted.
"""
embed = EmbedCreator.create_log_embed(
title="Integration Deleted",
description=f"Integration: {integration.name}",
@ -193,7 +298,16 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_webhooks_update(self, channel: discord.TextChannel):
async def on_webhooks_update(self, channel: discord.TextChannel) -> None:
"""
Logs when webhooks are updated in a channel.
Parameters
----------
channel : discord.TextChannel
The channel where the webhooks were updated.
"""
embed = EmbedCreator.create_log_embed(
title="Webhooks Updated",
description=f"Channel: {channel.name}",
@ -204,7 +318,16 @@ class AuditLogging(commands.Cog):
"""Audit Logging - Role"""
@commands.Cog.listener()
async def on_guild_role_create(self, role: discord.Role):
async def on_guild_role_create(self, role: discord.Role) -> None:
"""
Logs when a role is created in a guild.
Parameters
----------
role : discord.Role
The role that was created.
"""
embed = EmbedCreator.create_log_embed(
title="Role Created",
description=f"Role: {role.name}",
@ -213,7 +336,16 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_guild_role_delete(self, role: discord.Role):
async def on_guild_role_delete(self, role: discord.Role) -> None:
"""
Logs when a role is deleted in a guild.
Parameters
----------
role : discord.Role
The role that was deleted.
"""
embed = EmbedCreator.create_log_embed(
title="Role Deleted",
description=f"Role: {role.name}",
@ -222,7 +354,18 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_guild_role_update(self, before: discord.Role, after: discord.Role):
async def on_guild_role_update(self, before: discord.Role, after: discord.Role) -> None:
"""
Logs when a role is updated in a guild.
Parameters
----------
before : discord.Role
The role before the update.
after : discord.Role
The role after the update.
"""
embed = EmbedCreator.create_log_embed(
title="Role Updated",
description=f"Role: {before.name}",
@ -233,7 +376,16 @@ class AuditLogging(commands.Cog):
"""Audit Logging - Scheduled Events"""
@commands.Cog.listener()
async def on_scheduled_event_create(self, event: discord.ScheduledEvent):
async def on_scheduled_event_create(self, event: discord.ScheduledEvent) -> None:
"""
Logs when a scheduled event is created.
Parameters
----------
event : discord.ScheduledEvent
The scheduled event that was created.
"""
embed = EmbedCreator.create_log_embed(
title="Scheduled Event Created",
description=f"Event: {event.name}",
@ -242,7 +394,16 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_scheduled_event_delete(self, event: discord.ScheduledEvent):
async def on_scheduled_event_delete(self, event: discord.ScheduledEvent) -> None:
"""
Logs when a scheduled event is deleted.
Parameters
----------
event : discord.ScheduledEvent
The scheduled event that was deleted.
"""
embed = EmbedCreator.create_log_embed(
title="Scheduled Event Deleted",
description=f"Event: {event.name}",
@ -253,7 +414,18 @@ class AuditLogging(commands.Cog):
@commands.Cog.listener()
async def on_scheduled_event_update(
self, before: discord.ScheduledEvent, after: discord.ScheduledEvent
):
) -> None:
"""
Logs when a scheduled event is updated.
Parameters
----------
before : discord.ScheduledEvent
The scheduled event before the update.
after : discord.ScheduledEvent
The scheduled event after the update.
"""
embed = EmbedCreator.create_log_embed(
title="Scheduled Event Updated",
description=f"Event: {before.name}",
@ -264,7 +436,16 @@ class AuditLogging(commands.Cog):
"""Audit Logging - Stage Instance"""
@commands.Cog.listener()
async def on_stage_instance_create(self, stage_instance: discord.StageInstance):
async def on_stage_instance_create(self, stage_instance: discord.StageInstance) -> None:
"""
Logs when a stage instance is created.
Parameters
----------
stage_instance : discord.StageInstance
The stage instance that was created.
"""
embed = EmbedCreator.create_log_embed(
title="Stage Instance Created",
description=f"Stage: {stage_instance.topic}",
@ -273,7 +454,16 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_stage_instance_delete(self, stage_instance: discord.StageInstance):
async def on_stage_instance_delete(self, stage_instance: discord.StageInstance) -> None:
"""
Logs when a stage instance is deleted.
Parameters
----------
stage_instance : discord.StageInstance
The stage instance that was deleted.
"""
embed = EmbedCreator.create_log_embed(
title="Stage Instance Deleted",
description=f"Stage: {stage_instance.topic}",
@ -284,7 +474,18 @@ class AuditLogging(commands.Cog):
@commands.Cog.listener()
async def on_stage_instance_update(
self, before: discord.StageInstance, after: discord.StageInstance
):
) -> None:
"""
Logs when a stage instance is updated.
Parameters
----------
before : discord.StageInstance
The stage instance before the update.
after : discord.StageInstance
The stage instance after the update.
"""
embed = EmbedCreator.create_log_embed(
title="Stage Instance Updated",
description=f"Stage: {before.topic}",
@ -295,7 +496,16 @@ class AuditLogging(commands.Cog):
"""Audit Logging - Thread"""
@commands.Cog.listener()
async def on_thread_create(self, thread: discord.Thread):
async def on_thread_create(self, thread: discord.Thread) -> None:
"""
Logs when a thread is created.
Parameters
----------
thread : discord.Thread
The thread that was created.
"""
embed = EmbedCreator.create_log_embed(
title="Thread Created",
description=f"Thread: {thread.name}",
@ -304,7 +514,17 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_thread_delete(self, thread: discord.Thread):
async def on_thread_delete(self, thread: discord.Thread) -> None:
"""
Logs when a thread is deleted.
Parameters
----------
thread : discord.Thread
The thread that was deleted.
"""
embed = EmbedCreator.create_log_embed(
title="Thread Deleted",
description=f"Thread: {thread.name}",
@ -313,7 +533,18 @@ class AuditLogging(commands.Cog):
await self.send_to_audit_log(embed)
@commands.Cog.listener()
async def on_thread_update(self, before: discord.Thread, after: discord.Thread):
async def on_thread_update(self, before: discord.Thread, after: discord.Thread) -> None:
"""
Logs when a thread is updated.
Parameters
----------
before : discord.Thread
The thread before the update.
after : discord.Thread
The thread after the update.
"""
embed = EmbedCreator.create_log_embed(
title="Thread Updated",
description=f"Thread: {before.name}",

View file

@ -2,23 +2,45 @@ import discord
from discord.ext import commands
from loguru import logger
from tux.utils.constants import Constants as CONST
from tux.utils.embeds import EmbedCreator
class CommandEventsCog(commands.Cog, name="Command Events Handler"):
def __init__(self, bot: commands.Bot) -> None:
self.bot = bot
self.dev_log_channel_id: int = CONST.LOG_CHANNELS["DEV"]
@commands.Cog.listener()
async def on_app_command_completion(
self, interaction: discord.Interaction, command: discord.app_commands.AppCommand
) -> None:
logger.info(
f"'{command.name}' command was used by {interaction.user} in {interaction.channel}."
)
logger.info(f"Command {command.name} completed by {interaction.user}")
dev_log_channel = self.bot.get_channel(self.dev_log_channel_id)
if isinstance(dev_log_channel, discord.TextChannel):
embed = EmbedCreator.create_log_embed(
title="Command Completed",
description=f"Command `{command.name}` completed by {interaction.user.mention}.",
)
await dev_log_channel.send(embed=embed)
@commands.Cog.listener()
async def on_command_completion(self, ctx: commands.Context[commands.Bot]) -> None:
if ctx.command is not None:
logger.info(f"'{ctx.command.name}' command was used by {ctx.author} in {ctx.channel}.")
logger.info(f"Command {ctx.command.name} completed by {ctx.author}")
dev_log_channel = self.bot.get_channel(self.dev_log_channel_id)
if isinstance(dev_log_channel, discord.TextChannel):
embed = EmbedCreator.create_log_embed(
title="Command Completed",
description=f"Command `{ctx.command.name}` completed by {ctx.author.mention}.",
)
await dev_log_channel.send(embed=embed)
async def setup(bot: commands.Bot) -> None:

View file

@ -12,20 +12,29 @@ class GateLogging(commands.Cog):
self.bot = bot
self.db_controller = DatabaseController()
self.gate_log_channel_id: int = CONST.LOG_CHANNELS["GATE"]
self.tux_log_channel_id: int = CONST.LOG_CHANNELS["TUX"]
self.dev_log_channel_id: int = CONST.LOG_CHANNELS["DEV"]
async def send_to_gate_log(self, embed: discord.Embed) -> None:
channel = self.bot.get_channel(self.gate_log_channel_id)
if isinstance(channel, discord.TextChannel):
await channel.send(embed=embed)
async def send_to_tux_log(self, embed: discord.Embed) -> None:
channel = self.bot.get_channel(self.tux_log_channel_id)
async def send_to_dev_log(self, embed: discord.Embed) -> None:
channel = self.bot.get_channel(self.dev_log_channel_id)
if isinstance(channel, discord.TextChannel):
await channel.send(embed=embed)
@commands.Cog.listener()
async def on_member_join(self, member: discord.Member) -> None:
"""
When a member joins the server
Parameters
----------
member : discord.Member
The member that joined the server.
"""
gate_embed = EmbedCreator.create_log_embed(
title="Member Joined", description=f"Welcome {member.mention}!"
)
@ -59,10 +68,19 @@ class GateLogging(commands.Cog):
else f"User {member.mention} synced to database.",
)
await self.send_to_tux_log(log_embed)
await self.send_to_dev_log(log_embed)
@commands.Cog.listener()
async def on_member_remove(self, member: discord.Member) -> None:
"""
When a member leaves the server
Parameters
----------
member : discord.Member
The member that left the server.
"""
embed = EmbedCreator.create_log_embed(
title="Member Left",
description=f"Goodbye {member.mention}!",
@ -85,6 +103,15 @@ class GateLogging(commands.Cog):
@commands.Cog.listener()
async def on_invite_create(self, invite: discord.Invite) -> None:
"""
When an invite is created
Parameters
----------
invite : discord.Invite
The invite that was created.
"""
if invite.expires_at is not None:
expires_at = datetime_to_unix(invite.expires_at)
else:

View file

@ -1,6 +1,5 @@
import discord
from discord.ext import commands
from loguru import logger
from tux.database.controllers import DatabaseController
from tux.utils.constants import Constants as CONST
@ -20,7 +19,18 @@ class MemberLogging(commands.Cog):
await channel.send(embed=embed)
@commands.Cog.listener()
async def on_member_update(self, before: discord.Member, after: discord.Member):
async def on_member_update(self, before: discord.Member, after: discord.Member) -> None:
"""
When a member is updated
Parameters
----------
before : discord.Member
The member before the update.
after : discord.Member
The member after the update.
"""
embed = EmbedCreator.create_log_embed(
title="Member Updated",
description=f"Member {before.mention} has been updated.",
@ -28,19 +38,16 @@ class MemberLogging(commands.Cog):
if before.name != after.name:
embed.add_field(name="Name", value=f"`{before.name}` -> `{after.name}`")
logger.info(f"{before} -> {after}")
if before.display_name != after.display_name:
embed.add_field(
name="Display Name", value=f"`{before.display_name}` -> `{after.display_name}`"
)
logger.info(f"{before} -> {after}")
if before.global_name != after.global_name:
embed.add_field(
name="Global Name", value=f"`{before.global_name}` -> `{after.global_name}`"
)
logger.info(f"{before} -> {after}")
await self.send_to_audit_log(embed)

View file

@ -8,15 +8,24 @@ from tux.utils.embeds import EmbedCreator
class GuildLogging(commands.Cog):
def __init__(self, bot: commands.Bot):
self.bot = bot
self.audit_log_channel_id: int = CONST.LOG_CHANNELS["AUDIT"]
self.dev_logs_channel_id: int = CONST.LOG_CHANNELS["DEV"]
async def send_to_audit_log(self, embed: discord.Embed):
channel = self.bot.get_channel(self.audit_log_channel_id)
async def send_to_dev_log(self, embed: discord.Embed):
channel = self.bot.get_channel(self.dev_logs_channel_id)
if isinstance(channel, discord.TextChannel):
await channel.send(embed=embed)
@commands.Cog.listener()
async def on_message(self, message: discord.Message):
async def on_message(self, message: discord.Message) -> None:
"""
When a message is sent in a guild
Parameters
----------
message : discord.Message
The message that was sent.
"""
# check if the message has no embeds, attachments, or content, stickers, or isnt a nitro gift/boost
# if so its probably a poll
poll_channel = self.bot.get_channel(1228717294788673656)
@ -29,7 +38,7 @@ class GuildLogging(commands.Cog):
title="Non-Poll Deleted",
description=f"Message: {message.id}",
)
await self.send_to_audit_log(embed)
await self.send_to_dev_log(embed)
return
# make a thread for the poll
@ -54,7 +63,7 @@ class GuildLogging(commands.Cog):
title="Poll Deleted",
description=f"Message: {message.id}",
)
await self.send_to_audit_log(embed)
await self.send_to_dev_log(embed)
async def setup(bot: commands.Bot) -> None: