irpg overhaul
This commit is contained in:
parent
0c5e00716c
commit
91e4f3404a
35
src/inc/fetch.mjs
Normal file
35
src/inc/fetch.mjs
Normal file
@ -0,0 +1,35 @@
|
||||
import http from "http";
|
||||
import https from "https";
|
||||
import url from "url";
|
||||
import querystring from "querystring";
|
||||
|
||||
const readdata = (res, mode, data = "") => new Promise((resolve, reject) => res
|
||||
.setEncoding("utf8")
|
||||
.on("data", chunk => data += chunk)
|
||||
.on("end", () => {
|
||||
switch(mode) {
|
||||
case "text": resolve(data); break;
|
||||
case "json": try { resolve(JSON.parse(data)); } catch(err) { reject("no json D:"); } break;
|
||||
case "buffer": resolve(new Buffer.from(data)); break;
|
||||
default: reject("lol no"); break;
|
||||
}
|
||||
}));
|
||||
|
||||
export default (a, options = {}, link = url.parse(a), body = "") => new Promise((resolve, reject) => {
|
||||
options = {...{ hostname: link.hostname, path: link.path, method: "GET" }, ...options};
|
||||
if(options.method === "POST") {
|
||||
body = querystring.stringify(options.body);
|
||||
delete options.body;
|
||||
options.headers = {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Content-Length": Buffer.byteLength(body)
|
||||
};
|
||||
}
|
||||
(link.protocol === "https:"?https:http).request(options, res => resolve({
|
||||
body: res,
|
||||
headers: res.headers,
|
||||
text: () => readdata(res, "text"),
|
||||
json: () => readdata(res, "json"),
|
||||
buffer: () => readdata(res, "buffer")
|
||||
})).on("error", err => reject(err)).end(body);
|
||||
});
|
@ -1,33 +1,45 @@
|
||||
import jsdom from "jsdom";
|
||||
import rp from "request-promise-native";
|
||||
import fetch from "../fetch";
|
||||
|
||||
export default bot => {
|
||||
bot._trigger.set("irpg", new bot.trigger({
|
||||
call: /^(\.|\/)irpg/i,
|
||||
call: /^\.irpg/i,
|
||||
set: "uwe",
|
||||
f: e => {
|
||||
const user = e.args[0] || e.user.nick;
|
||||
rp("https://irpg.zneb.org/players.php").then(top => {
|
||||
let prank = 0
|
||||
, i = 1;
|
||||
const dom = new jsdom.JSDOM(top);
|
||||
let playerlist = dom.window.document.querySelectorAll("ol > li");
|
||||
let player = null;
|
||||
for(let p of playerlist) {
|
||||
if (p.innerHTML.includes(`player=${user}">`)) {
|
||||
prank = i;
|
||||
player = p;
|
||||
break;
|
||||
fetch("https://irpg.zneb.org/db.php")
|
||||
.then(res => res.text())
|
||||
.then(res => {
|
||||
e.args[0] = e.args[0] || e.user.nick;
|
||||
let players = new Map();
|
||||
res
|
||||
.split("\n").join``
|
||||
.match(/\<tr\>(.*?)\<\/tr\>/gsi)
|
||||
.map((l, i) => `<td>${i}</td>${l}`.match(/\<td.*?\>(.*?)\<\/td\>/gsi))
|
||||
.map(l => l?l.map(m => m
|
||||
.replace("<td>", "")
|
||||
.replace("<td nowrap>", "")
|
||||
.replace("</td>", "")
|
||||
):false)
|
||||
.splice(1)
|
||||
.forEach(p => players.set(p[1].toLowerCase(), {
|
||||
rang: parseInt(p[0]),
|
||||
name: p[1],
|
||||
level: parseInt(p[2]),
|
||||
class: p[4],
|
||||
ttl: p[5],
|
||||
online: p[7] === "Yes"
|
||||
}));
|
||||
let p = players.has(e.args[0].toLowerCase())?players.get(e.args[0].toLowerCase()):false;
|
||||
let tmp = e.args[0].match(/#\d+/);
|
||||
if(tmp) {
|
||||
tmp = parseInt(tmp[0].slice(1)) - 1;
|
||||
if(tmp >= 0 && tmp <= players.size - 1)
|
||||
p = [...players.entries()][tmp][1];
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if(player !== null) {
|
||||
const [, plevel, pclass, puntil] = player.innerHTML.match(/.*, the level (.*?) (.*?)\. Next level in (.*?)\./);
|
||||
e.reply(`([i]${prank} / ${playerlist.length}[/i]) [b]${user}[/b], the [b]${pclass}[/b]: level: ${plevel}, next level in ${puntil}.`);
|
||||
}
|
||||
else
|
||||
e.reply(`player [b]${user}[/b] was not found`);
|
||||
});
|
||||
if(!p)
|
||||
return e.reply("user / rang doesn't exist. D:");
|
||||
e.reply(`(${p.rang} / ${players.size}) [b]${p.name}[/b], [b]${p.class}[/b]: level: ${p.level}, next level in ${p.ttl}.`);
|
||||
})
|
||||
.catch(err => console.log(err));
|
||||
}
|
||||
}));
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user