0

I created a discord bot using Node.js and the discord.js-commando framework.

One of the features is to create writing sprints, which essentially is a timer, so you could say: I want to write for 20 minutes, starting in 5 minutes. The bot would then wait 5 minutes and start the sprint, then after 20 minutes it notifies the users doing it that it's ended and waits for wordcounts to come in, then posts the results.

This was working fine when the bot was only on one server, but it's been added to several more recently (78 according to the !stats command, though I don't know how many are actively using it), and since then, it's been very erratic.

Sometimes the sprint never starts, sometimes it never ends, sometimes it ends and then after you post your wordcounts, it never posts the final results.

This is my first ever dabbling with Node.js, so I don't know if I'm doing something wrong. I am doing all of the timers with the setTimeout function.

Here is the command file: link to GitHub

As an example, this is the timeout that is set after a user submits their wordcount, if everyone has now submitted their wordcount, so we can display the results:

msg.say('The word counts are in. Results coming up shortly...');
this.finished = 1;

// Clear original timeout
this.clear();

// Set new one
this.messageTimeout = setTimeout(function() {
  obj.finish(msg);
}, 10000);

Where clear is:

clear() {
  clearTimeout(this.messageTimeout);
}

Is there something inherently wrong with doing it this way? I know very little about Node.js... Should I perhaps look at doing a cron every minute instead to process sprints? Or could this be a server issue? I am running it on a free EC2 AWS server, but the reports all look okay, no resources are being used at abnormally high levels.

Thanks.

Federico Grandi
  • 6,785
  • 5
  • 30
  • 50
CMR
  • 1,366
  • 4
  • 15
  • 31
  • Could you clarify your architecture ? Do you have one server that receives all the requests from all clients ? Or every of the 72 servers is only talking to a single chat ? – Danielo515 Nov 05 '18 at 22:22
  • Hi, It's one web server that runs the bot and all the discord servers which add the bot to their server, send their requests to the same server. – CMR Nov 06 '18 at 09:09
  • Then your problem is clearly that you are sharing your state across all the request. Probably you need to use a Map for the different timeouts. I'll try to provide an example later. Meanwhile, if you share a gist that will probably help – Danielo515 Nov 06 '18 at 10:39
  • Hi, i'm not too sure what you mean. This is the only nodejs project I've done. Does it not work like a standard server-client relationship, in which the state of the objects/variables/etc... will be unique to the client request? – CMR Nov 06 '18 at 15:23

0 Answers0