Skip to content

De classes logger. Sets up framework to allow bot messages to be passed to logger #1304

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
25 changes: 24 additions & 1 deletion techsupport_bot/commands/echo.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from core import auxiliary, cogs
from discord.ext import commands
from functions import logger as function_logger

if TYPE_CHECKING:
import bot
Expand Down Expand Up @@ -72,10 +73,32 @@ async def echo_channel(
)
return

await channel.send(content=message)
sent_message = await channel.send(content=message)

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

config = self.bot.guild_configs[str(channel.guild.id)]

# Don't allow logging if extension is disabled
if "logger" not in config.enabled_extensions:
return

target_logging_channel = await function_logger.pre_log_checks(
self.bot, config, channel
)
if not target_logging_channel:
return

await function_logger.send_message(
self.bot,
sent_message,
ctx.author,
channel,
target_logging_channel,
content_override=message,
special_flags=["Echo command"],
)

@auxiliary.with_typing
@echo.command(
name="user",
Expand Down
54 changes: 52 additions & 2 deletions techsupport_bot/commands/factoids.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from croniter import CroniterBadCronError
from discord import app_commands
from discord.ext import commands
from functions import logger as function_logger

if TYPE_CHECKING:
import bot
Expand Down Expand Up @@ -851,7 +852,9 @@ async def response(

try:
# define the message and send it
await ctx.reply(content=content, embed=embed, mention_author=not mentions)
sent_message = await ctx.reply(
content=content, embed=embed, mention_author=not mentions
)
# log it in the logging channel with type info and generic content
config = self.bot.guild_configs[str(ctx.guild.id)]
log_channel = config.get("logging_channel")
Expand All @@ -876,12 +879,15 @@ async def response(
exception=exception,
)
# Sends the raw factoid instead of the embed as fallback
await ctx.reply(
sent_message = await ctx.reply(
f"{mentions+' ' if mentions else ''}{factoid.message}",
mention_author=not mentions,
)

await self.send_to_irc(ctx.channel, ctx.message, factoid.message)
await self.send_to_logger(
sent_message, ctx.author, ctx.channel, factoid.message
)

async def send_to_irc(
self: Self,
Expand All @@ -907,6 +913,44 @@ async def send_to_irc(
factoid_message=factoid_message,
)

async def send_to_logger(
self: Self,
factoid_message_object: discord.Message,
factoid_caller: discord.Member,
channel: discord.abc.GuildChannel | discord.Thread,
factoid_message: str,
) -> None:
"""Send a factoid call to the logger function

Args:
factoid_message_object (discord.Message): The message that the factoid is sent in
factoid_caller (discord.Member): The person who called the factoid
channel (discord.abc.GuildChannel | discord.Thread): The channel the
factoid was sent in
factoid_message (str): The plaintext message content of the factoid
"""
config = self.bot.guild_configs[str(channel.guild.id)]

# Don't allow logging if extension is disabled
if "logger" not in config.enabled_extensions:
return

target_logging_channel = await function_logger.pre_log_checks(
self.bot, config, channel
)
if not target_logging_channel:
return

await function_logger.send_message(
self.bot,
factoid_message_object,
factoid_caller,
channel,
target_logging_channel,
content_override=factoid_message,
special_flags=["Factoid call"],
)

@factoid_app_group.command(
name="call",
description="Calls a factoid from the database and sends it publicy in the channel.",
Expand Down Expand Up @@ -1019,6 +1063,11 @@ async def factoid_call_command(
interaction.channel, interaction.message, factoid.message
)

sent_message = await interaction.original_response()
await self.send_to_logger(
sent_message, interaction.user, interaction.channel, factoid.message
)

# -- Factoid job related functions --
async def kickoff_jobs(self: Self) -> None:
"""Gets a list of cron jobs and starts them"""
Expand Down Expand Up @@ -1182,6 +1231,7 @@ async def cronjob(
message = await channel.send(content=factoid.message)

await self.send_to_irc(channel, message, factoid.message)
await self.send_to_logger(message, ctx.author, ctx.channel, factoid.message)

@commands.group(
brief="Executes a factoid command",
Expand Down
26 changes: 25 additions & 1 deletion techsupport_bot/commands/relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from bidict import bidict
from core import auxiliary, cogs
from discord.ext import commands
from functions import logger as function_logger

if TYPE_CHECKING:
import bot
Expand Down Expand Up @@ -415,7 +416,30 @@ async def send_message_from_irc(self: Self, split_message: dict[str, str]) -> No

embed = self.generate_sent_message_embed(split_message=split_message)

await discord_channel.send(content=mentions_string, embed=embed)
sent_message = await discord_channel.send(content=mentions_string, embed=embed)

config = self.bot.guild_configs[str(discord_channel.guild.id)]
# Don't allow logging if extension is disabled
if "logger" not in config.enabled_extensions:
return
target_logging_channel = await function_logger.pre_log_checks(
self.bot, config, discord_channel
)
if not target_logging_channel:
return

irc_message_content = split_message["content"]
irc_message_hostmask = split_message["hostmask"]

await function_logger.send_message(
self.bot,
sent_message,
discord_channel.guild.me,
discord_channel,
target_logging_channel,
content_override=irc_message_content,
special_flags=[f"IRC Message from: {irc_message_hostmask}"],
)

def get_mentions(
self: Self, message: str, channel: discord.abc.Messageable
Expand Down
1 change: 1 addition & 0 deletions techsupport_bot/functions/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
"""Functions are commandless cogs"""

from .logger import *
from .nickname import *
Loading