From 8e041bb2042fab36675017fc211e4d1eb747d50f Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh <102361830+SeaswimmerTheFsh@users.noreply.github.com> Date: Sat, 1 Jul 2023 13:52:01 -0400 Subject: [PATCH] implemented #16 and some other things --- cogs/info.py | 9 +++++++++ cogs/moderation.py | 49 +++++++++++++++++++++++++++++++++++++++++++--- main.py | 13 ++++-------- 3 files changed, 59 insertions(+), 12 deletions(-) diff --git a/cogs/info.py b/cogs/info.py index 0ce13a2..1f5ba4a 100644 --- a/cogs/info.py +++ b/cogs/info.py @@ -33,6 +33,15 @@ class Info(commands.Cog): class CustomError(Exception): pass + @commands.command() + async def avatar(self, ctx: commands.Context, target: commands.UserConverter): + """This command retrieves a user's avatar. - NOTE: Move to cog""" + if not isinstance(target, revolt.User): + await ctx.message.reply("Please provide a user argument!") + return + avatar = target.avatar.url + await ctx.message.reply(f"{avatar}") + @commands.command() async def channelinfo(self, ctx: commands.Context, channel: commands.ChannelConverter): """Displays information about a channel.""" diff --git a/cogs/moderation.py b/cogs/moderation.py index 12a8730..26aa3f7 100644 --- a/cogs/moderation.py +++ b/cogs/moderation.py @@ -28,8 +28,42 @@ class Moderation(commands.Cog): connection = mysql.connector.connect(host=db_host,user=db_user,password=db_password,database=db) return connection + def create_server_table(self, server_id): + database = Moderation.mysql_connect(self) + cursor = database.cursor() + try: + cursor.execute(f"SELECT * FROM `{server_id.lower()}_moderation`") + except mysql.connector.errors.ProgrammingError: + query = f""" + CREATE TABLE `{server_id.lower()}_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 + ) + """ + cursor.execute(query) + insert_query = f""" + INSERT INTO `{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) + """ + insert_values = (0, 0, "NULL", 0, 0, "NULL", 0, "NULL", 0, "NULL") + cursor.execute(insert_query, insert_values) + database.commit() + database.close() + print(f"MySQL Table Created!\n{server_id.lower()}_moderation") + else: + database.close() + return + def mysql_log(self, ctx: commands.Context, moderation_type, target_id, duration, reason): - moderator_id = ctx.author.id timestamp = int(time.time()) if duration != "NULL": end_timedelta = datetime.fromtimestamp(timestamp) + duration @@ -41,11 +75,20 @@ class Moderation(commands.Cog): 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 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)" - val = (moderation_id, timestamp, moderation_type, target_id, moderator_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") cursor.execute(sql, val) database.commit() database.close() - print(f"MySQL Row Inserted!\n{moderation_id}, {timestamp}, {moderation_type}, {target_id}, {moderator_id}, {duration}, {end_timestamp} {reason}, 0, NULL") + print(f"MySQL Row Inserted!\n{moderation_id}, {timestamp}, {moderation_type}, {target_id}, {ctx.author.id}, {duration}, {end_timestamp} {reason}, 0, NULL") + + @commands.command() + async def dbcreate(self, ctx: commands.Context): + required_role = utils.get(ctx.server.roles, id=required_role_id) + if required_role not in ctx.author.roles: + await ctx.message.reply("You do not have permission to use this command!") + return + self.create_server_table(server_id=ctx.server.id) + await ctx.message.reply("MySQL Table Created!") @commands.command(name="timeout", aliases=["mute"]) async def timeout(self, ctx: commands.Context, target: commands.MemberConverter, duration: str, *, reason: str): diff --git a/main.py b/main.py index dca09a8..0006bb9 100644 --- a/main.py +++ b/main.py @@ -26,6 +26,10 @@ class Client(commands.CommandsClient): return prefix return input + async def on_server_join(self, server: revolt.Server): + print(f"Joined server: {server.name} ({server.id})") + Moderation.create_server_table(self, server.id) + async def on_message_delete(self, message: revolt.Message): if isinstance(message.author, revolt.Member): if message.author.bot is True: @@ -74,15 +78,6 @@ class Client(commands.CommandsClient): await msg.edit(content=" ", embeds=embeds) print(f'Ping {int(ping)}ms') - @commands.command() - async def avatar(self, ctx: commands.Context, target: commands.UserConverter): - """This command retrieves a user's avatar. - NOTE: Move to cog""" - if not isinstance(target, revolt.User): - await ctx.message.reply("Please provide a user argument!") - return - avatar = target.avatar.url - await ctx.message.reply(f"{avatar}") - async def prefix_change(self, message: revolt.Message, new_prefix: str, silent: bool | None = False): dotenv.set_key(env, 'PREFIX', new_prefix) if silent is not True: