• If you need help or want to discuss things, you now can also join us on our Discord Server!
  • A first preview of the unlimited version of SinusBot can be found in the Upcoming Changes thread. A version for Windows will follow, but we don't have a release date, yet.

script that disconnects when bot is alone

flynns82

Member
Hi,
I need a script that, when the bot remains alone or is moved to a channel where there is only him, if he stays there for x hours, he stops and disconnects.
PREMISE: I know some programming language (medium-low knowledge), but javascript absolutely nothing, this said, I tried to do something with what I found on the internet but I still could not make anything work.
someone could tell me what there is it wrong in the code? and how to fix it?
or, alternatively, if it is completely wrong, to do it?(if it is not too much a waste of time)
Thanks so much!

JavaScript:
registerPlugin({
    name: 'LogoutWhenAlone',
    version: '1.1',
    description: 'This script disconnect the bot when it is alone by x time.',
    author: 'Marco Canazza <[email protected]>',
    vars: [{
        name: 'mode',
        title: 'Set Milliseconds',
        type: 'number'
    }]
}, function(sinusbot, config) {
    var engine = require('engine'),
        backend = require('backend'),
        event = require('event'),
        msg = config.message ;
 
    event.on('clientMove', function(ev) {
        if (backend.getCurrentChannel().getClientCount() <= 1 ) {
        setTimeout(function(){
            if (backend.getCurrentChannel().getClientCount() <= 1 ) {
                disconnect();
                }
            }, msg );
        }
    });
});
 

Tuetchen

Diesmon Dominator
is awesome!
Contributor
Insider
several missing 'vars' and 'disconnect' is a function of Backend not a global function. Else it should work
 

Tuetchen

Diesmon Dominator
is awesome!
Contributor
Insider
.....
.....
Backend.disconnect() instead of disconnect() ?
 

Diesmon

Tuetchen Dominator
is awesome!
Contributor
Insider
your timeout will also not work as expected. The timeout duration (the var msg) is undefined, because you are pushing the config.message into it. But your only config variable you got ist config.mode
 

Tuetchen

Diesmon Dominator
is awesome!
Contributor
Insider
ye replace that msg = config.message ; with var msg = (+config.mode || 0) ;
 

cakemasher

Well-Known Member
Contributor
As stated by others, your timeout prorably wont work because the timeout you're trying to set, has a undefined amount of miliseconds. As teutchen stated, replace
Code:
msg = config.message;
to
Code:
msg = config.mode || 3600000;
placing the set config into the message var, and enter 3600000 (1 hour) if the configuration isn't set.

Also, maybe a tip, create an empty var to place your timeout in. for example, add to your var list:
Code:
 var my_timeout;
basically creating an empty variable. Then, replace
Code:
setTimeout(...
to
Code:
my_timeout = setTimeout(...

The timeout is now running, where the my_timeout has a reference to the memory block where the timer is running. This is very usefull in case you need to clear the timeout. Clearing the timeout could be usefull for you, because you might want to clear the timeout if someone rejoins the channel after the bot was alone for a while. Currently you're checking this in the timeout function, but this approach is not realy efficient. When a client leaves the bot alone in a channel, rejoins and repeat this action 100 times, your bot will create 100 different timeout's.

You got an if statement where you're checking if the bot is alone. If this is true, you set the timeout. Here you can add an 'else' statement, which is executed when the bot is not alone. Use the clearTimeout function within this else statement, like this:
Code:
} else {
   clearTimeout (my_timeout);
}

This way the timeout will be cleared the moment someone (re)joines the channel.

Maybe a hint, check the 'Instance Log' in the SinusBot web interface. Here you can see any javascript error's your script throws after it was initialized.
 
Last edited:

flynns82

Member
I think I understand, thank you very much in the meantime, as soon as I solve the problems with the bot, I try to fix this script
 
Top