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 fetch from "../fetch";
|
||||||
import rp from "request-promise-native";
|
|
||||||
|
|
||||||
export default bot => {
|
export default bot => {
|
||||||
bot._trigger.set("irpg", new bot.trigger({
|
bot._trigger.set("irpg", new bot.trigger({
|
||||||
call: /^(\.|\/)irpg/i,
|
call: /^\.irpg/i,
|
||||||
set: "uwe",
|
set: "uwe",
|
||||||
f: e => {
|
f: e => {
|
||||||
const user = e.args[0] || e.user.nick;
|
fetch("https://irpg.zneb.org/db.php")
|
||||||
rp("https://irpg.zneb.org/players.php").then(top => {
|
.then(res => res.text())
|
||||||
let prank = 0
|
.then(res => {
|
||||||
, i = 1;
|
e.args[0] = e.args[0] || e.user.nick;
|
||||||
const dom = new jsdom.JSDOM(top);
|
let players = new Map();
|
||||||
let playerlist = dom.window.document.querySelectorAll("ol > li");
|
res
|
||||||
let player = null;
|
.split("\n").join``
|
||||||
for(let p of playerlist) {
|
.match(/\<tr\>(.*?)\<\/tr\>/gsi)
|
||||||
if (p.innerHTML.includes(`player=${user}">`)) {
|
.map((l, i) => `<td>${i}</td>${l}`.match(/\<td.*?\>(.*?)\<\/td\>/gsi))
|
||||||
prank = i;
|
.map(l => l?l.map(m => m
|
||||||
player = p;
|
.replace("<td>", "")
|
||||||
break;
|
.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(!p)
|
||||||
}
|
return e.reply("user / rang doesn't exist. D:");
|
||||||
if(player !== null) {
|
e.reply(`(${p.rang} / ${players.size}) [b]${p.name}[/b], [b]${p.class}[/b]: level: ${p.level}, next level in ${p.ttl}.`);
|
||||||
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}.`);
|
.catch(err => console.log(err));
|
||||||
}
|
|
||||||
else
|
|
||||||
e.reply(`player [b]${user}[/b] was not found`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
};
|
};
|
Loading…
Reference in New Issue
Block a user