JABirchall / NimdaDiscord

An Advanced Discord Bot with a nice plugin system.
GNU Lesser General Public License v3.0
8 stars 2 forks source link

Major Bot Delays #19

Open SilvorMike opened 4 years ago

SilvorMike commented 4 years ago

Does anyone else suffer an issue where there bot is severely delayed?

So at first, my bot ran like a dream. Never a flicker. Now recently, it responds instantly on rebooting the bot for the first few minutes, then the delay between the bot's reply coming through on discord can be up to several hours.

I've noticed other bots don't have this issue. However, I do have a few scripts that run when the bot receives a command. The scripts do run, as some of them are MySQL inserts etc before a reply is sent. The MySQL insert is instant, but the reply on Discord itself can be 3+ hours later. (Sometimes 1hr - usually more).

Has anyone else noticed this or had this issue and rectified?

JABirchallHR commented 4 years ago

That is a huge delay. Are the core commands delayed or have you written your own which are being delayed? Also make sure you are handling any errors with stuff like database queries, HTTP requests and exceptions etc. As these can lock up the bot untill it is restarted (I am trying to look into solving this).

The bot is written in an async manor, so if you send a message to a channel or user and want to access properties from that message, becareful to use then() as it might be possible to access something which does not exist yet or try doing something in a errorous state. Example:

$channel->send('this is message 1')->then(function(Message $message) use ($channel) {
    return $channel->send('this is mesasage 2');
})->otherwise(function($error) {
    print_r($error)
});

Where as if you just do

$channel->send('this is message 1');
$channel->send('this is mesasage 2');

Not only would those messages run the risk of sending in a random order, but if any of them fail it would be essentially unhandled.

Since Nimda is built ontop of React it would be nice to read: https://reactphp.org/promise/ for a better understanding of async php Also make sure you arent being ratelimited by discord: https://discordapp.com/developers/docs/topics/rate-limits#header-format

SilvorMike commented 4 years ago

Thanks so much for replying.

Core commands do it too. The standard welcome message that users get when there is a join event sometimes sends so late the user has already left, so then it sends and comes through as @invalid-user.

I am going to go through and check error handling on each return line etc.

The scripts do work straight away regarding MySQL etc. They add to databases and stuff immediately, it's the response in Discord that takes forever.

JABirchallHR commented 4 years ago

90% of the time its an error with something which is being performed which locks up the bot. If there are any loose strings they can cause big problems.

A good example is in the Dice command: https://github.com/JABirchall/NimdaDiscord/blob/master/Nimda/Core/Commands/Dice.php On line 37, I should really be error checking the message edit, because for any reason the that message could no longer exist. For example if someone where to use the !purge command. Then when the bot has simulated the roll and edits the message, it wont exist and the bot will have a state error unhandled.