forked from blizzthewolf/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.embed_factory import embed_factory
|
||||
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):
|
||||
|
@ -572,12 +572,13 @@ class Aurora(commands.Cog):
|
|||
database = connect()
|
||||
|
||||
if export:
|
||||
cursor = database.cursor(dictionary=True)
|
||||
database.row_factory = sqlite3.Row
|
||||
cursor = database.cursor()
|
||||
|
||||
query = """SELECT *
|
||||
FROM moderation_%s
|
||||
query = f"""SELECT *
|
||||
FROM moderation_{interaction.guild.id}
|
||||
ORDER BY moderation_id DESC;"""
|
||||
cursor.execute(query, (interaction.guild.id,))
|
||||
cursor.execute(query)
|
||||
|
||||
results = cursor.fetchall()
|
||||
|
||||
|
@ -599,22 +600,22 @@ class Aurora(commands.Cog):
|
|||
cursor = database.cursor()
|
||||
|
||||
if target:
|
||||
query = """SELECT *
|
||||
FROM moderation_%s
|
||||
WHERE target_id = %s
|
||||
query = f"""SELECT *
|
||||
FROM moderation_{interaction.guild.id}
|
||||
WHERE target_id = ?
|
||||
ORDER BY moderation_id DESC;"""
|
||||
cursor.execute(query, (interaction.guild.id, target.id))
|
||||
cursor.execute(query, (target.id,))
|
||||
elif moderator:
|
||||
query = """SELECT *
|
||||
FROM moderation_%s
|
||||
WHERE moderator_id = %s
|
||||
query = f"""SELECT *
|
||||
FROM moderation_{interaction.guild.id}
|
||||
WHERE moderator_id = ?
|
||||
ORDER BY moderation_id DESC;"""
|
||||
cursor.execute(query, (interaction.guild.id, moderator.id))
|
||||
cursor.execute(query, (moderator.id,))
|
||||
else:
|
||||
query = """SELECT *
|
||||
FROM moderation_%s
|
||||
query = f"""SELECT *
|
||||
FROM moderation_{interaction.guild.id}
|
||||
ORDER BY moderation_id DESC;"""
|
||||
cursor.execute(query, (interaction.guild.id,))
|
||||
cursor.execute(query)
|
||||
|
||||
results = cursor.fetchall()
|
||||
result_dict_list = []
|
||||
|
@ -694,15 +695,15 @@ class Aurora(commands.Cog):
|
|||
database = connect()
|
||||
cursor = database.cursor()
|
||||
|
||||
query_1 = "SELECT * FROM moderation_%s WHERE moderation_id = %s;"
|
||||
cursor.execute(query_1, (interaction.guild.id, case))
|
||||
query_1 = f"SELECT * FROM moderation_{interaction.guild.id} WHERE moderation_id = ?;"
|
||||
cursor.execute(query_1, (case,))
|
||||
result_1 = cursor.fetchone()
|
||||
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)
|
||||
return
|
||||
|
||||
query_2 = "SELECT * FROM moderation_%s WHERE moderation_id = %s AND resolved = 0;"
|
||||
cursor.execute(query_2, (interaction.guild.id, case))
|
||||
query_2 = f"SELECT * FROM moderation_{interaction.guild.id} WHERE moderation_id = ? AND resolved = 0;"
|
||||
cursor.execute(query_2, (case,))
|
||||
result_2 = cursor.fetchone()
|
||||
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)
|
||||
|
@ -754,9 +755,9 @@ class Aurora(commands.Cog):
|
|||
except discord.NotFound:
|
||||
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:
|
||||
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']))
|
||||
database.commit()
|
||||
|
@ -850,10 +851,6 @@ class Aurora(commands.Cog):
|
|||
parsed_time = None
|
||||
case_dict = await fetch_case(case, interaction.guild.id)
|
||||
if case_dict:
|
||||
conf = await check_conf(['mysql_database'])
|
||||
if conf:
|
||||
raise(LookupError)
|
||||
|
||||
if duration:
|
||||
try:
|
||||
parsed_time = parse(sval=duration, as_timedelta=True, raise_exception=True)
|
||||
|
@ -915,13 +912,12 @@ class Aurora(commands.Cog):
|
|||
|
||||
database = connect()
|
||||
cursor = database.cursor()
|
||||
db = await config.mysql_database()
|
||||
|
||||
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))
|
||||
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))
|
||||
database.commit()
|
||||
|
||||
|
@ -938,19 +934,14 @@ class Aurora(commands.Cog):
|
|||
|
||||
@tasks.loop(minutes=1)
|
||||
async def handle_expiry(self):
|
||||
conf = await check_conf(['mysql_database'])
|
||||
if conf:
|
||||
raise(LookupError)
|
||||
|
||||
database = connect()
|
||||
cursor = database.cursor()
|
||||
db = await config.mysql_database()
|
||||
|
||||
guilds: list[discord.Guild] = self.bot.guilds
|
||||
for guild in guilds:
|
||||
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:
|
||||
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:
|
||||
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(),))
|
||||
|
||||
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:
|
||||
cursor.execute(blacklist_query, (time.time(),))
|
||||
result = cursor.fetchall()
|
||||
|
|
|
@ -10,7 +10,7 @@ from discord import Guild
|
|||
from redbot.core import data_manager
|
||||
|
||||
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:
|
||||
|
@ -37,43 +37,39 @@ async def create_guild_table(guild: Guild):
|
|||
except sqlite3.ProgrammingError:
|
||||
query = f"""
|
||||
CREATE TABLE `moderation_{guild.id}` (
|
||||
moderation_id INT UNIQUE PRIMARY KEY NOT NULL,
|
||||
timestamp INT NOT NULL,
|
||||
moderation_type LONGTEXT NOT NULL,
|
||||
target_type LONGTEXT NOT NULL,
|
||||
target_id LONGTEXT NOT NULL,
|
||||
moderator_id LONGTEXT NOT NULL,
|
||||
role_id LONGTEXT,
|
||||
duration LONGTEXT,
|
||||
end_timestamp INT,
|
||||
reason LONGTEXT,
|
||||
resolved BOOL NOT NULL,
|
||||
resolved_by LONGTEXT,
|
||||
resolve_reason LONGTEXT,
|
||||
expired BOOL NOT NULL,
|
||||
changes JSON NOT NULL,
|
||||
metadata JSON NOT NULL
|
||||
moderation_id INTEGER PRIMARY KEY,
|
||||
timestamp INTEGER NOT NULL,
|
||||
moderation_type TEXT NOT NULL,
|
||||
target_type TEXT NOT NULL,
|
||||
target_id TEXT NOT NULL,
|
||||
moderator_id TEXT NOT NULL,
|
||||
role_id TEXT,
|
||||
duration TEXT,
|
||||
end_timestamp INTEGER,
|
||||
reason TEXT,
|
||||
resolved INTEGER NOT NULL,
|
||||
resolved_by TEXT,
|
||||
resolve_reason TEXT,
|
||||
expired INTEGER NOT NULL,
|
||||
changes TEXT NOT NULL,
|
||||
metadata TEXT NOT NULL
|
||||
)
|
||||
"""
|
||||
cursor.execute(query)
|
||||
|
||||
index_query_1 = "CREATE INDEX idx_target_id ON moderation_%s(target_id(25));"
|
||||
cursor.execute(index_query_1, (guild.id,))
|
||||
index_query_1 = "CREATE INDEX idx_target_id ON moderation_{}(target_id);"
|
||||
cursor.execute(index_query_1.format(guild.id))
|
||||
|
||||
index_query_2 = (
|
||||
"CREATE INDEX idx_moderator_id ON moderation_%s(moderator_id(25));"
|
||||
)
|
||||
cursor.execute(index_query_2, (guild.id,))
|
||||
index_query_2 = "CREATE INDEX idx_moderator_id ON moderation_{}(moderator_id);"
|
||||
cursor.execute(index_query_2.format(guild.id))
|
||||
|
||||
index_query_3 = (
|
||||
"CREATE INDEX idx_moderation_id ON moderation_%s(moderation_id);"
|
||||
)
|
||||
cursor.execute(index_query_3, (guild.id,))
|
||||
index_query_3 = "CREATE INDEX idx_moderation_id ON moderation_{}(moderation_id);"
|
||||
cursor.execute(index_query_3.format(guild.id))
|
||||
|
||||
insert_query = 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)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
"""
|
||||
insert_values = (
|
||||
0,
|
||||
|
@ -98,7 +94,7 @@ async def create_guild_table(guild: Guild):
|
|||
database.commit()
|
||||
|
||||
logger.debug(
|
||||
"SQLite Table (moderation_%s) created for %s (%s)",
|
||||
"SQLite Table (moderation_?) created for ? (?)",
|
||||
guild.id,
|
||||
guild.name,
|
||||
guild.id,
|
||||
|
@ -122,9 +118,9 @@ async def mysql_log(
|
|||
resolved_by: str = None,
|
||||
resolved_reason: str = None,
|
||||
expired: bool = None,
|
||||
changes: list = [],
|
||||
metadata: dict = {},
|
||||
) -> int: # pylint: disable=dangerous-default-value
|
||||
changes: list = [], # pylint: disable=dangerous-default-value
|
||||
metadata: dict = {}, # pylint: disable=dangerous-default-value
|
||||
) -> int:
|
||||
if not timestamp:
|
||||
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)
|
||||
|
||||
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 = (
|
||||
moderation_id,
|
||||
timestamp,
|
||||
|
@ -210,8 +206,8 @@ async def fetch_case(moderation_id: int, guild_id: str) -> dict:
|
|||
database = connect()
|
||||
cursor = database.cursor()
|
||||
|
||||
query = "SELECT * FROM moderation_%s WHERE moderation_id = %s;"
|
||||
cursor.execute(query, (guild_id, moderation_id))
|
||||
query = f"SELECT * FROM moderation_{guild_id} WHERE moderation_id = ?;"
|
||||
cursor.execute(query, (moderation_id,))
|
||||
result = cursor.fetchone()
|
||||
|
||||
cursor.close()
|
||||
|
|
|
@ -10,17 +10,6 @@ from redbot.core import commands
|
|||
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(
|
||||
user: User,
|
||||
permissions: list,
|
||||
|
|
Loading…
Reference in a new issue