database connectivity for timers

This commit is contained in:
Flummi 2018-03-03 07:12:40 +01:00
parent 703152f170
commit 47faa9dff4
5 changed files with 76 additions and 19 deletions

View File

@ -11,6 +11,7 @@ export class discord extends EventEmitter {
this.options = options || {};
this.token = options.token || null;
this.set = this.options.set || "all";
this.network = "discord";
this.bot = new Discord.Client();
this.bot.login(this.token);
@ -74,6 +75,9 @@ export class discord extends EventEmitter {
break;
}
}
sendmsg(mode, recipient, msg) {
this.bot.channels.get(recipient).send(msg);
}
format(msg) {
if(this.server.spurdo)
msg = spurdo(msg);

View File

@ -98,6 +98,7 @@ export class irc extends EventEmitter {
type: "irc",
network: this.network,
channel: tmp.params[0],
channelid: tmp.params[0],
user: Object.assign(this.parsePrefix(tmp.prefix), {
account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account,
prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix,

View File

@ -100,6 +100,9 @@ export class tg extends EventEmitter {
logger.error(`(${this.network}) ${err.message}`);
});
}
sendmsg(mode, recipient, msg) {
this.send(recipient, msg);
}
reply(tmp) {
return {
type: "tg",

View File

@ -1,4 +1,14 @@
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() {
@ -12,21 +22,44 @@ export default new class timer {
m: val => val * 60, // minutes
s: val => val // seconds
};
setTimeout(() => { this.getTimers(); }, 1000);
}
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";
return new Promise((resolve, reject) => {
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);
resolve(rest);
}
else
reject();
});
}
getTimers() {
sql.any("select * from nxy_timers")
.then(rows => {
if(rows.length === 0)
return;
rows.forEach(r => {
r.target = JSON.parse(r.target);
clients.forEach(client => {
if(client.type.toLowerCase() === r.target.type.toLowerCase()
&& 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);
}
});
});
}).catch(err => console.log(err));
}
}

View File

@ -1,4 +1,5 @@
import timer from "./lib/timer";
import sql from "../../inc/sql";
export default bot => {
bot._trigger.set("timer", new bot.trigger({
@ -9,11 +10,26 @@ export default bot => {
usage: "[b].timer[/b] [i]<delay>[/i] [i]<message>[/i]..."
},
f: e => {
//e.reply(
timer.add(e.args.shift(), () => {
e.reply(e.args.join(" "));
});
//);
const t = e.args.shift();
const msg = e.args.join(" ");
timer.add(t, () => {
e.reply(msg);
}).then(seconds => {
sql.any(
"insert into nxy_timers (mask, target, message, delay, created) values ($1, $2, $3, $4, $5)",
[
e.user.prefix,
JSON.stringify({
type: e.type,
network: e.network,
channel: e.channelid
}),
msg,
t,
~~(Date.now() / 1000)
]
);
}).catch(err => { e.reply("error lol"); });
}
}));
};