Added Error Handling

Added Permissions Check
Adding Updating ChannelID
pull/8/head
sgoudham 4 years ago
parent 14a36158b3
commit 1a8803dbd9

@ -1,8 +1,9 @@
import datetime import datetime
import mariadb
from discord import Embed from discord import Embed
from discord.ext import commands from discord.ext import commands
from discord.ext.commands import command, cooldown, BucketType from discord.ext.commands import command, cooldown, BucketType, has_permissions
import db import db
from settings import enso_embedmod_colours, blank_space from settings import enso_embedmod_colours, blank_space
@ -14,19 +15,40 @@ class SetupModmail(commands.Cog):
self.bot = bot self.bot = bot
@command(name="mmsetup") @command(name="mmsetup")
@has_permissions(manage_messages=True, manage_roles=True, manage_channels=True)
@cooldown(1, 1, BucketType.user) @cooldown(1, 1, BucketType.user)
async def _setup(self, ctx, *args): async def _setup(self, ctx, *args):
"""Allows the bot to setup a channel for users to react to for sending modmail""" """Allows the bot to setup a channel for users to react to for sending modmail"""
# Make sure the first two arguments are set and modmail # Retrieve a list of channel id's in the guild
channels = [channel.id for channel in ctx.guild.channels]
# Checking if the guild already exists within the database
with db.connection() as conn:
# Get the author's row from the Members Table
select_query = """SELECT * FROM moderatormail WHERE guildID = (?)"""
val = ctx.author.guild.id,
cursor = conn.cursor()
# Execute the SQL Query
cursor.execute(select_query, val)
result = cursor.fetchone()
# Throw error if the guild already exists and then stop the function
if result[0] is not None:
await ctx.send("Looks like this guild already has a modmail system set up!"
"\nPlease check `~help` for information on how to update/delete existing information")
return
# Allows the user to setup modmail for the first time
if args[0] == "set": if args[0] == "set":
if args[1] == "modmail": if args[1] == "modmail":
# Retrieve a list of channel id's in the guild # As long as the channel exists within the guild
channels = [channel.id for channel in ctx.guild.channels] if int(args[2]) in channels:
# Ask for the channel ID that the modmail should be logged to # 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`") await ctx.send("**Please enter the ID of the channel you want your modmail to be sent**")
# Check the response is from the author and from the same channel as the previous message # Check the response is from the author and from the same channel as the previous message
def check(m): def check(m):
@ -57,6 +79,7 @@ class SetupModmail(commands.Cog):
for name, value, inline in fields: for name, value, inline in fields:
ModMail.add_field(name=name, value=value, inline=inline) ModMail.add_field(name=name, value=value, inline=inline)
try:
# Get the channel object from the channelID input by the user # Get the channel object from the channelID input by the user
channel = ctx.author.guild.get_channel(int(args[2])) channel = ctx.author.guild.get_channel(int(args[2]))
modmailchannelID = await channel.send(embed=ModMail) modmailchannelID = await channel.send(embed=ModMail)
@ -72,11 +95,58 @@ class SetupModmail(commands.Cog):
# Execute the SQL Query # Execute the SQL Query
cursor.execute(insert_query, vals) cursor.execute(insert_query, vals)
except mariadb.IntegrityError as err:
print(err)
await ctx.send("Looks like this guild already has a modmail system set up!"
"\nPlease check `~help` for information on how to update/delete existing information")
else:
# Send error message if the channel ID is not recognised
await ctx.send("`Invalid Channel ID. Aborting Process...`")
return
else: else:
# Send error message if the channel ID is not recognised # Send error message if the channel ID is not recognised
await ctx.send("`Invalid Channel ID. Aborting Process...`") await ctx.send("`Invalid Channel ID. Aborting Process...`")
return return
# Allows the user to update the channel that the modmail can be sent to
if args[0] == "update":
if args[1] == "modmail":
# As long as the channel exists within the guild
if int(args[2]) in channels:
try:
# Store the information within the database
with db.connection() as conn:
# Define the insert statement that will insert information about the modmail channel
update_query = """UPDATE moderatormail SET modmailChannelID = (?) WHERE guildID = (?)"""
vals = args[2], ctx.author.guild.id
cursor = conn.cursor()
# Execute the SQL Query
cursor.execute(update_query, vals)
conn.commit()
except mariadb.Error as err:
print(err)
await ctx.send("Looks like something went wrong during the update!"
"\nMake sure that the Channel ID is correct!")
channel = ctx.author.guild.get_channel(int(args[2]))
await ctx.send(
f"**The channel has been updated! Your new modmail will be sent to** {channel.mention}")
else:
# Send error message if the channel ID is not recognised
await ctx.send("`Invalid Channel ID. Aborting Process...`")
return
# Allows the user to completely erase the modmail system currently set up
if args[0] == "delete":
if args[1] == "modmail":
pass
def setup(bot): def setup(bot):
bot.add_cog(SetupModmail(bot)) bot.add_cog(SetupModmail(bot))

Loading…
Cancel
Save