From 9f7f571048504b5249ee2aab48f610fa29ebe3bd Mon Sep 17 00:00:00 2001 From: sgoudham Date: Thu, 9 Jul 2020 19:27:25 +0100 Subject: [PATCH] Logging Messages In Every Guild --- main.py | 86 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/main.py b/main.py index a461cfda..dadf01b1 100644 --- a/main.py +++ b/main.py @@ -1,10 +1,12 @@ import asyncio import discord +import mariadb from decouple import config from discord import Embed from discord.ext import commands +import db import settings from settings import blank_space, enso_embedmod_colours, time, enso_guild_ID @@ -31,6 +33,49 @@ 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 USERLOGS (guildID, messageTime, discordID, messageContent) VALUES (?, ?, ?, ?)""" + cursor = conn.cursor() + # Execute the SQL Query + cursor.execute(insert_query, val) + 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) @@ -162,48 +207,7 @@ def write_to_dm_file(time, author, content): - # 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 USERLOGS (guildID, messageTime, discordID, messageContent) VALUES (?, ?, ?, ?)"""""" - cursor = conn.cursor() - # Execute the SQL Query - cursor.execute(insert_query, val) - print(cursor.rowcount, "Record inserted successfully into Logs") - - except mariadb.Error as ex: - print("Parameterized Query Failed: {}".format(ex))