r/Discord_Bots•
12px

My bot's database keeps just...disappearing?

Hello!

I'm a front-end developer that decided to try my hand at discord bots. I've made a few chat bots before so I'm relatively familiar with command processing and the like, but databases were never my specialty, working with the frontend mainly.


I decided to go Mongo for reliability/stability on the VPS I'm running the bot on, but for some reason every now and again in the past few days of testing, the entire database just...poofs.

I am 0% confident in my entire database connection, so if anybody wanted to give it a look over and tell me if I'm missing something obvious or doing something incredibly dumb, I'd be eternally grateful.

The bot is open source and hosted at https://github.com/12px/chandler

The relevant database files are plugins/state.js and plugins/state.model.js


About the bot

Chandler is very simple and only does a few things, basic message management and the like. However, I'm trying to implement a timezone tracking feature that keeps track of what time it is for users in the server. This feature actually works great, you can do >time and Chandler tells you what time it is for everyone who set a timezone.

However, when the database of user timezones keeps randomly resetting...
And it's the entire database, it seems, not just the zone collection. I also lose all other custom settings I'm saving.

Posted Jul 15, 2019, 2:31 PM UTC

1011
jennasisis•

You might wanna try using a different database system like SQLite3 or PostgreSQL. Mongo isn’t all that webscale and can become slow when working with larger projects, at least in my experience. That said if you want to use Mongo, go ahead. I’ve had plenty of odd tools that people try and steer me away from.

As for your initial problem, I’ll take a look and see what’s the issue.

2
jennasisis•

As I’m looking through I’m noticing a bunch of things,

  1. Format your code, dude. As in use indentations where you can.. ie for loops, if statements, etc.
  2. You’re using the abstract inequality operator (==) to check one value against another. This is generally frowned upon because it can produce some pretty wonky results if not used correctly. I would switch all of those to the strict equality operator (===).
1
jennasisis•

When does this problem occur? After a restart? At random intervals?

1
12pxOP•

I really wanted to go no SQL ideally, but I'm not entirely set on Mongo. I was actually considering lowdb beforehand, but I can't imagine that scaling well either.

Right now, I have a database for every guild, and collections, but someone suggested I have a singular database and collections instead, just adding guildID to all my data. I'm not sure if that would work but I'm willing to give it a shot, I suppose.

Also not sure how that would affect my database just disappearing.

Thanks for taking the time to help though! I appreciate it.

1
sudosussudio•

First thing I'd do is put some logging middleware in there, which would make it easier to pinpoint what part of your codebase is responsible.

​

For fairly simple bots if you are not a DB person, I quite like Firebase.

1
12pxOP•

Yeah, more robust logging is definitely going to be needed.

I like firebase in theory, but I like not spending money even more, heh. I have several VPS's I already pay and budget for, one of which is specifically for random things I do. Right now it's solely running a discord bot, so I might as well keep it all contained.

1
sudosussudio•

If it's not too high traffic, firebase might be free for you. You might also look into Botkit since it contains middleware for databases. https://github.com/brh55/botkit-discord

1
MrPowerGamerBR•

Is your database accessible from outside? Some MongoDB versions when running from the mongod executable binds the server to 0.0.0.0 without any password, allowing anyone to login to the database.

1
12pxOP•

I never configured it not to be, so it could be. The bot isn't in production yet, and is only in one or two of my servers, so people aren't really aware it exists. I don't think it's an outside influence erasing my data, I'm sure it's just my own incompetence.

1
MrPowerGamerBR•

You may never know, there are bots that scans public MongoDB servers to delete everything on it just to sell the "backup" for $$$

To find out if that's the case, go to an open port checker website, put the bot's machine IP address there and check for 3306 (IIRC that's MongoDB default port)

2
[deleted]•

[deleted]

1
[deleted]•

[deleted]

-1