• 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.

EN Reload a script after a certain time

Status
Not open for further replies.

C4undBIER

Member
Hello,

i am trying to build a script which tells me the moon phase and writes an expression in a defined channel. i got my script working, but i want it to refresh/reload every defined time. do i have to build a seperate function to use settimeout?

my script until now is:

registerPlugin({
name: 'Moon',
version: 'IDK anymore',
description: 'MOON!',
author: 'Who cares?',
vars: {
fannel: {
title: 'Channel',
type: 'channel'
}
}
},

function(sinusbot,config) {
var time = new Date();
var day = "DD";
var month = "MM";
var year = "YYYY";
var cname;
var chname;
month =(time.getMonth() + 1);
day = time.getDate();
year = time.getFullYear();
function Moon_phase(year, month, day) {
var g;
var e;

if (month == 1) --day;
else if (month == 2) day += 30;
else // m >= 3
{
day += 28 + (month-2)*3059/100;

// adjust for leap years
if (!(year & 3)) ++day;
if ((year%100) == 0) --day;
}

g = (year-1900)%19 + 1;
e = (11*g + 18) % 30;
if ((e == 25 && g > 11) || e == 24) e++;
return ((((e + day)*6+11)%177)/22 & 7);
}

b = (Moon_phase(year, month, day));

// 0 => New Moon
// 1 => Waxing Crescent Moon
// 2 => Quarter Moon
// 3 => Waxing Gibbous Moon
// 4 => Full Moon
// 5 => Waning Gibbous Moon
// 6 => Last Quarter Moon
// 7 => Waning Crescent Moon

if (b === 4) {
cname = "Howwwwlll!!";
}
else if (b == 3) {
cname = "Arms and legs are getting hairier";
}
else if (b == 5) {
cname = "Back on two feet";
}
else if (b < 3) {
cname = "I swear i am not a werewolf";
}
else if (b> 5) {
cname = "I swear i am not a werewolf";
}
else {
cname = "You are shit at scripting.";
}
chname = "[cspacer]" + cname;
sinusbot.log('Mondphase ist: ' + b);
sinusbot.log("//0/8=New Moon");
sinusbot.log("//4=Full Moon");
sinusbot.log(day + "." + month + "." +year);
var topic = (day + "." + month + "." +year);
sinusbot.channelUpdate((config.fannel), { name: chname , topic: topic});
}
);



thanks for your answers
 

irgendwr

no longer active, "retired" staff member
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
do i have to build a seperate function to use settimeout?
put everything in a function and pass it to setInterval or just pass an anonymous function containing everything
sinusbot.channelUpdate((config.fannel), { name: chname , topic: topic});
also: don't use the old engine when writing new scripts, see the new doc for more info
 

C4undBIER

Member
put everything in a function and pass it to setInterval or just pass an anonymous function containing everything

Well thanks, i did and i works now.

also: don't use the old engine when writing new scripts, see the new doc for more info

Instead of "sinusbot.channelUpdate((config.fannel), { name: chname , topic: topic});" i tried
"config.fannel.setName(chname);" and "config.fannel.setTopic(topic);" or
"fannel.update(channelParams: name(chname), topic(topic));"
Both of them are not working. I can't find a script that uses the config on channels and the new scripting engine and i might just be too dumb too read the doc. Can anybody help here?
 

irgendwr

no longer active, "retired" staff member
is awesome!
V.I.P.
is uber awesome!
Contributor
Insider
Both of them are not working. I can't find a script that uses the config on channels and the new scripting engine and i might just be too dumb too read the doc. Can anybody help here?
.update(...) is a method that can only be used on objects of type channel. The object you are using it on is an integer containing the channel id which hast no own methods.
So to actually get a channel object that you can use you need to import the module backend and use its method getChannelByID(id), the argument id is the int that you get from the config.

Example (in case the text above might not be clear):
Code:
// import module
var backend = require('backend');

// acquire channel object
var channel = backend.getChannelByID(config.channel);

// now you can call methods of the channel object
channel.setName('foo');
channel.setTopic('bar');

// note: I'm not using the update method in this case as it's more simple this way
 
Status
Not open for further replies.
Top