catch poll timeout

This commit is contained in:
Flummi 2019-09-14 14:56:56 +00:00
parent 82ab5f7c91
commit bde8819e19

View File

@ -41,8 +41,9 @@ export default class tg extends EventEmitter {
}; };
} }
async poll() { async poll() {
try {
let res = await (await fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)).json(); 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) if (!res.ok)
return this.emit("data", ["error", res.description]); return this.emit("data", ["error", res.description]);
if (res.result.length === 0) if (res.result.length === 0)
@ -64,6 +65,13 @@ export default class tg extends EventEmitter {
return this.emit("data", ["message", this.reply(res.message)]); return this.emit("data", ["message", this.reply(res.message)]);
} }
} }
catch(lel) {
return this.emit("data", ["error", "tg timed out lol"]);
}
finally {
setTimeout(async () => { await this.poll(); }, this.options.pollrate);
}
}
async send(chatid, msg, reply = null) { async send(chatid, msg, reply = null) {
msg = Array.isArray(msg) ? msg.join("\n") : msg; msg = Array.isArray(msg) ? msg.join("\n") : msg;
if (msg.length === 0 || msg.length > 2048) if (msg.length === 0 || msg.length > 2048)