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

View File

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