import logging from logging import Logger from typing import Dict, Any from discord import Member, Message, TextChannel, MessageType from discord import utils as ds_utils from discord.ext import commands from libbot.utils import config_get from classes.holo_bot import HoloBot from modules.database import col_users logger: Logger = logging.getLogger(__name__) class Logger(commands.Cog): def __init__(self, client: HoloBot): self.client: HoloBot = client @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) and (message.author.bot is False) and (message.author.system is False) ): if (await col_users.find_one({"user": message.author.id})) is None: user: Dict[str, Any] = {} defaults: Dict[str, Any] = await config_get("user", "defaults") user["user"] = message.author.id for key in defaults: user[key] = defaults[key] await col_users.insert_one(document=user) if ( (message.type == MessageType.thread_created) and (message.channel is not None) and ( await col_users.count_documents({"customchannel": message.channel.id}) > 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, ) @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 welcome_chan is None: logger.warning("Could not find a welcome channel by its id") if ( (member != self.client.user) and (member.bot is False) and (member.system is False) ): await welcome_chan.send( content=(await config_get("welcome", "messages")).format( mention=member.mention, rules=rules_chan.mention ) ) if (await col_users.find_one({"user": member.id})) is None: user: Dict[str, Any] = {} defaults: Dict[str, Any] = await config_get("user", "defaults") user["user"] = member.id for key in defaults: user[key] = defaults[key] await col_users.insert_one(document=user) def setup(client: HoloBot) -> None: client.add_cog(Logger(client))