From 684b64985f83e49f47a05b44a2362bb19fadc0b9 Mon Sep 17 00:00:00 2001 From: Mint <117024989+LovelyMint@users.noreply.github.com> Date: Sun, 13 Nov 2022 19:30:55 +0900 Subject: [PATCH] Handle osu! command when the module is not initialized --- src/discord/osu.ts | 64 +++++++++++++++++------------------------ src/providers/App.ts | 4 +++ src/providers/osuAPI.ts | 8 ++---- 3 files changed, 32 insertions(+), 44 deletions(-) diff --git a/src/discord/osu.ts b/src/discord/osu.ts index bf0623a..3782c62 100644 --- a/src/discord/osu.ts +++ b/src/discord/osu.ts @@ -73,6 +73,12 @@ const EMBEDS = { title: `No osu! beatmap id provided`, user: data.getUser() }); + }, + NOT_INITIALIZED: (data: HybridInteractionMessage) => { + return makeErrorEmbed({ + title: `osu! feature is disabled`, + user: data.getUser() + }); } }; @@ -93,6 +99,12 @@ export default class osu extends DiscordModule { const Guild = await Prisma.client.guild.findFirst({ where: { id: data.getGuild()!.id } }); if (!Guild) return; + + if (!osuAPI.client) + return await sendHybridInteractionMessageResponse(data, { + embeds: [EMBEDS.NOT_INITIALIZED(data)] + }); + const funct = { user: async (data: HybridInteractionMessage) => { let user; @@ -111,7 +123,7 @@ export default class osu extends DiscordModule { embeds: [EMBEDS.INVALID_USER_MENTIONED(data)] }); - let result = await osuAPI.client.getUser({ u: user }); + let result = await osuAPI.client!.getUser({ u: user }); if (result instanceof Array && result.length === 0) return await sendHybridInteractionMessageResponse(data, { @@ -123,12 +135,8 @@ export default class osu extends DiscordModule { } const level = { - number: (Math.round((result.level + Number.EPSILON) * 100) / 100) - .toFixed(2) - .split('.')[0], - progression: (Math.round((result.level + Number.EPSILON) * 100) / 100) - .toFixed(2) - .split('.')[1] + number: (Math.round((result.level + Number.EPSILON) * 100) / 100).toFixed(2).split('.')[0], + progression: (Math.round((result.level + Number.EPSILON) * 100) / 100).toFixed(2).split('.')[1] }; const embed = makeInfoEmbed({ icon: '', @@ -142,10 +150,7 @@ export default class osu extends DiscordModule { )}**, totaling in **${this.numberWithCommas( parseFloat( ( - Math.round( - (result.secondsPlayed / (60 * 60) + Number.EPSILON) * - 100 - ) / 100 + Math.round((result.secondsPlayed / (60 * 60) + Number.EPSILON) * 100) / 100 ).toFixed(2) ) )} ${result.secondsPlayed < 60 ? 'hour' : 'hours'}** of songs played @@ -236,9 +241,7 @@ export default class osu extends DiscordModule { name: `❤ Account Information`, value: `Joined: , ` + )}:R>, ` } //, /*{ name: `💌 Recent Events (Coming soon)`, @@ -255,8 +258,7 @@ export default class osu extends DiscordModule { // TODO: Fix for ppl with no image embed.setThumbnail( - `https://a.ppy.sh/${result.id}` || - 'https://osu.ppy.sh/images/layout/avatar-guest.png' + `https://a.ppy.sh/${result.id}` || 'https://osu.ppy.sh/images/layout/avatar-guest.png' ); const row = new ActionRowBuilder().addComponents([ @@ -289,7 +291,7 @@ export default class osu extends DiscordModule { embeds: [EMBEDS.INVALID_BEATMAP_ID_MENTIONED(data)] }); - let result = await osuAPI.client.getBeatmaps({ b: beatmap }); + let result = await osuAPI.client!.getBeatmaps({ b: beatmap }); if (result instanceof Array && result.length === 0) return await sendHybridInteractionMessageResponse(data, { @@ -331,17 +333,13 @@ export default class osu extends DiscordModule { { name: '⭐ Star Difficulty', value: `**${( - Math.round((bm_result.difficulty.rating + Number.EPSILON) * 100) / - 100 + Math.round((bm_result.difficulty.rating + Number.EPSILON) * 100) / 100 ).toFixed(2)}**`, inline: true }, { name: `⌛ Length`, - value: `**${( - Math.round((bm_result.length.total / 60 + Number.EPSILON) * 100) / - 100 - ) + value: `**${(Math.round((bm_result.length.total / 60 + Number.EPSILON) * 100) / 100) .toFixed(2) .replace('.', ':')}**`, inline: true @@ -389,19 +387,13 @@ export default class osu extends DiscordModule { Genre: **${bm_result.genre}** Submission Date: , + )}:R>, Last updated: , + )}:R>, Approved: , + )}:R>, \u200b` }, { @@ -433,9 +425,7 @@ export default class osu extends DiscordModule { url: `https://osu.ppy.sh/beatmapsets/${bm_result.beatmapSetId}${url_mode}/${bm_result.id}`, iconURL: `https://upload.wikimedia.org/wikipedia/commons/e/e3/Osulogo.png` }); - embed2.setImage( - `https://assets.ppy.sh/beatmaps/${bm_result.beatmapSetId}/covers/cover.jpg` - ); + embed2.setImage(`https://assets.ppy.sh/beatmaps/${bm_result.beatmapSetId}/covers/cover.jpg`); const row = new ActionRowBuilder(); if (bm_result.hasDownload) @@ -455,9 +445,7 @@ export default class osu extends DiscordModule { new ButtonBuilder() .setEmoji('💬') .setLabel(' Open discussion') - .setURL( - `https://osu.ppy.sh/beatmapsets/${bm_result.beatmapSetId}/discussion` - ) + .setURL(`https://osu.ppy.sh/beatmapsets/${bm_result.beatmapSetId}/discussion`) .setStyle(ButtonStyle.Link) ]); diff --git a/src/providers/App.ts b/src/providers/App.ts index b39d96f..570f43c 100644 --- a/src/providers/App.ts +++ b/src/providers/App.ts @@ -39,6 +39,10 @@ class App { } public load_osu(): void { + if (!Environment.get().OSU_API_KEY) { + Logger.log('warn', 'OSU_API_KEY is not defined in .env, osu! features will be disabled'); + return; + } Logger.log('info', 'Loading osu! Client'); osu.init(); } diff --git a/src/providers/osuAPI.ts b/src/providers/osuAPI.ts index 65314c4..8a8b7e4 100644 --- a/src/providers/osuAPI.ts +++ b/src/providers/osuAPI.ts @@ -2,14 +2,10 @@ import { Api } from 'node-osu'; import Environment from './Environment'; class osuAPI { - public client: Api; + public client: Api | null; constructor() { - this.client = new Api(Environment.get().OSU_API_KEY, { - notFoundAsError: false, - completeScores: true, - parseNumeric: true - }); + this.client = null; } public init(): void {