forked from cswimr/SeaCogs
fix(aurora): fixing sqlite queries and pylint errors
This commit is contained in:
parent
ca50deedd5
commit
a402f23001
3 changed files with 59 additions and 83 deletions
|
@ -25,7 +25,7 @@ from .utilities.config import config, register_config
|
||||||
from .utilities.database import connect, create_guild_table, fetch_case, mysql_log
|
from .utilities.database import connect, create_guild_table, fetch_case, mysql_log
|
||||||
from .utilities.embed_factory import embed_factory
|
from .utilities.embed_factory import embed_factory
|
||||||
from .utilities.logger import logger
|
from .utilities.logger import logger
|
||||||
from .utilities.utils import check_conf, check_moddable, check_permissions, fetch_channel_dict, fetch_user_dict, generate_dict, log, send_evidenceformat
|
from .utilities.utils import check_moddable, check_permissions, fetch_channel_dict, fetch_user_dict, generate_dict, log, send_evidenceformat
|
||||||
|
|
||||||
|
|
||||||
class Aurora(commands.Cog):
|
class Aurora(commands.Cog):
|
||||||
|
@ -572,12 +572,13 @@ class Aurora(commands.Cog):
|
||||||
database = connect()
|
database = connect()
|
||||||
|
|
||||||
if export:
|
if export:
|
||||||
cursor = database.cursor(dictionary=True)
|
database.row_factory = sqlite3.Row
|
||||||
|
cursor = database.cursor()
|
||||||
|
|
||||||
query = """SELECT *
|
query = f"""SELECT *
|
||||||
FROM moderation_%s
|
FROM moderation_{interaction.guild.id}
|
||||||
ORDER BY moderation_id DESC;"""
|
ORDER BY moderation_id DESC;"""
|
||||||
cursor.execute(query, (interaction.guild.id,))
|
cursor.execute(query)
|
||||||
|
|
||||||
results = cursor.fetchall()
|
results = cursor.fetchall()
|
||||||
|
|
||||||
|
@ -599,22 +600,22 @@ class Aurora(commands.Cog):
|
||||||
cursor = database.cursor()
|
cursor = database.cursor()
|
||||||
|
|
||||||
if target:
|
if target:
|
||||||
query = """SELECT *
|
query = f"""SELECT *
|
||||||
FROM moderation_%s
|
FROM moderation_{interaction.guild.id}
|
||||||
WHERE target_id = %s
|
WHERE target_id = ?
|
||||||
ORDER BY moderation_id DESC;"""
|
ORDER BY moderation_id DESC;"""
|
||||||
cursor.execute(query, (interaction.guild.id, target.id))
|
cursor.execute(query, (target.id,))
|
||||||
elif moderator:
|
elif moderator:
|
||||||
query = """SELECT *
|
query = f"""SELECT *
|
||||||
FROM moderation_%s
|
FROM moderation_{interaction.guild.id}
|
||||||
WHERE moderator_id = %s
|
WHERE moderator_id = ?
|
||||||
ORDER BY moderation_id DESC;"""
|
ORDER BY moderation_id DESC;"""
|
||||||
cursor.execute(query, (interaction.guild.id, moderator.id))
|
cursor.execute(query, (moderator.id,))
|
||||||
else:
|
else:
|
||||||
query = """SELECT *
|
query = f"""SELECT *
|
||||||
FROM moderation_%s
|
FROM moderation_{interaction.guild.id}
|
||||||
ORDER BY moderation_id DESC;"""
|
ORDER BY moderation_id DESC;"""
|
||||||
cursor.execute(query, (interaction.guild.id,))
|
cursor.execute(query)
|
||||||
|
|
||||||
results = cursor.fetchall()
|
results = cursor.fetchall()
|
||||||
result_dict_list = []
|
result_dict_list = []
|
||||||
|
@ -694,15 +695,15 @@ class Aurora(commands.Cog):
|
||||||
database = connect()
|
database = connect()
|
||||||
cursor = database.cursor()
|
cursor = database.cursor()
|
||||||
|
|
||||||
query_1 = "SELECT * FROM moderation_%s WHERE moderation_id = %s;"
|
query_1 = f"SELECT * FROM moderation_{interaction.guild.id} WHERE moderation_id = ?;"
|
||||||
cursor.execute(query_1, (interaction.guild.id, case))
|
cursor.execute(query_1, (case,))
|
||||||
result_1 = cursor.fetchone()
|
result_1 = cursor.fetchone()
|
||||||
if result_1 is None or case == 0:
|
if result_1 is None or case == 0:
|
||||||
await interaction.response.send_message(content=f"There is no moderation with a case number of {case}.", ephemeral=True)
|
await interaction.response.send_message(content=f"There is no moderation with a case number of {case}.", ephemeral=True)
|
||||||
return
|
return
|
||||||
|
|
||||||
query_2 = "SELECT * FROM moderation_%s WHERE moderation_id = %s AND resolved = 0;"
|
query_2 = f"SELECT * FROM moderation_{interaction.guild.id} WHERE moderation_id = ? AND resolved = 0;"
|
||||||
cursor.execute(query_2, (interaction.guild.id, case))
|
cursor.execute(query_2, (case,))
|
||||||
result_2 = cursor.fetchone()
|
result_2 = cursor.fetchone()
|
||||||
if result_2 is None:
|
if result_2 is None:
|
||||||
await interaction.response.send_message(content=f"This moderation has already been resolved!\nUse `/case {case}` for more information.", ephemeral=True)
|
await interaction.response.send_message(content=f"This moderation has already been resolved!\nUse `/case {case}` for more information.", ephemeral=True)
|
||||||
|
@ -754,9 +755,9 @@ class Aurora(commands.Cog):
|
||||||
except discord.NotFound:
|
except discord.NotFound:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
resolve_query = f"UPDATE `moderation_{interaction.guild.id}` SET resolved = 1, changes = %s, resolved_by = %s, resolve_reason = %s WHERE moderation_id = %s"
|
resolve_query = f"UPDATE `moderation_{interaction.guild.id}` SET resolved = 1, changes = ?, resolved_by = ?, resolve_reason = ? WHERE moderation_id = ?"
|
||||||
else:
|
else:
|
||||||
resolve_query = f"UPDATE `moderation_{interaction.guild.id}` SET resolved = 1, changes = %s, resolved_by = %s, resolve_reason = %s WHERE moderation_id = %s"
|
resolve_query = f"UPDATE `moderation_{interaction.guild.id}` SET resolved = 1, changes = ?, resolved_by = ?, resolve_reason = ? WHERE moderation_id = ?"
|
||||||
|
|
||||||
cursor.execute(resolve_query, (json.dumps(changes), interaction.user.id, reason, case_dict['moderation_id']))
|
cursor.execute(resolve_query, (json.dumps(changes), interaction.user.id, reason, case_dict['moderation_id']))
|
||||||
database.commit()
|
database.commit()
|
||||||
|
@ -850,10 +851,6 @@ class Aurora(commands.Cog):
|
||||||
parsed_time = None
|
parsed_time = None
|
||||||
case_dict = await fetch_case(case, interaction.guild.id)
|
case_dict = await fetch_case(case, interaction.guild.id)
|
||||||
if case_dict:
|
if case_dict:
|
||||||
conf = await check_conf(['mysql_database'])
|
|
||||||
if conf:
|
|
||||||
raise(LookupError)
|
|
||||||
|
|
||||||
if duration:
|
if duration:
|
||||||
try:
|
try:
|
||||||
parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True)
|
parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True)
|
||||||
|
@ -915,13 +912,12 @@ class Aurora(commands.Cog):
|
||||||
|
|
||||||
database = connect()
|
database = connect()
|
||||||
cursor = database.cursor()
|
cursor = database.cursor()
|
||||||
db = await config.mysql_database()
|
|
||||||
|
|
||||||
if parsed_time:
|
if parsed_time:
|
||||||
update_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET changes = %s, reason = %s, duration = %s, end_timestamp = %s WHERE moderation_id = %s"
|
update_query = f"UPDATE `moderation_{interaction.guild.id}` SET changes = %s, reason = %s, duration = %s, end_timestamp = %s WHERE moderation_id = %s"
|
||||||
cursor.execute(update_query, (json.dumps(changes), reason, parsed_time, end_timestamp, case))
|
cursor.execute(update_query, (json.dumps(changes), reason, parsed_time, end_timestamp, case))
|
||||||
else:
|
else:
|
||||||
update_query = f"UPDATE `{db}`.`moderation_{interaction.guild.id}` SET changes = %s, reason = %s WHERE moderation_id = %s"
|
update_query = f"UPDATE `moderation_{interaction.guild.id}` SET changes = %s, reason = %s WHERE moderation_id = %s"
|
||||||
cursor.execute(update_query, (json.dumps(changes), reason, case))
|
cursor.execute(update_query, (json.dumps(changes), reason, case))
|
||||||
database.commit()
|
database.commit()
|
||||||
|
|
||||||
|
@ -938,19 +934,14 @@ class Aurora(commands.Cog):
|
||||||
|
|
||||||
@tasks.loop(minutes=1)
|
@tasks.loop(minutes=1)
|
||||||
async def handle_expiry(self):
|
async def handle_expiry(self):
|
||||||
conf = await check_conf(['mysql_database'])
|
|
||||||
if conf:
|
|
||||||
raise(LookupError)
|
|
||||||
|
|
||||||
database = connect()
|
database = connect()
|
||||||
cursor = database.cursor()
|
cursor = database.cursor()
|
||||||
db = await config.mysql_database()
|
|
||||||
|
|
||||||
guilds: list[discord.Guild] = self.bot.guilds
|
guilds: list[discord.Guild] = self.bot.guilds
|
||||||
for guild in guilds:
|
for guild in guilds:
|
||||||
if not await self.bot.cog_disabled_in_guild(self, guild):
|
if not await self.bot.cog_disabled_in_guild(self, guild):
|
||||||
|
|
||||||
tempban_query = f"SELECT target_id, moderation_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= %s AND moderation_type = 'TEMPBAN' AND expired = 0"
|
tempban_query = f"SELECT target_id, moderation_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= ? AND moderation_type = 'TEMPBAN' AND expired = 0"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
cursor.execute(tempban_query, (time.time(),))
|
cursor.execute(tempban_query, (time.time(),))
|
||||||
|
@ -975,10 +966,10 @@ class Aurora(commands.Cog):
|
||||||
except [discord.errors.NotFound, discord.errors.Forbidden, discord.errors.HTTPException] as e:
|
except [discord.errors.NotFound, discord.errors.Forbidden, discord.errors.HTTPException] as e:
|
||||||
print(f"Failed to unban {user.name}#{user.discriminator} ({user.id}) from {guild.name} ({guild.id})\n{e}")
|
print(f"Failed to unban {user.name}#{user.discriminator} ({user.id}) from {guild.name} ({guild.id})\n{e}")
|
||||||
|
|
||||||
expiry_query = f"UPDATE `{db}`.`moderation_{guild.id}` SET expired = 1 WHERE (end_timestamp != 0 AND end_timestamp <= %s AND expired = 0 AND moderation_type != 'BLACKLIST') OR (expired = 0 AND resolved = 1 AND moderation_type != 'BLACKLIST')"
|
expiry_query = f"UPDATE `moderation_{guild.id}` SET expired = 1 WHERE (end_timestamp != 0 AND end_timestamp <= ? AND expired = 0 AND moderation_type != 'BLACKLIST') OR (expired = 0 AND resolved = 1 AND moderation_type != 'BLACKLIST')"
|
||||||
cursor.execute(expiry_query, (time.time(),))
|
cursor.execute(expiry_query, (time.time(),))
|
||||||
|
|
||||||
blacklist_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= %s AND moderation_type = 'BLACKLIST' AND expired = 0"
|
blacklist_query = f"SELECT target_id, moderation_id, role_id FROM moderation_{guild.id} WHERE end_timestamp != 0 AND end_timestamp <= ? AND moderation_type = 'BLACKLIST' AND expired = 0"
|
||||||
try:
|
try:
|
||||||
cursor.execute(blacklist_query, (time.time(),))
|
cursor.execute(blacklist_query, (time.time(),))
|
||||||
result = cursor.fetchall()
|
result = cursor.fetchall()
|
||||||
|
|
|
@ -10,7 +10,7 @@ from discord import Guild
|
||||||
from redbot.core import data_manager
|
from redbot.core import data_manager
|
||||||
|
|
||||||
from .logger import logger
|
from .logger import logger
|
||||||
from .utils import check_conf, generate_dict, get_next_case_number
|
from .utils import generate_dict, get_next_case_number
|
||||||
|
|
||||||
|
|
||||||
def connect() -> sqlite3.Connection:
|
def connect() -> sqlite3.Connection:
|
||||||
|
@ -37,43 +37,39 @@ async def create_guild_table(guild: Guild):
|
||||||
except sqlite3.ProgrammingError:
|
except sqlite3.ProgrammingError:
|
||||||
query = f"""
|
query = f"""
|
||||||
CREATE TABLE `moderation_{guild.id}` (
|
CREATE TABLE `moderation_{guild.id}` (
|
||||||
moderation_id INT UNIQUE PRIMARY KEY NOT NULL,
|
moderation_id INTEGER PRIMARY KEY,
|
||||||
timestamp INT NOT NULL,
|
timestamp INTEGER NOT NULL,
|
||||||
moderation_type LONGTEXT NOT NULL,
|
moderation_type TEXT NOT NULL,
|
||||||
target_type LONGTEXT NOT NULL,
|
target_type TEXT NOT NULL,
|
||||||
target_id LONGTEXT NOT NULL,
|
target_id TEXT NOT NULL,
|
||||||
moderator_id LONGTEXT NOT NULL,
|
moderator_id TEXT NOT NULL,
|
||||||
role_id LONGTEXT,
|
role_id TEXT,
|
||||||
duration LONGTEXT,
|
duration TEXT,
|
||||||
end_timestamp INT,
|
end_timestamp INTEGER,
|
||||||
reason LONGTEXT,
|
reason TEXT,
|
||||||
resolved BOOL NOT NULL,
|
resolved INTEGER NOT NULL,
|
||||||
resolved_by LONGTEXT,
|
resolved_by TEXT,
|
||||||
resolve_reason LONGTEXT,
|
resolve_reason TEXT,
|
||||||
expired BOOL NOT NULL,
|
expired INTEGER NOT NULL,
|
||||||
changes JSON NOT NULL,
|
changes TEXT NOT NULL,
|
||||||
metadata JSON NOT NULL
|
metadata TEXT NOT NULL
|
||||||
)
|
)
|
||||||
"""
|
"""
|
||||||
cursor.execute(query)
|
cursor.execute(query)
|
||||||
|
|
||||||
index_query_1 = "CREATE INDEX idx_target_id ON moderation_%s(target_id(25));"
|
index_query_1 = "CREATE INDEX idx_target_id ON moderation_{}(target_id);"
|
||||||
cursor.execute(index_query_1, (guild.id,))
|
cursor.execute(index_query_1.format(guild.id))
|
||||||
|
|
||||||
index_query_2 = (
|
index_query_2 = "CREATE INDEX idx_moderator_id ON moderation_{}(moderator_id);"
|
||||||
"CREATE INDEX idx_moderator_id ON moderation_%s(moderator_id(25));"
|
cursor.execute(index_query_2.format(guild.id))
|
||||||
)
|
|
||||||
cursor.execute(index_query_2, (guild.id,))
|
|
||||||
|
|
||||||
index_query_3 = (
|
index_query_3 = "CREATE INDEX idx_moderation_id ON moderation_{}(moderation_id);"
|
||||||
"CREATE INDEX idx_moderation_id ON moderation_%s(moderation_id);"
|
cursor.execute(index_query_3.format(guild.id))
|
||||||
)
|
|
||||||
cursor.execute(index_query_3, (guild.id,))
|
|
||||||
|
|
||||||
insert_query = f"""
|
insert_query = f"""
|
||||||
INSERT INTO `moderation_{guild.id}`
|
INSERT INTO `moderation_{guild.id}`
|
||||||
(moderation_id, timestamp, moderation_type, target_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata)
|
(moderation_id, timestamp, moderation_type, target_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata)
|
||||||
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||||
"""
|
"""
|
||||||
insert_values = (
|
insert_values = (
|
||||||
0,
|
0,
|
||||||
|
@ -98,7 +94,7 @@ async def create_guild_table(guild: Guild):
|
||||||
database.commit()
|
database.commit()
|
||||||
|
|
||||||
logger.debug(
|
logger.debug(
|
||||||
"SQLite Table (moderation_%s) created for %s (%s)",
|
"SQLite Table (moderation_?) created for ? (?)",
|
||||||
guild.id,
|
guild.id,
|
||||||
guild.name,
|
guild.name,
|
||||||
guild.id,
|
guild.id,
|
||||||
|
@ -122,9 +118,9 @@ async def mysql_log(
|
||||||
resolved_by: str = None,
|
resolved_by: str = None,
|
||||||
resolved_reason: str = None,
|
resolved_reason: str = None,
|
||||||
expired: bool = None,
|
expired: bool = None,
|
||||||
changes: list = [],
|
changes: list = [], # pylint: disable=dangerous-default-value
|
||||||
metadata: dict = {},
|
metadata: dict = {}, # pylint: disable=dangerous-default-value
|
||||||
) -> int: # pylint: disable=dangerous-default-value
|
) -> int:
|
||||||
if not timestamp:
|
if not timestamp:
|
||||||
timestamp = int(time.time())
|
timestamp = int(time.time())
|
||||||
|
|
||||||
|
@ -155,7 +151,7 @@ async def mysql_log(
|
||||||
|
|
||||||
moderation_id = await get_next_case_number(guild_id=guild_id, cursor=cursor)
|
moderation_id = await get_next_case_number(guild_id=guild_id, cursor=cursor)
|
||||||
|
|
||||||
sql = f"INSERT INTO `moderation_{guild_id}` (moderation_id, timestamp, moderation_type, target_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
|
sql = f"INSERT INTO `moderation_{guild_id}` (moderation_id, timestamp, moderation_type, target_type, target_id, moderator_id, role_id, duration, end_timestamp, reason, resolved, resolved_by, resolve_reason, expired, changes, metadata) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
|
||||||
val = (
|
val = (
|
||||||
moderation_id,
|
moderation_id,
|
||||||
timestamp,
|
timestamp,
|
||||||
|
@ -210,8 +206,8 @@ async def fetch_case(moderation_id: int, guild_id: str) -> dict:
|
||||||
database = connect()
|
database = connect()
|
||||||
cursor = database.cursor()
|
cursor = database.cursor()
|
||||||
|
|
||||||
query = "SELECT * FROM moderation_%s WHERE moderation_id = %s;"
|
query = f"SELECT * FROM moderation_{guild_id} WHERE moderation_id = ?;"
|
||||||
cursor.execute(query, (guild_id, moderation_id))
|
cursor.execute(query, (moderation_id,))
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
|
|
||||||
cursor.close()
|
cursor.close()
|
||||||
|
|
|
@ -10,17 +10,6 @@ from redbot.core import commands
|
||||||
from .config import config
|
from .config import config
|
||||||
|
|
||||||
|
|
||||||
async def check_conf(config_list: list):
|
|
||||||
"""Checks if any required config options are not set."""
|
|
||||||
not_found_list = []
|
|
||||||
|
|
||||||
for item in config_list:
|
|
||||||
if await config.item() == " ":
|
|
||||||
not_found_list.append(item)
|
|
||||||
|
|
||||||
return not_found_list
|
|
||||||
|
|
||||||
|
|
||||||
def check_permissions(
|
def check_permissions(
|
||||||
user: User,
|
user: User,
|
||||||
permissions: list,
|
permissions: list,
|
||||||
|
|
Loading…
Reference in a new issue