From 32c7ec7d447b8e38847413108ac8a53fc3f890ab Mon Sep 17 00:00:00 2001 From: profitroll Date: Sat, 6 May 2023 17:08:35 +0200 Subject: [PATCH] Added logger thats adds users to DB --- cogs/logger.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ main.py | 2 ++ 2 files changed, 51 insertions(+) create mode 100644 cogs/logger.py diff --git a/cogs/logger.py b/cogs/logger.py new file mode 100644 index 0000000..6a427cd --- /dev/null +++ b/cogs/logger.py @@ -0,0 +1,49 @@ +from discord import Member, Message +from discord import utils as ds_utils +from discord.ext import commands + +from modules.database import col_users +from modules.utils import config_get + + +class Logger(commands.Cog): + + def __init__(self, client): + self.client = client + + @commands.Cog.listener() + async def on_message(self, message: Message): + + if (message.author != self.client.user) and (message.author.bot == False) and (message.author.system == False): + + if col_users.find_one({"user": message.author.id}) is None: + user = {} + defaults = await config_get("user", "defaults") + + user["user"] = message.author.id + + for key in defaults: + user[key] = defaults[key] + + col_users.insert_one(document=user) + + @commands.Cog.listener() + async def on_member_join(self, member: Member): + + welcome_chan = ds_utils.get(self.client.get_guild(await config_get("guild")).channels, id=await config_get("logging", "channels", "text")) + rules_chan = ds_utils.get(self.client.get_guild(await config_get("guild")).channels, id=await config_get("rules", "channels", "text")) + + if (member != self.client.user) and (member.bot == False) and (member.system == False): + + await welcome_chan.send(content=(await config_get("welcome", "messages")).format(mention=member.mention, rules=rules_chan.mention)) + + if col_users.find_one({"user": member.id}) is None: + col_users.insert_one(document=user) + + user = {} + defaults = await config_get("user", "defaults") + + user["user"] = member.id + + for key in defaults: + user[key] = defaults[key] \ No newline at end of file diff --git a/main.py b/main.py index 9fb86db..e279106 100644 --- a/main.py +++ b/main.py @@ -9,6 +9,7 @@ from modules.utils import config_get from modules.utils_sync import config_get_sync from cogs.custom_channels import CustomChannels +from cogs.logger import Logger logging.basicConfig( level=logging.INFO, @@ -36,6 +37,7 @@ async def on_ready(): def main(): client.add_cog(CustomChannels(client)) + client.add_cog(Logger(client)) try: scheduler.start()