multimsg in tg

This commit is contained in:
Flummi 2022-05-07 12:35:09 +02:00
parent d510b14880
commit 3b75d441f0
3 changed files with 37 additions and 36 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "cuffeo", "name": "cuffeo",
"version": "1.0.8-1", "version": "1.1.0",
"description": "A multi-protocol chatbot library with nearly zero dependencies.", "description": "A multi-protocol chatbot library with nearly zero dependencies.",
"main": "src/index.mjs", "main": "src/index.mjs",
"scripts": {}, "scripts": {},
@ -17,7 +17,7 @@
"author": "Flummi & jkhsjdhjs", "author": "Flummi & jkhsjdhjs",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"flumm-fetch-cookies": "^1.4.0" "flumm-fetch": "^1.0.1"
}, },
"bugs": { "bugs": {
"url": "https://github.com/kein-Bot/cuffeo/issues" "url": "https://github.com/kein-Bot/cuffeo/issues"

View File

@ -1,7 +1,7 @@
import https from "https"; import https from "https";
import url from "url"; import url from "url";
import EventEmitter from "events"; import EventEmitter from "events";
import fetch from "flumm-fetch-cookies"; import fetch from "flumm-fetch";
export default class slack extends EventEmitter { export default class slack extends EventEmitter {
constructor(options) { constructor(options) {

View File

@ -1,4 +1,4 @@
import fetch from "flumm-fetch-cookies"; import fetch from "flumm-fetch";
import EventEmitter from "events"; import EventEmitter from "events";
const allowedFiles = [ 'audio', 'video', 'photo', 'document' ]; const allowedFiles = [ 'audio', 'video', 'photo', 'document' ];
@ -50,44 +50,45 @@ export default class tg extends EventEmitter {
} }
async poll() { async poll() {
try { try {
let res = await (await fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)).json(); const _res = await (await fetch(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`)).json();
if(!res.ok) if(!_res.ok)
throw { type: "tg", message: res.description}; throw { type: "tg", message: _res.description};
if(res.result.length === 0) if(_res.result.length === 0)
return; return;
res = res.result[res.result.length - 1]; this.lastUpdate = _res.result[_res.result.length - 1].update_id + 1;
this.lastUpdate = res.update_id + 1;
if(res.message.date >= ~~(Date.now() / 1000) - 10 && res.message.message_id !== this.lastMessage) { _res.result.forEach(async res => {
this.lastMessage = res.message.message_id; if(res.message.date >= ~~(Date.now() / 1000) - 10 && res.message.message_id !== this.lastMessage) {
if(!this.server.user.has(res.message.from.username || res.message.from.first_name)) { this.lastMessage = res.message.message_id;
this.server.user.set(res.message.from.username || res.message.from.first_name, { if(!this.server.user.has(res.message.from.username || res.message.from.first_name)) {
nick: res.message.from.first_name, this.server.user.set(res.message.from.username || res.message.from.first_name, {
username: res.message.from.username, nick: res.message.from.first_name,
account: res.message.from.id.toString(), username: res.message.from.username,
prefix: `${res.message.from.username}!${res.message.from.id.toString()}@${this.network}`, account: res.message.from.id.toString(),
id: res.message.from.id prefix: `${res.message.from.username}!${res.message.from.id.toString()}@${this.network}`,
}); id: res.message.from.id
} });
try {
let key;
if(key = Object.keys(res.message).filter(t => allowedFiles.includes(t))?.[0]) {
let media = res.message[key];
if(key === 'photo')
media = res.message[key][res.message[key].length - 1];
res.message.media = await this.getFile(media.file_id);
res.message.text = res.message.caption;
delete res.message[key];
} }
} catch {
// no media files
}
this.emit("data", ["message", this.reply(res.message)]); try {
} let key;
if(key = Object.keys(res.message).filter(t => allowedFiles.includes(t))?.[0]) {
let media = res.message[key];
if(key === 'photo')
media = res.message[key][res.message[key].length - 1];
res.message.media = await this.getFile(media.file_id);
res.message.text = res.message.caption;
delete res.message[key];
}
} catch {
// no media files
}
this.emit("data", ["message", this.reply(res.message)]);
}
});
} }
catch(err) { catch(err) {
if(!err.type) if(!err.type)