0
votes

I have made a discord bot that has the saves the id of a specific msg on a JSON file and when someone reacts to the msg he sends a message back.
My problem is when I do something which is going to turn the bot off for a period of time (restart, update, change host and more), the bot "forgets" all the messages that were made before he was activated again.

Is there a way to make the bot remember all the messages that happened before he was turned off?
(I tried channel.history loop and the bot did find his previous made message (I tested if the id is the same)
but after I tried to react to the message again, the bot still didn't recognize that a reaction has been made because the message was made before he was turned on.

side note:
There is a work around you can do that instead of trying to remember the message id, you need to remember the channel id that the msg has been sent in as well.
So after you have found your previous message, you can delete it and make a new message.
The thing is, I wanna know if there is a rather easier way to do it or if there is a way to "remember" previous messages.

1

1 Answers

0
votes

So what I would do is I would save the messages into an SQL database and on start up recover all the messages.

Like so:

# Start up (run this code first)
try:
    with open(path, "x") as file: pass
except: pass

global conn, c
conn = sqlite3.connect(path)
c = conn.cursor()

c.execute("""CREATE TABLE IF NOT EXISTS Messages (
             messages string NOT NULL
             )""")

# Gets message history
c.execute("SELECT * FROM Messages")
history = c.fetchall()

This will not work unless you save the messages when you receive them, so add these lines once you receive a message you want to be able to get later

# Saves message
c.execute("INSERT INTO messages VALUES (?)", (message,))
conn.commit()