fetch
This commit is contained in:
		@@ -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"
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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 => {
 | 
			
		||||
      });
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										19
									
								
								src/inc/fetch.mjs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								src/inc/fetch.mjs
									
									
									
									
									
										Normal file
									
								
							@@ -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));
 | 
			
		||||
});
 | 
			
		||||
		Reference in New Issue
	
	Block a user