0

So I'm trying to build a discord modmail bot. Whenever someone sends a message, it checks whether the message was sent in a private dm_channel to the bot. Then, if it was, it passes on the mod mail to the "mod-channel" text channel in the server, and it logs the modmail submitter's details (username, id, the modmail itself etc) to a mysql database hosted on my laptop, heres the python code:

connection = mysql.connector.connect(
    host = "127.0.0.1",
    port="3306",
    user="root",
    password="pretendpasswordishere",
    database="test"
)
cursor = connection.cursor()

@bot.event
async def on_message(message : discord.Message):
    mod_channel = get(bot.get_all_channels(), name="mod-channel")
    if message.author == bot.user:
        return
    if message.guild is None: # If the author of the message was a discord.Member, then that means the message was sent in a guild, otherwise the message was sent in a private channel, which is what we want (According to the api reference)
        author : discord.Member= get(bot.get_all_members(), id = message.author.id)
        if(message.channel is author.dm_channel and "(MAIL)" in message.content): # If bot was messaged in the dm_channel with the user who messaged the bot and if the message starts of with "(MAIL)"    
            msg_sent : discord.Message = await mod_channel.send(author.mention + " with ID `" + str(author.id) + "` sent this message to the modmail: \"`" + message.content +"`\".") # Says discord username then discord ID of the user then says what they sent to the Bot.
            await author.send("Your mail has been passed on to the mods!")
            await msg_sent.add_reaction("\U00002705")
            cursor.execute("INSERT INTO modmails_table VALUES('" + author.display_name +"'," + str(author.discriminator) + "," + str(author.id) + ",'" + message.content + "'," + '0);') # 0 is considered false (last param is a boolean which accounts whether the modmail "issue" has been resolved. Ofcourse its not resolved initially so we set it to false at first).
    else:
        await bot.process_commands(message)

Yes this not all the code, but the rest is irrelevant to putting the data in a MySQL database.

The table was created like this (MySQL):

CREATE TABLE modmails_table(
    mail_submitter_username VARCHAR(32),
    mail_submitter_discriminator INT,
    mail_submitter_id BIGINT,
    mail_message VARCHAR(1000),
    mail_report_resolved BOOL,
    PRIMARY KEY(mail_submitter_id)
);

Whenever I send in a modmail (to test), it all functions correctly and I get absolutely no errors in terminal (I used to get SQL syntax errors at the cursor.execute() line, but I fixed them). However when I run the query:

SELECT * FROM modmails_table;

It returns the table with 0 results.

My MySQL Server version is 5.7.

My MySQL Python Connector version is 8.0.

Why does it return an empty table when it's supposed to have my account details and modmail in the table?

AtinChing
  • 1
  • 1
  • 2
  • Don't use string concatenation to create SQL queries. Put placeholders in the SQL and send the values as a tuple. – Barmar Apr 04 '21 at 14:26
  • 1
    You're missing `connection.commit()` – Barmar Apr 04 '21 at 14:29
  • You need to save it. Let's supposed you call your connection to the database conn. You need to commit the code like conn.commit() after your insert. – Hani Apr 04 '21 at 14:30

0 Answers0