From 61fe835ccc4b34352b12d0cdf3e72c6498db9bf9 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Sun, 16 Aug 2020 22:08:39 +0100 Subject: [PATCH] Made setting up modlogs and modmail much easier Using typehints to convert into Channel objects Command specific error handling is now used --- cogs/guild.py | 174 ++++++++++++++++---------------------------------- 1 file changed, 56 insertions(+), 118 deletions(-) diff --git a/cogs/guild.py b/cogs/guild.py index c851aeda..3b52b120 100644 --- a/cogs/guild.py +++ b/cogs/guild.py @@ -19,7 +19,6 @@ import datetime import io import random -import aiomysql import discord from discord import Embed, TextChannel from discord import File @@ -261,23 +260,12 @@ class Guild(Cog): mod_log_setup = True await self.bot.storage_modlog_for_guild(self.bot.db, ctx, user_channel.id, mod_log_setup) - @mlsetup.error - async def mlsetup_command_error(self, ctx, exc): - """Catching error if channel is not recognised""" - - if isinstance(exc, BadArgument): - text = "**Channel Not Detected... Aborting Process**" - await self.bot.generate_embed(ctx, desc=text) - @modlogs.command(name="update") @has_permissions(manage_guild=True) @bot_has_permissions(administrator=True) async def mlupdate(self, ctx, user_channel: TextChannel): """Change the Channel that your Modlogs are Sent to""" - # Retrieve a list of channel id's in the guild - channels = [channel for channel in ctx.guild.channels] - # Setup pool pool = self.bot.db @@ -298,11 +286,6 @@ class Guild(Cog): f"\nDo **{ctx.prefix}help modlogs** to find out more!" await self.bot.generate_embed(ctx, desc=text) - # Abort the process if the channel does not exist within the guild - if user_channel not in channels: - text = "**Invalid ChannelID Detected... Aborting Process**" - await self.bot.generate_embed(ctx, desc=text) - else: # Update the modlog channel within the database and cache mod_log_setup = False @@ -365,15 +348,13 @@ class Guild(Cog): @modmail.command(name="setup") @has_permissions(manage_guild=True) @bot_has_permissions(administrator=True) - async def mmsetup(self, ctx, channelID: int): + async def mmsetup(self, ctx, user_channel: TextChannel, modmail_channel: TextChannel): """ Setup Modmail System - Input the ID of the Channel where the Modmail will be sent + First Argument: Input Channel(Mention or ID) where members can send modmail + Second Argument: Input Channel(Mention or ID) where the members mail should be sent """ - # Retrieve a list of channel id's in the guild - channels = [channel.id for channel in ctx.guild.channels] - # Setup pool pool = self.bot.db @@ -382,7 +363,7 @@ class Guild(Cog): async with conn.cursor() as cur: # Get the author's row from the Members Table select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)""" - val = ctx.author.guild.id, + val = ctx.guild.id, # Execute the SQL Query await cur.execute(select_query, val) @@ -395,87 +376,48 @@ class Guild(Cog): await self.bot.generate_embed(ctx, desc=text) return - # As long as the channel exists within the guild - if channelID in channels: + desc = "React to this message if you want to send a message to the Staff Team!" \ + "\n\n**React with ✅**" \ + "\n\nWe encourage all suggestions/thoughts and opinions on the server!" \ + "\nAs long as it is **valid** criticism." \ + "\n\n\n**Purely negative feedback will not be considered.**" - # Ask for the channel ID that the modmail should be logged to - await ctx.send("**Please enter the ID of the channel you want your modmail to be sent**") + # Set up embed to let the user how to start sending modmail + ModMail = Embed(title="**Welcome to Modmail!**", + description=desc, + colour=self.bot.admin_colour, + timestamp=datetime.datetime.utcnow()) + ModMail.set_thumbnail(url=self.bot.user.avatar_url) - # Check the response is from the author and from the same channel as the previous message - def check(m): - return m.author == ctx.author and m.channel == ctx.channel + modmail_message = await user_channel.send(embed=ModMail) - # Wait for the message from the author - msg = await self.bot.wait_for('message', check=check) + # Auto add the ✅ reaction + await modmail_message.add_reaction('✅') - # As long as the channel exists within the guild - if int(msg.content) in channels: - - desc = "React to this message if you want to send a message to the Staff Team!" \ - "\n\n**React with ✅**" \ - "\n\nWe encourage all suggestions/thoughts and opinions on the server!" \ - "\nAs long as it is **valid** criticism." \ - "\n\n\n**Purely negative feedback will not be considered.**" - - # Set up embed to let the user how to start sending modmail - ModMail = Embed(title="**Welcome to Modmail!**", - description=desc, - colour=self.bot.admin_colour, - timestamp=datetime.datetime.utcnow()) + # Setup up pool connection and cursor + async with pool.acquire() as conn: + async with conn.cursor() as cur: + # Define the insert statement that will insert information about the modmail channel + insert_query = """INSERT INTO moderatormail (guildID, channelID, messageID, modmailChannelID) VALUES (%s, %s, %s, %s)""" + vals = ctx.guild.id, user_channel.id, modmail_message.id, modmail_channel.id, - ModMail.set_thumbnail(url=self.bot.user.avatar_url) + # Execute the SQL Query + await cur.execute(insert_query, vals) + await conn.commit() - try: - # Get the channel object from the channelID input by the user - channel = ctx.author.guild.get_channel(channelID) - modmailchannelID = await channel.send(embed=ModMail) - - # Auto add the ✅ reaction - await modmailchannelID.add_reaction('✅') - - # Setup up pool connection and cursor - async with pool.acquire() as conn: - async with conn.cursor() as cur: - # Define the insert statement that will insert information about the modmail channel - insert_query = """INSERT INTO moderatormail (guildID, channelID, messageID, modmailChannelID) VALUES (%s, %s, %s, %s)""" - vals = ctx.author.guild.id, channelID, modmailchannelID.id, int(msg.content), - - # Execute the SQL Query - await cur.execute(insert_query, vals) - await conn.commit() - - text = "**Modmail System** is successfully set up!" \ - f"\nRefer to **{ctx.prefix}help modmail** for more information" - await self.bot.generate_embed(ctx, desc=text) - return - - except aiomysql.IntegrityError: - text = "**Modmail System** already set up!" \ - f"\nRefer to **{ctx.prefix}help modmail** for more information" - await self.bot.generate_embed(ctx, desc=text) - else: - # Send error message if the channel ID is not recognised - text = "**Invalid ChannelID Detected... Aborting Process**" - await self.bot.generate_embed(ctx, desc=text) - return - else: - # Send error message if the channel ID is not recognised - text = "**Invalid ChannelID Detected... Aborting Process**" + text = "**Modmail System** is successfully set up!" \ + f"\nRefer to **{ctx.prefix}help modmail** for more information" await self.bot.generate_embed(ctx, desc=text) - return @modmail.command(name="update") @has_permissions(manage_guild=True) @bot_has_permissions(administrator=True) - async def mmupdate(self, ctx, channelID: int): + async def mmupdate(self, ctx, user_channel: TextChannel): """ Update the Channel that the Modmail is logged to - Input the ID of the New Channel + You can Mention or use the Channel ID """ - # Retrieve a list of channel id's in the guild - channels = [channel.id for channel in ctx.guild.channels] - # Setup pool pool = self.bot.db @@ -484,7 +426,7 @@ class Guild(Cog): async with conn.cursor() as cur: # Get the author's row from the Members Table select_query = """SELECT * FROM moderatormail WHERE guildID = (%s)""" - vals = ctx.author.guild.id, + vals = ctx.guild.id, # Execute the SQL Query await cur.execute(select_query, vals) @@ -497,36 +439,21 @@ class Guild(Cog): await self.bot.generate_embed(ctx, desc=text) return - # As long as the channel exists within the guild - if channelID in channels: - - try: - - # Setup up pool connection and cursor - async with pool.acquire() as conn: - async with conn.cursor() as cur: - # Define the update statement that will insert information about the modmail channel - update_query = """UPDATE moderatormail SET modmailChannelID = (%s) WHERE guildID = (%s)""" - vals = channelID, ctx.author.guild.id - - # Execute the SQL Query - await cur.execute(update_query, vals) - await conn.commit() - - except aiomysql.Error: - text = "**Something Went Wrong! >:( Try Again Later!**" - await self.bot.generate_embed(ctx, desc=text) + # Setup up pool connection and cursor + async with pool.acquire() as conn: + async with conn.cursor() as cur: + # Define the update statement that will insert information about the modmail channel + update_query = """UPDATE moderatormail SET modmailChannelID = (%s) WHERE guildID = (%s)""" + vals = user_channel.id, ctx.guild.id - # Send confirmation that the channel has been updated - channel = ctx.author.guild.get_channel(channelID) - text = "**Channel Updated**" \ - f"\nNew Modmail will be sent to {channel.mention}" - await self.bot.generate_embed(ctx, desc=text) + # Execute the SQL Query + await cur.execute(update_query, vals) + await conn.commit() - else: - # Send error message if the channel ID is not recognised - text = "**Invalid ChannelID Detected... Aborting Process**" - await self.bot.generate_embed(ctx, desc=text) + # Send confirmation that the channel has been updated + text = "**Channel Updated**" \ + f"\nNew Modmail will be sent to {user_channel.mention}" + await self.bot.generate_embed(ctx, desc=text) @modmail.command(name="delete") @has_permissions(manage_guild=True) @@ -734,6 +661,17 @@ class Guild(Cog): await asyncio.sleep(5) await user_channel.delete() + @mlsetup.error + @mlupdate.error + @mmsetup.error + @mmupdate.error + async def mlsetup_command_error(self, ctx, exc): + """Catching error if channel is not recognised""" + + if isinstance(exc, BadArgument): + text = "**Channel Not Detected... Aborting Process**" + await self.bot.generate_embed(ctx, desc=text) + def setup(bot): bot.add_cog(Guild(bot))