Skip to content

Commit

Permalink
🤑 Try working on logging system
Browse files Browse the repository at this point in the history
  • Loading branch information
tookender committed May 18, 2024
1 parent 1951ead commit 214c6dc
Show file tree
Hide file tree
Showing 16 changed files with 2,861 additions and 6 deletions.
95 changes: 93 additions & 2 deletions bot.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
import typing
import logging
import pathlib
from typing import List
from typing import List, Type
from collections import defaultdict

import aiohttp
import asyncpg
import discord
import mystbin as mystbin_library
from discord.ext import commands
from utils.utils import col
from utils.logging import LoggingEventsFlags

import config


class LoggingConfig:
__slots__ = ("default", "message", "member", "join_leave", "voice", "server")

def __init__(self, default, message, member, join_leave, voice, server):
self.default = default
self.message = message
self.member = member
self.join_leave = join_leave
self.voice = voice
self.server = server

def _replace(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)


class Korii(commands.AutoShardedBot):
pool: asyncpg.Pool
user: discord.ClientUser
Expand Down Expand Up @@ -43,6 +63,19 @@ def __init__(self) -> None:
self.ping_cooldown: commands.CooldownMapping = commands.CooldownMapping.from_cooldown(1, 5, commands.BucketType.user)
self.levelling_cooldown: commands.CooldownMapping = commands.CooldownMapping.from_cooldown(1, 45, commands.BucketType.member)

self.log_webhooks: Type[LoggingConfig] = LoggingConfig
self.log_channels: typing.Dict[int, LoggingConfig] = {}
self.log_cache = defaultdict(lambda: defaultdict(list))
self.guild_loggings: typing.Dict[int, LoggingEventsFlags] = {}

def tick(self, boolean: bool | None):
if boolean == True:
return self.E["yes"]
elif boolean == False:
return self.E["no"]
else:
return self.E["hyphen"]

def fill(self, name: str, variable: list):
file = open(name, encoding="utf-8")
for line in file.readlines():
Expand Down Expand Up @@ -71,6 +104,32 @@ def bot_code(self):
if "#" in line:
self.comments += 1

def update_log(self, deliver_type: str, webhook_url: str, guild_id: int):
guild_id = getattr(guild_id, "id", guild_id)
if deliver_type == "default":
self.log_channels[guild_id]._replace(default=webhook_url)
elif deliver_type == "message":
self.log_channels[guild_id]._replace(message=webhook_url)
elif deliver_type == "member":
self.log_channels[guild_id]._replace(member=webhook_url)
elif deliver_type == "join_leave":
self.log_channels[guild_id]._replace(join_leave=webhook_url)
elif deliver_type == "voice":
self.log_channels[guild_id]._replace(voice=webhook_url)
elif deliver_type == "server":
self.log_channels[guild_id]._replace(server=webhook_url)

async def load_emojis(self) -> None:
await self.wait_until_ready()
EMOJI_GUILDS = [1036756543917527161, 1040293187354361857]

for guild_id in EMOJI_GUILDS:
guild = self.get_guild(guild_id)
assert guild
emojis = guild.emojis
for emoji in emojis:
self.E[f"{emoji.name}"] = f"<:ghost:{emoji.id}>"

async def load_extensions(self) -> None:
success = 0
failed = 0
Expand Down Expand Up @@ -103,7 +162,7 @@ async def load_extensions(self) -> None:
return self.ext_logger.info(f"Loaded {success} out of {success + failed} extensions")

async def setup_hook(self) -> None:
self.pool = await asyncpg.create_pool(config.DATABASE) # type: ignore
self.pool = await asyncpg.create_pool(config.DATABASE) # type: ignore

if not self.pool:
raise RuntimeError("Failed to connect with the database.")
Expand All @@ -112,6 +171,7 @@ async def setup_hook(self) -> None:
await self.pool.execute(file.read())

self.bot_code()
await self.load_emojis()
await self.load_extensions()

async def start(self) -> None:
Expand All @@ -134,3 +194,34 @@ async def get_prefix(self, message: discord.Message, /) -> List[str] | str:
prefixes.append("")

return commands.when_mentioned_or(*prefixes)(self, message)

async def populate_cache(self):
for entry in await self.pool.fetch("SELECT * FROM log_channels"):
guild_id = entry["guild_id"]
await self.pool.execute(
"INSERT INTO logging_events(guild_id) VALUES ($1) ON CONFLICT (guild_id) DO NOTHING",
entry["guild_id"],
)

