diff --git a/package.json b/package.json index cec5fb9..1b2eb97 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "description": "lul", "main": "index.mjs", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "start": "node --experimental-modules test.mjs" }, "repository": { "type": "git", @@ -13,7 +13,5 @@ "author": "Flummi", "license": "ISC", "dependencies": { - "request": "^2.88.0", - "request-promise-native": "^1.0.5" } } diff --git a/src/clients/tg.mjs b/src/clients/tg.mjs index a5da270..169af31 100644 --- a/src/clients/tg.mjs +++ b/src/clients/tg.mjs @@ -1,5 +1,5 @@ import { getLevel } from "../inc/admin"; -import rp from "request-promise-native"; +import fetch from "../inc/fetch"; import EventEmitter from "events"; export class tg extends EventEmitter { @@ -25,7 +25,8 @@ export class tg extends EventEmitter { } connect() { return new Promise((resolve, reject) => { - rp(`${this.api}/getMe`, { json: true }) + fetch(`${this.api}/getMe`) + .then(res => res.json()) .then(res => { if(res.ok) { this.me = res.result; @@ -48,7 +49,8 @@ export class tg extends EventEmitter { }); } poll() { - rp(`${this.api}/getUpdates?offset=${this.lastUpdate}&allowed_updates=message`, { json:true }) + 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]; @@ -76,17 +78,15 @@ export class tg extends EventEmitter { return false; const opts = { method: 'POST', - uri: `${this.api}/sendMessage`, body: { chat_id: chatid, text: msg, parse_mode: "HTML" - }, - json: true + } }; if(reply) opts.body.reply_to_message_id = reply; - rp(opts) + fetch(`${this.api}/sendMessage`, opts) .then(res => {}) .catch(err => { }); diff --git a/src/inc/fetch.mjs b/src/inc/fetch.mjs new file mode 100644 index 0000000..ea92d48 --- /dev/null +++ b/src/inc/fetch.mjs @@ -0,0 +1,19 @@ +import http from "http"; +import https from "https"; +import url from "url"; + +export default (a, options = {}, link = url.parse(a)) => new Promise((resolve, reject) => { + (link.protocol === "https:"?https:http).get({...{ + hostname: link.hostname, + path: link.path, + method: "GET" + }, ...options}, (res, data = "") => res + .setEncoding("utf8") + .on("data", chunk => data += chunk) + .on("end", () => resolve({ + text: () => data, + json: () => { try { return JSON.parse(data); } catch(err) { return "no json D:"; } }, + buffer: () => new Buffer.from(data) + })) + ).on("error", err => reject(err)); +});