Skip to content
Advertisement

Sqlite table reverting to old data after about a hour inactivity

I made my own counting bot on discord to practice coding. The bot will work fine if messages are sent within about an hour of the last message. After a hour sometimes my bot will work, sometimes won’t and the data on my sqlite table will be reverted to previous data. This causes the bot to think that the number that was just sent is incorrect even though it is correct.

I am unsure how to fix or prevent my table from reverting to old data.

I am hosting my bot on repilt, might be important

@client.event  
async def on_message(message):
 if message.author == client.user:
   return
 guild = message.guild.id
 cursor.execute("SELECT channelname FROM counting WHERE guild = ?", (guild,))
 data = cursor.fetchall()
 if data != []:
   channelname = data[0][0]
   cursor.execute("SELECT currentnumber, lastuser, highscore, watchmessage, channelname FROM counting WHERE guild = ?", (guild,))
   data = cursor.fetchall()
   currentnumber = data[0][0]
   lastuser = data[0][1]
   highscore = data[0][2]
   watchmessage = data[0][3]
   channelname = data[0][4]
   if message.channel.id == channelname:
     try:
       number = int(message.content)
       if len(message.content.split()) == 1:
         if message.author.id != lastuser:
           if number == currentnumber:
             if number == 100:
               check = 'N{HUNDRED POINTS SYMBOL}'
             elif number == 420:
               check = 'N{HERB}'
             elif number > highscore:
               check = '☑️'
             else: 
               check = 'N{WHITE HEAVY CHECK MARK}'
             currentnumber = currentnumber + 1
             lastuser = message.author.id
             watchmessage = message.id
             if number > highscore:
               highscore = number
             cursor.execute("UPDATE counting SET currentnumber = ?, lastuser = ?, highscore = ?, watchmessage = ? WHERE guild = ?", (currentnumber, lastuser, highscore, watchmessage, guild))
             connection.commit()
             await message.add_reaction(check)
           else:
             check = 'N{CROSS MARK}'
             reset = 1
             cursor.execute("UPDATE counting SET currentnumber = ?, lastuser = ?, watchmessage = ? WHERE guild = ?", (reset, reset, reset, guild))
             connection.commit()
             await message.add_reaction(check)
             await message.channel.send("Incorrect number, the next number is 1")
             print("This failed")
         else:
           check = 'N{CROSS MARK}'
           reset = 1
           cursor.execute("UPDATE counting SET currentnumber = ?, lastuser = ?, watchmessage = ? WHERE guild = ?", (reset, reset, reset, guild))
           connection.commit()
           await message.add_reaction(check)
           await message.channel.send("You can't count twice in a row, the next number is 1")
     except:
       pass

Advertisement

Answer

You should not use Repl.it to host your bot.

While this may seem like a nice and free service, it has a lot more caveats than you might think, such as:

  • The machines are super underpowered.

    • This means your bot will lag a lot as it gets bigger.
  • You need to run a webserver alongside your bot to prevent it from being shut off.

    • This isn’t a trivial task, and eats more of the machines power.
  • Repl.it uses an ephemeral file system.

    • This means any file you saved via your bot will be overwritten when you next launch.
  • They use a shared IP for everything running on the service. This one is important – if someone is running a user bot on their service and gets banned, everyone on that IP will be banned. Including you.

Please avoid using repl.it to host your bot. It’s not worth the trouble. https://intuitiveexplanations.com/tech/replit/

Got this from the discord.py discord server. Should help out. I believe the problem you are having is that the bot is being shut off by repl.it

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement