Discord/cogs/logger.py

82 lines
2.8 KiB
Python
Raw Normal View History

2024-12-27 20:16:30 +01:00
import logging
from logging import Logger
2024-12-27 20:16:30 +01:00
from discord import Member, Message, TextChannel, MessageType
2023-05-06 17:08:35 +02:00
from discord import utils as ds_utils
from discord.ext import commands
2024-12-26 19:12:50 +01:00
from libbot.utils import config_get
2023-05-06 17:08:35 +02:00
2024-12-17 22:14:06 +01:00
from classes.holo_bot import HoloBot
2025-02-09 23:00:18 +01:00
from classes.holo_user import HoloUser
2023-05-06 17:08:35 +02:00
from modules.database import col_users
logger: Logger = logging.getLogger(__name__)
2024-12-27 20:16:30 +01:00
2023-05-06 17:08:35 +02:00
class Logger(commands.Cog):
2024-12-17 22:14:06 +01:00
def __init__(self, client: HoloBot):
self.client: HoloBot = client
2023-05-06 17:08:35 +02:00
@commands.Cog.listener()
async def on_message(self, message: Message):
"""Message listener. All actions on messages remain here for now."""
if (
(message.author != self.client.user)
2024-12-15 23:21:41 +01:00
and (message.author.bot is False)
and (message.author.system is False)
):
2025-02-09 23:00:18 +01:00
await HoloUser.from_user(message.author, cache=self.client.cache)
2023-05-06 17:08:35 +02:00
2024-12-27 20:16:30 +01:00
if (
(message.type == MessageType.thread_created)
and (message.channel is not None)
and (
2024-12-27 22:43:40 +01:00
await col_users.count_documents({"custom_channel": message.channel.id})
2024-12-27 20:16:30 +01:00
> 0
)
):
try:
logger.info(
"Deleting the thread creation message in a custom channel %s",
message.channel.id,
)
await message.delete()
except Exception as exc:
logger.warning(
"Could not delete the thread creation message in a custom channel %s due to %s",
message.channel.id,
exc,
)
2024-12-27 20:16:30 +01:00
2023-05-06 17:08:35 +02:00
@commands.Cog.listener()
async def on_member_join(self, member: Member) -> None:
"""Member join handler. All actions on member join remain here for now."""
welcome_chan: TextChannel | None = ds_utils.get(
self.client.get_guild(await config_get("guild")).channels,
id=await config_get("welcome", "channels", "text"),
)
rules_chan: TextChannel | None = ds_utils.get(
self.client.get_guild(await config_get("guild")).channels,
id=await config_get("rules", "channels", "text"),
)
if (
(member != self.client.user)
2024-12-15 23:21:41 +01:00
and (member.bot is False)
and (member.system is False)
):
2025-02-09 23:00:18 +01:00
if welcome_chan is not None and rules_chan is not None:
await welcome_chan.send(
content=(await config_get("welcome", "messages")).format(
mention=member.mention, rules=rules_chan.mention
)
)
2025-02-09 23:00:18 +01:00
else:
logger.warning("Could not find a welcome and/or rules channel by id")
2023-05-06 18:31:23 +02:00
2025-02-09 23:00:18 +01:00
await HoloUser.from_user(member, cache=self.client.cache)
2024-06-23 12:05:03 +02:00
2024-12-17 22:14:06 +01:00
def setup(client: HoloBot) -> None:
2024-06-23 12:05:03 +02:00
client.add_cog(Logger(client))