moar async

This commit is contained in:
Flummi 2019-08-20 21:53:43 +00:00
parent b6d3212828
commit d0821b3553
2 changed files with 58 additions and 69 deletions

View File

@ -12,22 +12,25 @@ export default class tg extends EventEmitter {
this.api = `https://api.telegram.org/bot${this.token}`; this.api = `https://api.telegram.org/bot${this.token}`;
this.lastUpdate = 0; this.lastUpdate = 0;
this.lastMessage = 0; this.lastMessage = 0;
this.poller = null;
this.server = { this.server = {
set: this.set, set: this.set,
channel: new Map(), channel: new Map(),
user: new Map(), user: new Map(),
me: {} me: {}
}; };
this.connect().then(() => {
this.poller = setInterval(() => { this.poll(); }, this.options.pollrate); return (async () => {
}); await this.connect();
await this.poll();
return this;
})();
} }
connect() { async connect() {
return new Promise((resolve, reject) => { const res = await (await fetch(`${this.api}/getMe`)).json();
fetch(`${this.api}/getMe`) if (!res.ok)
.then(res => res.json()) throw this.emit("data", ["error", res.description]); // more infos
.then(res => {
if(res.ok) {
this.me = res.result; this.me = res.result;
this.server.me = { this.server.me = {
nickname: res.result.first_name, nickname: res.result.first_name,
@ -36,22 +39,15 @@ export default class tg extends EventEmitter {
prefix: `${res.result.username}!${res.result.id.toString()}`, prefix: `${res.result.username}!${res.result.id.toString()}`,
id: res.result.id.toString() id: res.result.id.toString()
}; };
resolve();
} }
else { async poll() {
reject(); let res = await (await fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)).json();
} setTimeout(async () => { await this.poll(); }, this.options.pollrate);
}) if (!res.ok)
.catch(err => { return this.emit("data", ["error", res.description]);
reject(); if (res.result.length === 0)
}); return;
});
}
poll() {
fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)
.then(res => res.json())
.then(res => {
if(res.ok && res.result.length > 0) {
res = res.result[res.result.length - 1]; res = res.result[res.result.length - 1];
this.lastUpdate = res.update_id + 1; this.lastUpdate = res.update_id + 1;
if (res.message.date >= ~~(Date.now() / 1000) - 10 && res.message.message_id !== this.lastMessage) { if (res.message.date >= ~~(Date.now() / 1000) - 10 && res.message.message_id !== this.lastMessage) {
@ -65,17 +61,12 @@ export default class tg extends EventEmitter {
id: res.message.from.id id: res.message.from.id
}); });
} }
this.emit("data", ["message", this.reply(res.message)]); return this.emit("data", ["message", this.reply(res.message)]);
} }
} }
}) async send(chatid, msg, reply = null) {
.catch(err => {
this.emit("error", err);
});
}
send(chatid, msg, reply = null) {
if (msg.length === 0 || msg.length > 2048) if (msg.length === 0 || msg.length > 2048)
return false; return this.emit("data", ["error", "msg to short or to long lol"]);
const opts = { const opts = {
method: "POST", method: "POST",
body: { body: {
@ -86,14 +77,10 @@ export default class tg extends EventEmitter {
}; };
if (reply) if (reply)
opts.body.reply_to_message_id = reply; opts.body.reply_to_message_id = reply;
fetch(`${this.api}/sendMessage`, opts) await fetch(`${this.api}/sendMessage`, opts);
.then(res => {})
.catch(err => {
this.emit("error", err);
});
} }
sendmsg(mode, recipient, msg) { async sendmsg(mode, recipient, msg) {
this.send(recipient, msg); await this.send(recipient, msg);
} }
reply(tmp) { reply(tmp) {
return { return {

View File

@ -13,7 +13,7 @@ export default class cuffeo extends EventEmitter {
return (async () => { return (async () => {
this.libs = await this.loadLibs(); this.libs = await this.loadLibs();
this.clients = this.registerClients(cfg); this.clients = await this.registerClients(cfg);
return this; return this;
})(); })();
} }
@ -25,16 +25,18 @@ export default class cuffeo extends EventEmitter {
} }
return _libs; return _libs;
} }
registerClients(cfg) { async registerClients(cfg) {
return cfg.filter(e => e.enabled).map(srv => { return cfg.filter(e => e.enabled).map(async srv => {
if(!Object.keys(this.libs).includes(srv.type)) if(!Object.keys(this.libs).includes(srv.type))
throw new Error(`not supported client: ${srv.type}`); throw new Error(`not supported client: ${srv.type}`);
return { const client = {
name: srv.network, name: srv.network,
type: srv.type, type: srv.type,
client: new this.libs[srv.type](srv) client: await new this.libs[srv.type](srv)
}; };
client.client.on("data", ([type, data]) => this.emit(type, data));
return client;
}); });
} }
}; };