improved messages and general cleanup

This commit is contained in:
2022-01-31 19:33:39 -06:00
parent 00d04c787d
commit fe866050d5
10 changed files with 383 additions and 228 deletions

34
dist/api.js vendored
View File

@@ -24,7 +24,9 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPlayerInteraction = exports.getPlayer = void 0;
const cheerio = __importStar(require("cheerio"));
const discord_js_1 = require("discord.js");
const https_1 = __importDefault(require("https"));
const util_1 = require("./util");
const uniteApiRegex = {
//$1 = name, $2 = id
ogtitle: /unite api - (.+) \((.*)\)/i,
@@ -68,11 +70,14 @@ function getHTML(name) {
* interprets the html from getHTML()
* @param name name of player
* @returns player data from site
* @throws errorMessage class if the request fails
*/
function readHTML(html) {
let metaElems = cheerio.load(html)('meta').toArray(), foundData = {
let $ = cheerio.load(html);
let metaElems = $('meta').toArray(), foundData = {
name: "",
id: "",
avatar: "",
level: "",
rank: "",
elo: null,
@@ -102,7 +107,7 @@ function readHTML(html) {
extraLines.push(line);
}
if (!lines.length)
throw 'Unable to read data, please try again';
throw (0, util_1.emsg)('Unable to read data, please try again');
//bring the first lines removed back into the data
lines = [
...lines,
@@ -116,7 +121,7 @@ function readHTML(html) {
if (regex[0].test(line)) { //is master/has elo
let regexData = line.match(regex[0]);
if (!regexData || regexData.length < 4)
throw 'Unable to read data, please try again';
throw (0, util_1.emsg)('Unable to read data, please try again');
foundData.level = regexData[1];
foundData.rank = regexData[2];
foundData.elo = regexData[3];
@@ -124,7 +129,7 @@ function readHTML(html) {
else { //is not master/has a class
let regexData = line.match(regex[1]);
if (!regexData || regexData.length < 4)
throw 'Unable to read data, please try again';
throw (0, util_1.emsg)('Unable to read data, please try again');
foundData.level = regexData[1];
foundData.rank = regexData[2];
foundData.class = regexData[3];
@@ -148,6 +153,8 @@ function readHTML(html) {
});
}
});
foundData.avatar = $('.player-card-image img').attr('src') || "";
foundData.avatar = foundData.avatar.replace('../', 'https://uniteapi.dev/');
return foundData;
}
/**
@@ -173,17 +180,32 @@ async function getPlayer(name) {
return null;
}
exports.getPlayer = getPlayer;
async function sendPlayerEmbed(interaction, data) {
let 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}`}
**Battles** ${data.battles}
**Wins** ${data.wins}
**Win Rate** ${data.winrate}`);
await interaction.editReply({ embeds: [embed] });
}
/**
* calls getPlayer() with the name from the interaction
* @param interaction discord interaction
* @throws errorMessage class if the user cannot be found
*/
async function getPlayerInteraction(interaction) {
let username = interaction.options.getString('username', true);
await interaction.deferReply();
let data = await getPlayer(username);
if (data === null)
await interaction.editReply('Unable to find user');
throw (0, util_1.emsg)('Unable to find user');
else
await interaction.editReply('```\n' + JSON.stringify(data, null, 2) + '\n```');
sendPlayerEmbed(interaction, data);
}
exports.getPlayerInteraction = getPlayerInteraction;