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

View File

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