From c99e6053815904ceffd9a2f43b624b9c7430f365 Mon Sep 17 00:00:00 2001 From: sgoudham Date: Thu, 9 Jul 2020 23:40:03 +0100 Subject: [PATCH] Adding error handling for modmail logging --- main.py | 92 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 48 insertions(+), 44 deletions(-) diff --git a/main.py b/main.py index fabfd10a..52e102b1 100644 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ import asyncio import discord import mariadb from decouple import config -from discord import Embed +from discord import Embed, DMChannel from discord.ext import commands import db @@ -33,49 +33,53 @@ async def on_message(message): if message.author.bot: return - # Set up the connection to the database - conn = db.connection() - - # With the connection - with conn: - - # Make sure that mariaDB errors are handled properly - try: - msg_name = message.author.name - msg_discrim = message.author.discriminator - time = message.created_at - - # Get: - guild_id = message.guild.id # Guild of the message - msg_time = time.strftime('%Y-%m-%d %H:%M:%S') # Time of the Message - msg_author = f"{msg_name}#{msg_discrim}" # DiscordID - msg_content = f"{message.content}" # Content of the message - - # Store the variables - val = guild_id, msg_time, msg_author, msg_content - - attach = "" - # If an attachment (link) has been sent - if message.attachments: - # Loop through all attachments - for attachment in message.attachments: - # Get the message content and the link that was used - attach += f"Message: {message.content} Link: {attachment.url}" - # Define the new variables to send - val = guild_id, msg_time, msg_author, attach - else: - pass - - # Define the Insert Into Statement inserting into the database - insert_query = """INSERT INTO messages (guildID, messageTime, discordID, messageContent) VALUES (?, ?, ?, ?)""" - cursor = conn.cursor() - # Execute the SQL Query - cursor.execute(insert_query, val) - conn.commit() - print(cursor.rowcount, "Record inserted successfully into Logs") - - except mariadb.Error as ex: - print("Parameterized Query Failed: {}".format(ex)) + if not isinstance(message.channel, DMChannel): + return + else: + + # Set up the connection to the database + conn = db.connection() + + # With the connection + with conn: + + # Make sure that mariaDB errors are handled properly + try: + msg_name = message.author.name + msg_discrim = message.author.discriminator + time = message.created_at + + # Get: + guild_id = message.guild.id # Guild of the message + msg_time = time.strftime('%Y-%m-%d %H:%M:%S') # Time of the Message + msg_author = f"{msg_name}#{msg_discrim}" # DiscordID + msg_content = f"{message.content}" # Content of the message + + # Store the variables + val = guild_id, msg_time, msg_author, msg_content + + attach = "" + # If an attachment (link) has been sent + if message.attachments: + # Loop through all attachments + for attachment in message.attachments: + # Get the message content and the link that was used + attach += f"Message: {message.content} Link: {attachment.url}" + # Define the new variables to send + val = guild_id, msg_time, msg_author, attach + else: + pass + + # Define the Insert Into Statement inserting into the database + insert_query = """INSERT INTO messages (guildID, messageTime, discordID, messageContent) VALUES (?, ?, ?, ?)""" + cursor = conn.cursor() + # Execute the SQL Query + cursor.execute(insert_query, val) + conn.commit() + print(cursor.rowcount, "Record inserted successfully into Logs") + + except mariadb.Error as ex: + print("Parameterized Query Failed: {}".format(ex)) # Processing the message await client.process_commands(message)