self.log_channels[guild_id] = LoggingConfig(
default=entry["default_channel"],
message=entry["message_channel"],
join_leave=entry["join_leave_channel"],
member=entry["member_channel"],
voice=entry["voice_channel"],
server=entry["server_channel"],
)

flags = dict(
await self.pool.fetchrow(
"SELECT message_delete, message_purge, message_edit, member_join, member_leave, member_update, user_ban, user_unban, "
"user_update, invite_create, invite_delete, voice_join, voice_leave, voice_move, voice_mod, emoji_create, emoji_delete, "
"emoji_update, sticker_create, sticker_delete, sticker_update, server_update, stage_open, stage_close, channel_create, "
"channel_delete, channel_edit, role_create, role_delete, role_edit FROM logging_events WHERE guild_id = $1",
guild_id,
)
)
self.guild_loggings[guild_id] = LoggingEventsFlags(**flags)

logging.info(f"{col(2)}All cache populated successfully")
self.dispatch("cache_ready")
55 changes: 51 additions & 4 deletions data/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,54 @@ CREATE TABLE IF NOT EXISTS guilds (
levelling_delete_after INT DEFAULT NULL
);

CREATE TABLE IF NOT EXISTS avatars (
user_id BIGINT PRIMARY KEY,
avatar_url TEXT NOT NULL
)
CREATE TABLE IF NOT EXISTS log_channels (
guild_id BIGINT PRIMARY KEY,
default_channel TEXT,
default_chid BIGINT NOT NULL,
message_channel TEXT,
message_chid BIGINT,
join_leave_channel TEXT,
join_leave_chid BIGINT,
member_channel TEXT,
member_chid BIGINT,
voice_channel TEXT,
voice_chid BIGINT,
server_channel TEXT,
server_chid BIGINT,
CONSTRAINT fk_log_channels_guild_id FOREIGN KEY (guild_id) REFERENCES guilds(guild_id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS logging_events (
guild_id BIGINT NOT PRIMARY KEY,
message_delete BOOLEAN DEFAULT true NOT NULL,
message_purge BOOLEAN DEFAULT true NOT NULL,
message_edit BOOLEAN DEFAULT true NOT NULL,
member_join BOOLEAN DEFAULT true NOT NULL,
member_leave BOOLEAN DEFAULT true NOT NULL,
member_update BOOLEAN DEFAULT true NOT NULL,
user_ban BOOLEAN DEFAULT true NOT NULL,
user_unban BOOLEAN DEFAULT true NOT NULL,
user_update BOOLEAN DEFAULT true NOT NULL,
invite_create BOOLEAN DEFAULT true NOT NULL,
invite_delete BOOLEAN DEFAULT true NOT NULL,
voice_join BOOLEAN DEFAULT true NOT NULL,
voice_leave BOOLEAN DEFAULT true NOT NULL,
voice_move BOOLEAN DEFAULT true NOT NULL,
voice_mod BOOLEAN DEFAULT true NOT NULL,
emoji_create BOOLEAN DEFAULT true NOT NULL,
emoji_delete BOOLEAN DEFAULT true NOT NULL,
emoji_update BOOLEAN DEFAULT true NOT NULL,
sticker_create BOOLEAN DEFAULT true NOT NULL,
sticker_delete BOOLEAN DEFAULT true NOT NULL,
sticker_update BOOLEAN DEFAULT true NOT NULL,
server_update BOOLEAN DEFAULT true NOT NULL,
stage_open BOOLEAN DEFAULT true NOT NULL,
stage_close BOOLEAN DEFAULT true NOT NULL,
channel_create BOOLEAN DEFAULT true NOT NULL,
channel_delete BOOLEAN DEFAULT true NOT NULL,
channel_edit BOOLEAN DEFAULT true NOT NULL,
role_create BOOLEAN DEFAULT true NOT NULL,
role_delete BOOLEAN DEFAULT true NOT NULL,
role_edit BOOLEAN DEFAULT true NOT NULL,
CONSTRAINT fk_logging_events_guild_id FOREIGN KEY (guild_id) REFERENCES guilds(guild_id) ON DELETE CASCADE
);
9 changes: 9 additions & 0 deletions extensions/config/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .levelling import LevellingConfig


class Config(LevellingConfig):
pass


async def setup(bot):
await bot.add_cog(Config(bot))
8 changes: 8 additions & 0 deletions extensions/config/_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from discord.ext import commands

from bot import Korii


class ConfigBase(commands.Cog):
def __init__(self, bot: Korii):
self.bot = bot
Loading

0 comments on commit 214c6dc

Please sign in to comment.