timer, wip

This commit is contained in:
Flummi 2018-03-03 02:40:07 +01:00
parent 3bc2aa2b44
commit 14d5bdb183
4 changed files with 54 additions and 1 deletions

View File

@ -12,6 +12,7 @@
"dependencies": {
"discord.js": "^11.3.0",
"jsdom": "^11.4.0",
"long-timeout": "^0.1.1",
"pg-promise": "^7.3.2",
"request": "^2.83.0",
"request-promise": "^4.2.2",

View File

@ -14,6 +14,7 @@ import quotes from "./quotes";
import rape from "./rape";
import sandbox from "./sandbox";
import soundcloud from "./soundcloud";
import timer from "./timer";
import urban from "./urban";
import nxy from "./useless_nxy";
import uwe from "./useless_uwe";
@ -23,5 +24,5 @@ export default [
cfg, chatbot, coins, cookie, core, debug,
drugs, help, kernel, lastfm, mcmaniac,
parser, quotes, rape, sandbox, soundcloud,
urban, nxy, uwe, wttr
timer, urban, nxy, uwe, wttr
];

View File

@ -0,0 +1,32 @@
import lt from "long-timeout";
export default new class timer {
constructor() {
this.regex = /\d+[smhdwy]|mon/;
this.calc = {
y: val => val * 365 * 24 * 60 * 60, // years
mon: val => val * 30 * 24 * 60 * 60, // months
w: val => val * 7 * 24 * 60 * 60, // weeks
d: val => val * 24 * 60 * 60, // days
h: val => val * 60 * 60, // hours
m: val => val * 60, // minutes
s: val => val // seconds
};
}
add(time, fn, begin = ~~(Date.now()/1000)) {
if(this.regex.test(time)) {
let seconds = 0;
time.match(/\d+(mon|[smhdwy])/g).forEach(t => {
const [,val,mod] = t.match(/(\d+)(mon|[smhdwy])/);
seconds += parseInt(this.calc[mod](val));
})
const rest = seconds - (begin - ~~(Date.now() / 1000));
lt.setTimeout(() => {
fn();
}, rest * 1000);
return `timer eingetragen (${rest}s)`;
}
else
return "nope";
}
}

19
src/inc/trigger/timer.mjs Normal file
View File

@ -0,0 +1,19 @@
import timer from "./lib/timer";
export default bot => {
bot._trigger.set("timer", new bot.trigger({
call: /^(\.|\/)timer .*/i,
set: "nxy",
help: {
text: "(WIP) Sets a timer, delay can be: s, m, h, d, w, mon, y",
usage: "[b].timer[/b] [i]<delay>[/i] [i]<message>[/i]..."
},
f: e => {
//e.reply(
timer.add(e.args.shift(), () => {
e.reply(e.args.join(" "));
});
//);
}
}));
};