reject negative numbers :^)

This commit is contained in:
Flummi 2018-03-05 07:52:27 +01:00
parent 47faa9dff4
commit d830c7b104
2 changed files with 20 additions and 19 deletions

View File

@ -2,17 +2,9 @@ import lt from "long-timeout";
import sql from "../../../inc/sql";
import { clients } from "../../wrapper";
/*setTimeout(() => {
clients.forEach(client => {
if(client.type === "discord") {
client.client.sendmsg("", "417396055592665098", "TEST");
}
});
}, 2000);*/
export default new class timer {
constructor() {
this.regex = /\d+[smhdwy]|mon/;
this.regex = /(\d+)(mon|[smhdwy])/;
this.calc = {
y: val => val * 365 * 24 * 60 * 60, // years
mon: val => val * 30 * 24 * 60 * 60, // months
@ -22,6 +14,7 @@ export default new class timer {
m: val => val * 60, // minutes
s: val => val // seconds
};
this._timers = new Set();
setTimeout(() => { this.getTimers(); }, 1000);
}
add(time, fn, begin = ~~(Date.now()/1000)) {
@ -31,15 +24,20 @@ export default new class timer {
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));
});
if(seconds > 0) {
const rest = seconds - (~~(Date.now() / 1000) - begin);
lt.setTimeout(() => {
fn();
}, rest * 1000);
this._timers.add(rest);
resolve(rest);
}
else
reject();
}
else
reject();
});
}
getTimers() {
@ -54,9 +52,8 @@ export default new class timer {
&& client.client.network.toLowerCase() === r.target.network.toLowerCase())
{
this.add(r.delay, () => {
client.client.sendmsg("normal", r.target.channel, r.message);
//sql.any("delete from nxy_timers where id = $1", [ r.id ]);
}, r.created);
client.client.sendmsg("normal", r.target.channel, client.client.format(`[b]${r.target.nick}[/b]: ${r.message} [i](${r.delay})[/i]`));
}, r.created).catch(err => {});
}
});
});

View File

@ -12,14 +12,18 @@ export default bot => {
f: e => {
const t = e.args.shift();
const msg = e.args.join(" ");
if(t === "debug" && e.user.level.level >= 100)
return e.reply( JSON.stringify([...timer._timers]) );
timer.add(t, () => {
e.reply(msg);
e.reply(`[b]${e.user.nick}[/b]: ${msg} [i](${t})[/i]`);
}).then(seconds => {
sql.any(
"insert into nxy_timers (mask, target, message, delay, created) values ($1, $2, $3, $4, $5)",
[
e.user.prefix,
JSON.stringify({
nick: e.user.nick,
type: e.type,
network: e.network,
channel: e.channelid