feat(moderation): added the basis of the database system
Some checks failed
Pylint / Pylint (push) Failing after 1m12s

This commit is contained in:
Seaswimmer 2023-10-04 09:11:49 -04:00
parent da5fa2da45
commit 6017fbdd20
No known key found for this signature in database
GPG key ID: 5019678FD9CF50D8
3 changed files with 82 additions and 3 deletions

View file

@ -1,5 +1,9 @@
import logging
import discord
from redbot.core import checks, commands, Config
import mysql.connector
from pytimeparse2 import disable_dateutil
from redbot.core import Config, checks, commands
class Moderation(commands.Cog):
"""Custom cog moderation cog, meant to copy GalacticBot.
@ -14,9 +18,73 @@ class Moderation(commands.Cog):
mysql_user = " ",
mysql_password = " "
)
disable_dateutil()
async def cog_load(self):
"""This method prepares the database schema for all of the guilds the bot is currently in."""
guilds: list[discord.Guild] = self.bot.guilds
for guild in guilds:
await self.create_guild_table(guild)
@commands.Cog.listener('on_guild_join')
async def db_generate_guild_join(self, guild: discord.Guild):
"""This method prepares the database schema whenever the bot joins a guild."""
await self.create_guild_table(guild)
async def connect(self):
"""Connects to the MySQL database."""
"""Connects to the MySQL database, and returns a connection object."""
conf = await self.check_conf([
'mysql_address',
'mysql_database',
'mysql_user',
'mysql_password'
])
if conf:
raise LookupError
connection = mysql.connector.connect(
host=await self.config.mysql_address(),
user=await self.config.mysql_user(),
password=await self.config.mysql_password(),
database=await self.config.mysql_db()
)
return connection
async def create_guild_table(self, guild: discord.Guild):
database = await self.connect()
cursor = database.cursor()
try:
cursor.execute(f"SELECT * FROM `{guild.id}_moderation`")
logging.info(f"MySQL Table exists for server {guild.name} ({guild.id})")
except mysql.connector.errors.ProgrammingError:
query = f"""
CREATE TABLE `{guild.id}_moderation` (
moderation_id INT UNIQUE PRIMARY KEY NOT NULL,
timestamp INT NOT NULL,
moderation_type LONGTEXT NOT NULL,
target_id LONGTEXT NOT NULL,
moderator_id LONGTEXT NOT NULL,
duration LONGTEXT,
end_timestamp INT,
reason LONGTEXT,
resolved BOOL NOT NULL,
resolve_reason LONGTEXT,
expired BOOL NOT NULL
)
"""
cursor.execute(query)
insert_query = f"""
INSERT INTO `{guild.id}_moderation`
(moderation_id, timestamp, moderation_type, target_id, moderator_id, duration, end_timestamp, reason, resolved, resolve_reason, expired)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
"""
insert_values = (0, 0, "NULL", 0, 0, "NULL", 0, "NULL", 0, "NULL", 0)
cursor.execute(insert_query, insert_values)
database.commit()
database.close()
logging.info(f"MySQL Table created for {guild.name} ({guild.id})\n{guild.id}_moderation")
else:
database.close()
return
async def check_conf(self, config: list):
"""Checks if any required config options are not set."""

12
poetry.lock generated
View file

@ -1138,6 +1138,16 @@ files = [
{file = "mutagen-1.47.0.tar.gz", hash = "sha256:719fadef0a978c31b4cf3c956261b3c58b6948b32023078a2117b1de09f0fc99"},
]
[[package]]
name = "mysql-connector"
version = "2.2.9"
description = "MySQL driver written in Python"
optional = false
python-versions = "*"
files = [
{file = "mysql-connector-2.2.9.tar.gz", hash = "sha256:1733e6ce52a049243de3264f1fbc22a852cb35458c4ad739ba88189285efdf32"},
]
[[package]]
name = "nodeenv"
version = "1.8.0"
@ -2345,4 +2355,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p
[metadata]
lock-version = "2.0"
python-versions = ">=3.11,<3.12"
content-hash = "634e8b087e541f788c43821efee6c592a16030cb93e6fc0a4e98891abc90b061"
content-hash = "9abb44d966d5eaab361627f12ac0d479abee1dd213979040187ffad74ca324fe"

View file

@ -12,6 +12,7 @@ Red-DiscordBot = "^3.5.5"
pytimeparse2 = "^1.7.1"
yt-dlp = "^2023.9.24"
prisma = "^0.10.0"
mysql-connector = "^2.2.9"
[tool.poetry.group.dev]
optional = true