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

View File

@@ -1,7 +1,8 @@
import * as cheerio from 'cheerio';
import { CommandInteraction } from 'discord.js';
import { CommandInteraction, MessageEmbed } from 'discord.js';
import { IncomingMessage } from 'http';
import http from 'https';
import { emsg } from './util';
const uniteApiRegex = {
//$1 = name, $2 = id
@@ -16,6 +17,7 @@ const uniteApiRegex = {
type uniteApiData = {
name: string,
id: string,
avatar: string,
level: string,
rank: string,
@@ -71,12 +73,15 @@ function getHTML(name: string): Promise<string> {
* 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: string): uniteApiData {
let metaElems = cheerio.load(html)('meta').toArray(),
let $ = cheerio.load(html)
let metaElems = $('meta').toArray(),
foundData: uniteApiData = {
name: "",
id: "",
avatar: "",
level: "",
rank: "",
@@ -113,7 +118,7 @@ function readHTML(html: string): uniteApiData {
}
if (!lines.length)
throw 'Unable to read data, please try again';
throw emsg('Unable to read data, please try again');
//bring the first lines removed back into the data
lines = [
@@ -133,7 +138,7 @@ function readHTML(html: string): uniteApiData {
let regexData = line.match(regex[0]);
if (!regexData || regexData.length < 4)
throw 'Unable to read data, please try again';
throw emsg('Unable to read data, please try again');
foundData.level = regexData[1];
foundData.rank = regexData[2];
@@ -144,7 +149,7 @@ function readHTML(html: string): uniteApiData {
let regexData = line.match(regex[1]);
if (!regexData || regexData.length < 4)
throw 'Unable to read data, please try again';
throw emsg('Unable to read data, please try again');
foundData.level = regexData[1];
foundData.rank = regexData[2];
@@ -179,7 +184,10 @@ function readHTML(html: string): uniteApiData {
}
})
});
foundData.avatar = $('.player-card-image img').attr('src') || "";
foundData.avatar = foundData.avatar.replace('../', 'https://uniteapi.dev/');
return foundData;
@@ -209,9 +217,27 @@ export async function getPlayer(name: string): Promise<uniteApiData|null> {
return null;
}
async function sendPlayerEmbed(interaction: CommandInteraction, data: uniteApiData) {
let embed = new 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
*/
export async function getPlayerInteraction(interaction: CommandInteraction) {
let username = interaction.options.getString('username', true);
@@ -219,7 +245,7 @@ export async function getPlayerInteraction(interaction: CommandInteraction) {
let data = await getPlayer(username);
if (data === null)
await interaction.editReply('Unable to find user');
throw emsg('Unable to find user');
else
await interaction.editReply('```\n'+JSON.stringify(data, null, 2)+'\n```');
sendPlayerEmbed(interaction, data);
}