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,70 +12,61 @@ 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, username: res.result.username,
username: res.result.username, account: res.result.id.toString(),
account: res.result.id.toString(), 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(); async poll() {
} let res = await (await fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)).json();
else { setTimeout(async () => { await this.poll(); }, this.options.pollrate);
reject(); if (!res.ok)
} return this.emit("data", ["error", res.description]);
}) if (res.result.length === 0)
.catch(err => { return;
reject();
res = res.result[res.result.length - 1];
this.lastUpdate = res.update_id + 1;
if (res.message.date >= ~~(Date.now() / 1000) - 10 && res.message.message_id !== this.lastMessage) {
this.lastMessage = res.message.message_id;
if (!this.server.user.has(res.message.from.username || res.message.from.first_name)) {
this.server.user.set(res.message.from.username || res.message.from.first_name, {
nick: res.message.from.first_name,
username: res.message.from.username,
account: res.message.from.id.toString(),
prefix: `${res.message.from.username}!${res.message.from.id.toString()}`,
id: res.message.from.id
}); });
}); }
return this.emit("data", ["message", this.reply(res.message)]);
}
} }
poll() { async send(chatid, msg, reply = null) {
fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`) if (msg.length === 0 || msg.length > 2048)
.then(res => res.json()) return this.emit("data", ["error", "msg to short or to long lol"]);
.then(res => {
if(res.ok && res.result.length > 0) {
res = res.result[res.result.length-1];
this.lastUpdate = res.update_id + 1;
if (res.message.date >= ~~(Date.now() / 1000) - 10 && res.message.message_id !== this.lastMessage) {
this.lastMessage = res.message.message_id;
if(!this.server.user.has(res.message.from.username || res.message.from.first_name)) {
this.server.user.set(res.message.from.username || res.message.from.first_name, {
nick: res.message.from.first_name,
username: res.message.from.username,
account: res.message.from.id.toString(),
prefix: `${res.message.from.username}!${res.message.from.id.toString()}`,
id: res.message.from.id
});
}
this.emit("data", ["message", this.reply(res.message)]);
}
}
})
.catch(err => {
this.emit("error", err);
});
}
send(chatid, msg, reply = null) {
if(msg.length === 0 || msg.length > 2048)
return false;
const opts = { const opts = {
method: "POST", method: "POST",
body: { body: {
@ -84,16 +75,12 @@ export default class tg extends EventEmitter {
parse_mode: "HTML" parse_mode: "HTML"
} }
}; };
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

@ -12,8 +12,8 @@ export default class cuffeo extends EventEmitter {
this.libs = {}; this.libs = {};
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;
}); });
} }
}; };