Skip to content

Commit 829aaeb

Browse files
authored
De classes logger. Sets up framework to allow bot messages to be passed to logger (#1304)
* De classes logger. Sets up framework to allow bot messages to be passed to logger * Formatting * Formatting, security fixes * Formatting
1 parent ee04b9e commit 829aaeb

File tree

5 files changed

+345
-130
lines changed

5 files changed

+345
-130
lines changed

techsupport_bot/commands/echo.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
from core import auxiliary, cogs
1616
from discord.ext import commands
17+
from functions import logger as function_logger
1718

1819
if TYPE_CHECKING:
1920
import bot
@@ -72,10 +73,32 @@ async def echo_channel(
7273
)
7374
return
7475

75-
await channel.send(content=message)
76+
sent_message = await channel.send(content=message)
7677

7778
await auxiliary.send_confirm_embed(message="Message sent", channel=ctx.channel)
7879

80+
config = self.bot.guild_configs[str(channel.guild.id)]
81+
82+
# Don't allow logging if extension is disabled
83+
if "logger" not in config.enabled_extensions:
84+
return
85+
86+
target_logging_channel = await function_logger.pre_log_checks(
87+
self.bot, config, channel
88+
)
89+
if not target_logging_channel:
90+
return
91+
92+
await function_logger.send_message(
93+
self.bot,
94+
sent_message,
95+
ctx.author,
96+
channel,
97+
target_logging_channel,
98+
content_override=message,
99+
special_flags=["Echo command"],
100+
)
101+
79102
@auxiliary.with_typing
80103
@echo.command(
81104
name="user",

techsupport_bot/commands/factoids.py

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
from core import auxiliary, cogs, custom_errors, extensionconfig
3535
from discord import app_commands
3636
from discord.ext import commands
37+
from functions import logger as function_logger
3738

3839
if TYPE_CHECKING:
3940
import bot
@@ -873,7 +874,9 @@ async def response(
873874

874875
try:
875876
# define the message and send it
876-
await ctx.reply(content=content, embed=embed, mention_author=not mentions)
877+
sent_message = await ctx.reply(
878+
content=content, embed=embed, mention_author=not mentions
879+
)
877880
# log it in the logging channel with type info and generic content
878881
config = self.bot.guild_configs[str(ctx.guild.id)]
879882
log_channel = config.get("logging_channel")
@@ -898,12 +901,15 @@ async def response(
898901
exception=exception,
899902
)
900903
# Sends the raw factoid instead of the embed as fallback
901-
await ctx.reply(
904+
sent_message = await ctx.reply(
902905
f"{mentions+' ' if mentions else ''}{factoid.message}",
903906
mention_author=not mentions,
904907
)
905908

906909
await self.send_to_irc(ctx.channel, ctx.message, factoid.message)
910+
await self.send_to_logger(
911+
sent_message, ctx.author, ctx.channel, factoid.message
912+
)
907913

908914
async def send_to_irc(
909915
self: Self,
@@ -929,6 +935,44 @@ async def send_to_irc(
929935
factoid_message=factoid_message,
930936
)
931937

938+
async def send_to_logger(
939+
self: Self,
940+
factoid_message_object: discord.Message,
941+
factoid_caller: discord.Member,
942+
channel: discord.abc.GuildChannel | discord.Thread,
943+
factoid_message: str,
944+
) -> None:
945+
"""Send a factoid call to the logger function
946+
947+
Args:
948+
factoid_message_object (discord.Message): The message that the factoid is sent in
949+
factoid_caller (discord.Member): The person who called the factoid
950+
channel (discord.abc.GuildChannel | discord.Thread): The channel the
951+
factoid was sent in
952+
factoid_message (str): The plaintext message content of the factoid
953+
"""
954+
config = self.bot.guild_configs[str(channel.guild.id)]
955+
956+
# Don't allow logging if extension is disabled
957+
if "logger" not in config.enabled_extensions:
958+
return
959+
960+
target_logging_channel = await function_logger.pre_log_checks(
961+
self.bot, config, channel
962+
)
963+
if not target_logging_channel:
964+
return
965+
966+
await function_logger.send_message(
967+
self.bot,
968+
factoid_message_object,
969+
factoid_caller,
970+
channel,
971+
target_logging_channel,
972+
content_override=factoid_message,
973+
special_flags=["Factoid call"],
974+
)
975+
932976
@factoid_app_group.command(
933977
name="call",
934978
description="Calls a factoid from the database and sends it publicy in the channel.",
@@ -1054,6 +1098,11 @@ async def factoid_call_command(
10541098
interaction.channel, interaction.message, factoid.message
10551099
)
10561100

1101+
sent_message = await interaction.original_response()
1102+
await self.send_to_logger(
1103+
sent_message, interaction.user, interaction.channel, factoid.message
1104+
)
1105+
10571106
# -- Factoid job related functions --
10581107
async def kickoff_jobs(self: Self) -> None:
10591108
"""Gets a list of cron jobs and starts them"""
@@ -1230,6 +1279,7 @@ async def cronjob(
12301279
message = await channel.send(content=factoid.message)
12311280

12321281
await self.send_to_irc(channel, message, factoid.message)
1282+
await self.send_to_logger(message, ctx.author, ctx.channel, factoid.message)
12331283

12341284
@commands.group(
12351285
brief="Executes a factoid command",

techsupport_bot/commands/relay.py

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from core import auxiliary, cogs
1313
from discord.ext import commands
1414
from functions import automod
15+
from functions import logger as function_logger
1516

1617
if TYPE_CHECKING:
1718
import bot
@@ -447,7 +448,30 @@ async def send_message_from_irc(self: Self, split_message: dict[str, str]) -> No
447448

448449
embed = self.generate_sent_message_embed(split_message=split_message)
449450

450-
await discord_channel.send(content=mentions_string, embed=embed)
451+
sent_message = await discord_channel.send(content=mentions_string, embed=embed)
452+
453+
config = self.bot.guild_configs[str(discord_channel.guild.id)]
454+
# Don't allow logging if extension is disabled
455+
if "logger" not in config.enabled_extensions:
456+
return
457+
target_logging_channel = await function_logger.pre_log_checks(
458+
self.bot, config, discord_channel
459+
)
460+
if not target_logging_channel:
461+
return
462+
463+
irc_message_content = split_message["content"]
464+
irc_message_hostmask = split_message["hostmask"]
465+
466+
await function_logger.send_message(
467+
self.bot,
468+
sent_message,
469+
discord_channel.guild.me,
470+
discord_channel,
471+
target_logging_channel,
472+
content_override=irc_message_content,
473+
special_flags=[f"IRC Message from: {irc_message_hostmask}"],
474+
)
451475

452476
def get_mentions(
453477
self: Self, message: str, channel: discord.abc.Messageable
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""Functions are commandless cogs"""
22

33
from .automod import *
4+
from .logger import *
45
from .nickname import *

0 commit comments

Comments
 (0)