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

View File

@ -98,6 +98,7 @@ export class irc extends EventEmitter {
type: "irc", type: "irc",
network: this.network, network: this.network,
channel: tmp.params[0], channel: tmp.params[0],
channelid: tmp.params[0],
user: Object.assign(this.parsePrefix(tmp.prefix), { user: Object.assign(this.parsePrefix(tmp.prefix), {
account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account, account: this.server.user.geti(this.parsePrefix(tmp.prefix).nick).account,
prefix: tmp.prefix.charAt(0) === ":" ? tmp.prefix.substring(1) : tmp.prefix, 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}`); logger.error(`(${this.network}) ${err.message}`);
}); });
} }
sendmsg(mode, recipient, msg) {
this.send(recipient, msg);
}
reply(tmp) { reply(tmp) {
return { return {
type: "tg", type: "tg",

View File

@ -1,4 +1,14 @@
import lt from "long-timeout"; 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 { export default new class timer {
constructor() { constructor() {
@ -12,21 +22,44 @@ export default new class timer {
m: val => val * 60, // minutes m: val => val * 60, // minutes
s: val => val // seconds s: val => val // seconds
}; };
setTimeout(() => { this.getTimers(); }, 1000);
} }
add(time, fn, begin = ~~(Date.now()/1000)) { add(time, fn, begin = ~~(Date.now()/1000)) {
if(this.regex.test(time)) { return new Promise((resolve, reject) => {
let seconds = 0; if(this.regex.test(time)) {
time.match(/\d+(mon|[smhdwy])/g).forEach(t => { let seconds = 0;
const [,val,mod] = t.match(/(\d+)(mon|[smhdwy])/); time.match(/\d+(mon|[smhdwy])/g).forEach(t => {
seconds += parseInt(this.calc[mod](val)); const [,val,mod] = t.match(/(\d+)(mon|[smhdwy])/);
}) seconds += parseInt(this.calc[mod](val));
const rest = seconds - (begin - ~~(Date.now() / 1000)); })
lt.setTimeout(() => { const rest = seconds - (begin - ~~(Date.now() / 1000));
fn(); lt.setTimeout(() => {
}, rest * 1000); fn();
return `timer eingetragen (${rest}s)`; }, rest * 1000);
} resolve(rest);
else }
return "nope"; 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 timer from "./lib/timer";
import sql from "../../inc/sql";
export default bot => { export default bot => {
bot._trigger.set("timer", new bot.trigger({ bot._trigger.set("timer", new bot.trigger({
@ -9,11 +10,26 @@ export default bot => {
usage: "[b].timer[/b] [i]<delay>[/i] [i]<message>[/i]..." usage: "[b].timer[/b] [i]<delay>[/i] [i]<message>[/i]..."
}, },
f: e => { f: e => {
//e.reply( const t = e.args.shift();
timer.add(e.args.shift(), () => { const msg = e.args.join(" ");
e.reply(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"); });
} }
})); }));
}; };