forgot to build

This commit is contained in:
2022-02-13 21:42:04 -06:00
parent f5ab2b4297
commit 8503f274bd
7 changed files with 248 additions and 245 deletions

66
dist/api.js vendored
View File

@@ -23,6 +23,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPlayerInteraction = exports.getPlayer = void 0;
/*eslint prefer-const: "error"*/
const cheerio = __importStar(require("cheerio"));
const discord_js_1 = require("discord.js");
const https_1 = __importDefault(require("https"));
@@ -73,25 +74,25 @@ function getHTML(name) {
* @throws errorMessage class if the request fails
*/
function readHTML(html) {
let $ = cheerio.load(html);
let metaElems = $('meta').toArray(), foundData = {
name: "",
id: "",
avatar: "",
level: "",
rank: "",
const $ = cheerio.load(html), foundData = {
name: '',
id: '',
avatar: '',
level: '',
rank: '',
elo: null,
class: null,
battles: "",
wins: "",
winrate: ""
battles: '',
wins: '',
winrate: ''
};
let metaElems = $('meta').toArray();
//filter down to just ones named "og:..."
metaElems = metaElems.filter(el => el.attribs.property?.startsWith('og:'));
metaElems.forEach(el => {
let attr = el.attribs;
const attr = el.attribs;
if (attr.property === 'og:title') {
let data = uniteApiRegex.ogtitle.exec(attr.content);
const data = uniteApiRegex.ogtitle.exec(attr.content);
if (data !== null && data.length >= 3) {
foundData.name = data[1];
foundData.id = data[2];
@@ -99,14 +100,15 @@ function readHTML(html) {
}
else if (attr.property === 'og:description') {
//all lines
let lines = attr.content.split('\n').map(l => l.trim()), extraLines = [];
let lines = attr.content.split('\n').map(l => l.trim());
const extraLines = [];
//ensure first line is correct
while (lines.length && !/pok.mon unite/i.test(lines[0])) {
let line = lines.shift();
if (line)
while ((lines.length > 0) && !/pok.mon unite/i.test(lines[0])) {
const line = lines.shift();
if (line !== undefined)
extraLines.push(line);
}
if (!lines.length)
if (lines.length === 0)
throw (0, util_1.emsg)('Unable to read data, please try again');
//bring the first lines removed back into the data
lines = [
@@ -116,10 +118,9 @@ function readHTML(html) {
//first line
{
//will be only text after "pokemon unite:"
let line = lines[0].split(':').slice(1).join(':').trim();
let regex = uniteApiRegex.ogdescription;
const line = lines[0].split(':').slice(1).join(':').trim(), regex = uniteApiRegex.ogdescription;
if (regex[0].test(line)) { //is master/has elo
let regexData = line.match(regex[0]);
const regexData = line.match(regex[0]);
if (!regexData || regexData.length < 4)
throw (0, util_1.emsg)('Unable to read data, please try again');
foundData.level = regexData[1];
@@ -127,7 +128,7 @@ function readHTML(html) {
foundData.elo = regexData[3];
}
else { //is not master/has a class
let regexData = line.match(regex[1]);
const regexData = line.match(regex[1]);
if (!regexData || regexData.length < 4)
throw (0, util_1.emsg)('Unable to read data, please try again');
foundData.level = regexData[1];
@@ -138,7 +139,7 @@ function readHTML(html) {
lines.shift();
//rest of lines
lines.forEach(line => {
let split = line.split(':').map(l => l.trim()), key = split[0].toLowerCase().replace(/[^\w]/g, ''), value = split[1];
const split = line.split(':').map(l => l.trim()), key = split[0].toLowerCase().replace(/[^\w]/g, ''), value = split[1];
switch (key) {
case 'battles':
foundData.battles = value;
@@ -153,7 +154,8 @@ function readHTML(html) {
});
}
});
foundData.avatar = $('.player-card-image img').attr('src') || "";
const imgSrc = $('.player-card-image img').attr('src');
foundData.avatar = imgSrc !== undefined ? imgSrc : '';
foundData.avatar = foundData.avatar.replace('../', 'https://uniteapi.dev/');
return foundData;
}
@@ -163,7 +165,7 @@ function readHTML(html) {
* @returns boolean, valid or invalid
*/
function verifyData(data) {
if (data.id.length)
if (data.id.length > 0)
return true;
return false;
}
@@ -173,21 +175,25 @@ function verifyData(data) {
* @returns player data
*/
async function getPlayer(name) {
let html = await getHTML(name);
let data = readHTML(html);
const html = await getHTML(name), data = readHTML(html);
if (verifyData(data))
return data;
return null;
}
exports.getPlayer = getPlayer;
async function sendPlayerEmbed(interaction, data) {
let embed = new discord_js_1.MessageEmbed()
let eloStr;
if (data.elo !== null)
eloStr = `(${data.elo})`;
else
eloStr = `Class ${data.class}`;
const embed = new discord_js_1.MessageEmbed()
.setTitle(`${data.name} (${data.id})`)
.setURL(`https://uniteapi.dev/p/${encodeURIComponent(data.name)}`)
.setTimestamp()
.setThumbnail(data.avatar)
.setDescription(`Level ${data.level}
${data.rank} ${data.elo ? `(${data.elo})` : `Class ${data.class}`}
${data.rank} ${eloStr}
**Battles** ${data.battles}
**Wins** ${data.wins}
@@ -200,9 +206,9 @@ ${data.rank} ${data.elo ? `(${data.elo})` : `Class ${data.class}`}
* @throws errorMessage class if the user cannot be found
*/
async function getPlayerInteraction(interaction) {
let username = interaction.options.getString('username', true);
const username = interaction.options.getString('username', true);
await interaction.deferReply();
let data = await getPlayer(username);
const data = await getPlayer(username);
if (data === null)
throw (0, util_1.emsg)('api.noUser');
else