Skip to content

Commit

Permalink
Merge pull request #2096 from DuckySoLucky/refactorAndFixedV2
Browse files Browse the repository at this point in the history
fix & refactor: fixes and refactors
  • Loading branch information
metalcupcake5 authored Nov 26, 2023
2 parents d8bc2f6 + b96be71 commit e021bdb
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 44 deletions.
40 changes: 6 additions & 34 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import bodyParser from "body-parser";
import "axios-debug-log";

import fs from "fs-extra";
import axios from "axios";

import path from "path";
import * as renderer from "./renderer.js";
Expand Down Expand Up @@ -315,7 +314,10 @@ app.all("/stats/:player/:profile?", async (req, res, next) => {
},
(err, html) => {
if (err) {
throw new SkyCryptError(err);
console.debug(`${debugId}: an error has occurred.`);
console.error(err);

helper.sendWebhookMessage(err, req);
}

console.debug(`${debugId}: page successfully rendered. (${Date.now() - renderStart}ms)`);
Expand All @@ -326,38 +328,8 @@ app.all("/stats/:player/:profile?", async (req, res, next) => {
);
} catch (e) {
if (e instanceof SkyCryptError === false) {
const webhookUrl = credentials.discord_webhook;
if (webhookUrl !== undefined) {
let description = "";
if (playerUsername) {
description += `Username: \`${playerUsername}\`\n`;
}

if (req.params) {
description += `Options: \`${JSON.stringify(req.params)}\`\n`;
}

if (paramProfile) {
description += `Profile: \`${paramProfile}\`\n`;
}

description += `Link: https://sky.shiiyu.moe/stats/${paramPlayer}${paramProfile ? `/${paramProfile}` : ""}\n`;
description += `\`\`\`${e.stack}\`\`\``;

const embed = {
title: "Error",
description: description,
color: 16711680,
fields: [],
footer: {
text: `by @duckysolucky`,
icon_url: "https://imgur.com/tgwQJTX.png",
},
};

axios.post(webhookUrl, { embeds: [embed] }).catch((error) => {
console.log(error);
});
if (e.message !== "socket hang up") {
helper.sendWebhookMessage(e, req);
}
}

Expand Down
32 changes: 32 additions & 0 deletions src/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1162,3 +1162,35 @@ export async function applyResourcePack(item, packs) {

return item;
}

export async function sendWebhookMessage(e, req) {
const webhookUrl = credentials.discord_webhook;
if (webhookUrl !== undefined && req.params !== undefined) {
let description = "";
const playerUsername = req.params.player;
if (playerUsername) {
description += `Username: \`${playerUsername}\`\n`;
}

description += `Options: \`${JSON.stringify(req.params)}\`\n`;

const paramProfile = req.params.profile;
if (paramProfile) {
description += `Profile: \`${paramProfile}\`\n`;
}

description += `Link: https://sky.shiiyu.moe/stats/${playerUsername}${paramProfile ? `/${paramProfile}` : ""}\n`;
description += `\`\`\`${e.stack}\`\`\``;

const embed = {
title: "Error",
description: description,
color: 16711680,
fields: [],
};

axios.post(webhookUrl, { embeds: [embed] }).catch((error) => {
console.log(error);
});
}
}
6 changes: 3 additions & 3 deletions src/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ export async function getStats(

output.skyblock_level = await stats.getSkyBlockLevel(userProfile);

output.visited_zones = userProfile.player_data.visited_zones || [];
output.visited_zones = userProfile.player_data?.visited_zones || [];

output.visited_modes = userProfile.player_data.visited_modes || [];
output.visited_modes = userProfile.player_data?.visited_modes || [];

output.perks = userProfile.player_data.perks || {};
output.perks = userProfile.player_data?.perks || {};

output.harp_quest = userProfile.quests?.harp_quest || {};

Expand Down
2 changes: 1 addition & 1 deletion src/stats/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export async function getCollections(uuid, profile, dungeons, kuudra, cacheOnly

const userProfile = profile.members[uuid];
if (
(userProfile?.player_data && !("unlocked_coll_tiers" in userProfile.player_data)) ||
(userProfile?.player_data && !("unlocked_coll_tiers" in (userProfile?.player_data ?? {}))) ||
!("collection" in userProfile)
) {
return;
Expand Down
2 changes: 1 addition & 1 deletion src/stats/misc.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ export function getMisc(profile, userProfile, hypixelProfile) {
};
}

if (userProfile.player_stats.end_island?.dragon_fight !== undefined) {
if (userProfile.player_stats.end_island?.dragon_fight !== undefined && userProfile.player_stats?.kills) {
const dragonKills = Object.keys(userProfile.player_stats.kills)
.filter((key) => key.endsWith("_dragon") && !key.startsWith("master_wither_king"))
.reduce((obj, key) => ({ ...obj, [key.replace("_dragon", "")]: userProfile.player_stats.kills[key] }), {});
Expand Down
4 changes: 2 additions & 2 deletions src/stats/rift.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export function getRift(userProfile) {
return {
motes: {
purse: userProfile.currencies?.motes_purse ?? 0,
lifetime: userProfile.player_stats.rift?.lifetime_motes_earned ?? 0,
orbs: userProfile.player_stats.rift?.motes_orb_pickup ?? 0,
lifetime: userProfile.player_stats?.rift?.lifetime_motes_earned ?? 0,
orbs: userProfile.player_stats?.rift?.motes_orb_pickup ?? 0,
},
enigma: {
souls: rift.enigma?.found_souls?.length ?? 0,
Expand Down
8 changes: 5 additions & 3 deletions views/sections/stats/misc/mythological_event.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
<p class="stat-raw-values">
<!-- Mytholocial Event Kills -->
<span class="stat-name">Kills: </span>
<span class="stat-value"><%= calculated.misc.mythological_event.kills.toLocaleString() %></span>
<br>
<% if (calculated.misc.mythological_event.kills !== undefined) { %>
<span class="stat-name">Kills: </span>
<span class="stat-value"><%= calculated.misc.mythological_event.kills.toLocaleString() %></span>
<br>
<% } %>
<%
const burrow_naming = {
Expand Down

0 comments on commit e021bdb

Please sign in to comment.