Update cogs/moderation.py
Some checks failed
Pylint / Pylint (push) Failing after 38s

This commit is contained in:
Seaswimmer 2023-09-24 08:09:25 -04:00
parent 2ab142af62
commit 506dda09b3

View file

@ -28,14 +28,15 @@ class Moderation(commands.Cog):
connection = mysql.connector.connect(host=db_host,user=db_user,password=db_password,database=db) connection = mysql.connector.connect(host=db_host,user=db_user,password=db_password,database=db)
return connection return connection
def create_server_table(self, server_id): def create_server_table(self, server):
database = Moderation.mysql_connect(self) database = Moderation.mysql_connect(self)
cursor = database.cursor() cursor = database.cursor()
try: try:
cursor.execute(f"SELECT * FROM `{server_id.lower()}_moderation`") cursor.execute(f"SELECT * FROM `{server.id.lower()}_moderation`")
print(f"MySQL Table exists for server {server.name} ({server.id})")
except mysql.connector.errors.ProgrammingError: except mysql.connector.errors.ProgrammingError:
query = f""" query = f"""
CREATE TABLE `{server_id.lower()}_moderation` ( CREATE TABLE `{server.id.lower()}_moderation` (
moderation_id INT UNIQUE PRIMARY KEY NOT NULL, moderation_id INT UNIQUE PRIMARY KEY NOT NULL,
timestamp INT NOT NULL, timestamp INT NOT NULL,
moderation_type LONGTEXT NOT NULL, moderation_type LONGTEXT NOT NULL,
@ -45,41 +46,61 @@ class Moderation(commands.Cog):
end_timestamp INT, end_timestamp INT,
reason LONGTEXT, reason LONGTEXT,
resolved BOOL NOT NULL, resolved BOOL NOT NULL,
resolve_reason LONGTEXT resolve_reason LONGTEXT,
expired BOOL NOT NULL
) )
""" """
cursor.execute(query) cursor.execute(query)
insert_query = f""" insert_query = f"""
INSERT INTO `{server_id.lower()}_moderation` INSERT INTO `{server.id.lower()}_moderation`
(moderation_id, timestamp, moderation_type, target_id, moderator_id, duration, end_timestamp, reason, resolved, resolve_reason) (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) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
""" """
insert_values = (0, 0, "NULL", 0, 0, "NULL", 0, "NULL", 0, "NULL") insert_values = (0, 0, "NULL", 0, 0, "NULL", 0, "NULL", 0, "NULL", 0)
cursor.execute(insert_query, insert_values) cursor.execute(insert_query, insert_values)
database.commit() database.commit()
database.close() database.close()
print(f"MySQL Table Created!\n{server_id.lower()}_moderation") print(f"MySQL Table created for {server.name} ({server.id})\n{server.id.lower()}_moderation")
else: else:
database.close() database.close()
return return
async def tempban_handler(self):
for server in self.servers:
database = Moderation.mysql_connect(self)
cursor = database.cursor()
bans = await server.fetch_bans()
for ServerBan in bans:
target_id = ServerBan.user_id
cursor.execute(f"SELECT moderation_id FROM `{server.id.lower()}_moderation` WHERE target_id = {target_id} AND moderation_type = 'Temporary Ban' AND end_timestamp < NOW() AND expired = 0")
result = cursor.fetchone()
if result is None:
continue
ServerBan.unban()
cursor.execute(f"UPDATE `{server.id.lower()}_moderation` SET expired = 1 WHERE moderation_id = {result[1]}")
database.commit()
cursor.close()
print("Tempban Handler run successful!")
def mysql_log(self, ctx: commands.Context, moderation_type, target_id, duration, reason): def mysql_log(self, ctx: commands.Context, moderation_type, target_id, duration, reason):
timestamp = int(time.time()) timestamp = int(time.time())
if duration != "NULL": if duration != "NULL":
end_timedelta = datetime.fromtimestamp(timestamp) + duration end_timedelta = datetime.fromtimestamp(timestamp) + duration
end_timestamp = int(end_timedelta.timestamp()) end_timestamp = int(end_timedelta.timestamp())
else: else:
end_timestamp = "NULL" end_timestamp = 0
database = Moderation.mysql_connect(self) database = Moderation.mysql_connect(self)
cursor = database.cursor() cursor = database.cursor()
cursor.execute(f"SELECT moderation_id FROM `{ctx.server.id.lower()}_moderation` ORDER BY moderation_id DESC LIMIT 1") cursor.execute(f"SELECT moderation_id FROM `{ctx.server.id.lower()}_moderation` ORDER BY moderation_id DESC LIMIT 1")
moderation_id = cursor.fetchone()[0] + 1 moderation_id = cursor.fetchone()[0] + 1
sql = f"INSERT INTO `{ctx.server.id.lower()}_moderation` (moderation_id, timestamp, moderation_type, target_id, moderator_id, duration, end_timestamp, reason, resolved, resolve_reason) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" sql = f"INSERT INTO `{ctx.server.id.lower()}_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)"
val = (moderation_id, timestamp, moderation_type, target_id, ctx.author.id, duration, end_timestamp, f"{reason}", 0, "NULL") val = (moderation_id, timestamp, moderation_type, target_id, ctx.author.id, duration, end_timestamp, f"{reason}", 0, "NULL", 0)
cursor.execute(sql, val) cursor.execute(sql, val)
database.commit() database.commit()
database.close() database.close()
print(f"MySQL Row Inserted!\n{moderation_id}, {timestamp}, {moderation_type}, {target_id}, {ctx.author.id}, {duration}, {end_timestamp} {reason}, 0, NULL") print(f"MySQL row inserted into {ctx.server.id.lower()}_moderation!\n{moderation_id}, {timestamp}, {moderation_type}, {target_id}, {ctx.author.id}, {duration}, {end_timestamp}, {reason}, 0, NULL")
@commands.command(name="timeout", aliases=["mute"]) @commands.command(name="timeout", aliases=["mute"])
async def timeout(self, ctx: commands.Context, target: commands.MemberConverter, duration: str, *, reason: str): async def timeout(self, ctx: commands.Context, target: commands.MemberConverter, duration: str, *, reason: str):
@ -98,30 +119,27 @@ class Moderation(commands.Cog):
await target.timeout(parsed_time) await target.timeout(parsed_time)
response = await ctx.message.reply(f"{target.mention} has been timed out for {str(parsed_time)}!\n**Reason** - `{reason}`") response = await ctx.message.reply(f"{target.mention} has been timed out for {str(parsed_time)}!\n**Reason** - `{reason}`")
try: try:
embeds = [CustomEmbed(title="Timed Out", description=f"You have been timed out for `{str(parsed_time)}` in {ctx.server.name}.\n### Reason\n`{reason}`", color="#5d82d1")] embed = CustomEmbed(title="Timed Out", description=f"You have been timed out for `{str(parsed_time)}` in {ctx.server.name}.\n### Reason\n`{reason}`", color="#5d82d1")
await target.send(embeds=embeds) await target.send(embed=embed)
except revolt.errors.HTTPError: except revolt.errors.HTTPError:
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
Moderation.mysql_log(self, ctx, moderation_type='Timeout', target_id=target.id, duration=parsed_time, reason=reason) Moderation.mysql_log(self, ctx, moderation_type='Timeout', target_id=target.id, duration=parsed_time, reason=reason)
@commands.command(name="untimeout", aliases=["unmute"]) @commands.command(name="untimeout", aliases=["unmute"])
async def untimeout(self, ctx: commands.Context, target: commands.MemberConverter, *, reason: str): async def untimeout(self, ctx: commands.Context, target: commands.MemberConverter, *, reason: str = "No reason provided."):
required_role = utils.get(ctx.server.roles, id=required_role_id) required_role = utils.get(ctx.server.roles, id=required_role_id)
if required_role not in ctx.author.roles: if required_role not in ctx.author.roles:
await ctx.message.reply("You do not have permission to use this command!") await ctx.message.reply("You do not have permission to use this command!")
return return
if not reason:
await ctx.message.reply("Please include a reason!")
return
parsed_time = parse(sval="0s", as_timedelta=True, raise_exception=True) parsed_time = parse(sval="0s", as_timedelta=True, raise_exception=True)
await target.timeout(parsed_time) await target.timeout(parsed_time)
response = await ctx.message.reply(f"{target.mention} has had their timeout removed!\n**Reason** - `{reason}`") response = await ctx.message.reply(f"{target.mention} has had their timeout removed!\n**Reason** - `{reason}`")
try: try:
embeds = [CustomEmbed(title="Timeout Removed", description=f"Your timeout has been removed in {ctx.server.name}.\n### Reason\n`{reason}`", color="#5d82d1")] embed = CustomEmbed(title="Timeout Removed", description=f"Your timeout has been removed in {ctx.server.name}.\n### Reason\n`{reason}`", color="#5d82d1")
await target.send(embeds=embeds) await target.send(embed=embed)
except revolt.errors.HTTPError: except revolt.errors.HTTPError:
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
Moderation.mysql_log(self, ctx, moderation_type='Untimeout', target_id=target.id, duration=parsed_time, reason=reason) Moderation.mysql_log(self, ctx, moderation_type='Untimeout', target_id=target.id, duration='NULL', reason=reason)
@commands.command() @commands.command()
async def warn(self, ctx: commands.Context, target: commands.MemberConverter, *, reason: str): async def warn(self, ctx: commands.Context, target: commands.MemberConverter, *, reason: str):
@ -134,8 +152,8 @@ class Moderation(commands.Cog):
return return
response = await ctx.message.reply(f"{target.mention} has been warned!\n**Reason** - `{reason}`") response = await ctx.message.reply(f"{target.mention} has been warned!\n**Reason** - `{reason}`")
try: try:
embeds = [CustomEmbed(title="Warned", description=f"You have been warned in {ctx.server.name}!\n### Reason\n`{reason}`", color="#5d82d1")] embed = CustomEmbed(title="Warned", description=f"You have been warned in {ctx.server.name}!\n### Reason\n`{reason}`", color="#5d82d1")
await target.send(embeds=embeds) await target.send(embed=embed)
except revolt.errors.HTTPError: except revolt.errors.HTTPError:
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
Moderation.mysql_log(self, ctx, moderation_type='Warning', target_id=target.id, duration='NULL', reason=reason) Moderation.mysql_log(self, ctx, moderation_type='Warning', target_id=target.id, duration='NULL', reason=reason)
@ -156,8 +174,8 @@ class Moderation(commands.Cog):
return return
response = await ctx.message.reply(f"{target.mention} has been kicked!\n**Reason** - `{reason}`") response = await ctx.message.reply(f"{target.mention} has been kicked!\n**Reason** - `{reason}`")
try: try:
embeds = [CustomEmbed(title="Warned", description=f"You have been kicked from {ctx.server.name}!\n### Reason\n`{reason}`", color="#5d82d1")] embed = CustomEmbed(title="Warned", description=f"You have been kicked from {ctx.server.name}!\n### Reason\n`{reason}`", color="#5d82d1")
await target.send(embeds=embeds) await target.send(embed=embed)
except revolt.errors.HTTPError: except revolt.errors.HTTPError:
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
Moderation.mysql_log(self, ctx, moderation_type='Kick', target_id=target.id, duration='NULL', reason=reason) Moderation.mysql_log(self, ctx, moderation_type='Kick', target_id=target.id, duration='NULL', reason=reason)
@ -175,8 +193,8 @@ class Moderation(commands.Cog):
await target.ban(reason=reason) await target.ban(reason=reason)
response = await ctx.message.reply(f"{target.mention} has been banned!\n**Reason** - `{reason}`") response = await ctx.message.reply(f"{target.mention} has been banned!\n**Reason** - `{reason}`")
try: try:
embeds = [CustomEmbed(title="Banned", description=f"You have been banned from `{ctx.server.name}`.\n### Reason\n`{reason}`", color="#5d82d1")] embed = CustomEmbed(title="Banned", description=f"You have been banned from `{ctx.server.name}`.\n### Reason\n`{reason}`", color="#5d82d1")
await target.send(embeds=embeds) await target.send(embed=embed)
except revolt.errors.HTTPError: except revolt.errors.HTTPError:
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
Moderation.mysql_log(self, ctx, moderation_type='Ban', target_id=target.id, duration='NULL', reason=reason) Moderation.mysql_log(self, ctx, moderation_type='Ban', target_id=target.id, duration='NULL', reason=reason)
@ -201,8 +219,8 @@ class Moderation(commands.Cog):
await ban.unban() await ban.unban()
response = await ctx.message.reply(f"{target.mention} has been unbanned!\n**Reason** - `{reason}`") response = await ctx.message.reply(f"{target.mention} has been unbanned!\n**Reason** - `{reason}`")
try: try:
embeds = [CustomEmbed(title="Unbanned", description=f"You have been unbanned from `{ctx.server.name}`.\n### Reason\n`{reason}`", color="#5d82d1")] embed = CustomEmbed(title="Unbanned", description=f"You have been unbanned from `{ctx.server.name}`.\n### Reason\n`{reason}`", color="#5d82d1")
await target.send(embeds=embeds) await target.send(embed=embed)
except revolt.errors.HTTPError: except revolt.errors.HTTPError:
await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*") await response.edit(content=f"{response.content}\n*Failed to send DM, user likely has the bot blocked.*")
Moderation.mysql_log(self, ctx, moderation_type='Unban', target_id=target.id, duration='NULL', reason=str(reason)) Moderation.mysql_log(self, ctx, moderation_type='Unban', target_id=target.id, duration='NULL', reason=str(reason))