Handle osu! command when the module is not initialized

This commit is contained in:
Mint
2022-11-13 19:31:17 +09:00
parent b88ead6d93
commit 684b64985f
3 changed files with 32 additions and 44 deletions
+26 -38
View File
@@ -73,6 +73,12 @@ const EMBEDS = {
title: `No osu! beatmap id provided`, title: `No osu! beatmap id provided`,
user: data.getUser() 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 } }); const Guild = await Prisma.client.guild.findFirst({ where: { id: data.getGuild()!.id } });
if (!Guild) return; if (!Guild) return;
if (!osuAPI.client)
return await sendHybridInteractionMessageResponse(data, {
embeds: [EMBEDS.NOT_INITIALIZED(data)]
});
const funct = { const funct = {
user: async (data: HybridInteractionMessage) => { user: async (data: HybridInteractionMessage) => {
let user; let user;
@@ -111,7 +123,7 @@ export default class osu extends DiscordModule {
embeds: [EMBEDS.INVALID_USER_MENTIONED(data)] 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) if (result instanceof Array && result.length === 0)
return await sendHybridInteractionMessageResponse(data, { return await sendHybridInteractionMessageResponse(data, {
@@ -123,12 +135,8 @@ export default class osu extends DiscordModule {
} }
const level = { const level = {
number: (Math.round((result.level + Number.EPSILON) * 100) / 100) number: (Math.round((result.level + Number.EPSILON) * 100) / 100).toFixed(2).split('.')[0],
.toFixed(2) progression: (Math.round((result.level + Number.EPSILON) * 100) / 100).toFixed(2).split('.')[1]
.split('.')[0],
progression: (Math.round((result.level + Number.EPSILON) * 100) / 100)
.toFixed(2)
.split('.')[1]
}; };
const embed = makeInfoEmbed({ const embed = makeInfoEmbed({
icon: '', icon: '',
@@ -142,10 +150,7 @@ export default class osu extends DiscordModule {
)}**, totaling in **${this.numberWithCommas( )}**, totaling in **${this.numberWithCommas(
parseFloat( parseFloat(
( (
Math.round( Math.round((result.secondsPlayed / (60 * 60) + Number.EPSILON) * 100) / 100
(result.secondsPlayed / (60 * 60) + Number.EPSILON) *
100
) / 100
).toFixed(2) ).toFixed(2)
) )
)} ${result.secondsPlayed < 60 ? 'hour' : 'hours'}** of songs played )} ${result.secondsPlayed < 60 ? 'hour' : 'hours'}** of songs played
@@ -236,9 +241,7 @@ export default class osu extends DiscordModule {
name: `❤ Account Information`, name: `❤ Account Information`,
value: `Joined: <t:${Math.round( value: `Joined: <t:${Math.round(
new Date(result.raw_joinDate).getTime() / 1000 new Date(result.raw_joinDate).getTime() / 1000
)}:R>, <t:${Math.round( )}:R>, <t:${Math.round(new Date(result.raw_joinDate).getTime() / 1000)}:f>`
new Date(result.raw_joinDate).getTime() / 1000
)}:f>`
} //, } //,
/*{ /*{
name: `💌 Recent Events (Coming soon)`, name: `💌 Recent Events (Coming soon)`,
@@ -255,8 +258,7 @@ export default class osu extends DiscordModule {
// TODO: Fix for ppl with no image // TODO: Fix for ppl with no image
embed.setThumbnail( embed.setThumbnail(
`https://a.ppy.sh/${result.id}` || `https://a.ppy.sh/${result.id}` || 'https://osu.ppy.sh/images/layout/avatar-guest.png'
'https://osu.ppy.sh/images/layout/avatar-guest.png'
); );
const row = new ActionRowBuilder<ButtonBuilder>().addComponents([ const row = new ActionRowBuilder<ButtonBuilder>().addComponents([
@@ -289,7 +291,7 @@ export default class osu extends DiscordModule {
embeds: [EMBEDS.INVALID_BEATMAP_ID_MENTIONED(data)] 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) if (result instanceof Array && result.length === 0)
return await sendHybridInteractionMessageResponse(data, { return await sendHybridInteractionMessageResponse(data, {
@@ -331,17 +333,13 @@ export default class osu extends DiscordModule {
{ {
name: '⭐ Star Difficulty', name: '⭐ Star Difficulty',
value: `**${( value: `**${(
Math.round((bm_result.difficulty.rating + Number.EPSILON) * 100) / Math.round((bm_result.difficulty.rating + Number.EPSILON) * 100) / 100
100
).toFixed(2)}**`, ).toFixed(2)}**`,
inline: true inline: true
}, },
{ {
name: `⌛ Length`, name: `⌛ Length`,
value: `**${( value: `**${(Math.round((bm_result.length.total / 60 + Number.EPSILON) * 100) / 100)
Math.round((bm_result.length.total / 60 + Number.EPSILON) * 100) /
100
)
.toFixed(2) .toFixed(2)
.replace('.', ':')}**`, .replace('.', ':')}**`,
inline: true inline: true
@@ -389,19 +387,13 @@ export default class osu extends DiscordModule {
Genre: **${bm_result.genre}** Genre: **${bm_result.genre}**
Submission Date: <t:${Math.round( Submission Date: <t:${Math.round(
new Date(bm_result.raw_submitDate).getTime() / 1000 new Date(bm_result.raw_submitDate).getTime() / 1000
)}:R>, <t:${Math.round( )}:R>, <t:${Math.round(new Date(bm_result.raw_submitDate).getTime() / 1000)}:f>
new Date(bm_result.raw_submitDate).getTime() / 1000
)}:f>
Last updated: <t:${Math.round( Last updated: <t:${Math.round(
new Date(bm_result.raw_lastUpdate).getTime() / 1000 new Date(bm_result.raw_lastUpdate).getTime() / 1000
)}:R>, <t:${Math.round( )}:R>, <t:${Math.round(new Date(bm_result.raw_lastUpdate).getTime() / 1000)}:f>
new Date(bm_result.raw_lastUpdate).getTime() / 1000
)}:f>
Approved: <t:${Math.round( Approved: <t:${Math.round(
new Date(bm_result.raw_approvedDate).getTime() / 1000 new Date(bm_result.raw_approvedDate).getTime() / 1000
)}:R>, <t:${Math.round( )}:R>, <t:${Math.round(new Date(bm_result.raw_approvedDate).getTime() / 1000)}:f>
new Date(bm_result.raw_approvedDate).getTime() / 1000
)}:f>
\u200b` \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}`, 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` iconURL: `https://upload.wikimedia.org/wikipedia/commons/e/e3/Osulogo.png`
}); });
embed2.setImage( embed2.setImage(`https://assets.ppy.sh/beatmaps/${bm_result.beatmapSetId}/covers/cover.jpg`);
`https://assets.ppy.sh/beatmaps/${bm_result.beatmapSetId}/covers/cover.jpg`
);
const row = new ActionRowBuilder<ButtonBuilder>(); const row = new ActionRowBuilder<ButtonBuilder>();
if (bm_result.hasDownload) if (bm_result.hasDownload)
@@ -455,9 +445,7 @@ export default class osu extends DiscordModule {
new ButtonBuilder() new ButtonBuilder()
.setEmoji('💬') .setEmoji('💬')
.setLabel(' Open discussion') .setLabel(' Open discussion')
.setURL( .setURL(`https://osu.ppy.sh/beatmapsets/${bm_result.beatmapSetId}/discussion`)
`https://osu.ppy.sh/beatmapsets/${bm_result.beatmapSetId}/discussion`
)
.setStyle(ButtonStyle.Link) .setStyle(ButtonStyle.Link)
]); ]);
+4
View File
@@ -39,6 +39,10 @@ class App {
} }
public load_osu(): void { 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'); Logger.log('info', 'Loading osu! Client');
osu.init(); osu.init();
} }
+2 -6
View File
@@ -2,14 +2,10 @@ import { Api } from 'node-osu';
import Environment from './Environment'; import Environment from './Environment';
class osuAPI { class osuAPI {
public client: Api; public client: Api | null;
constructor() { constructor() {
this.client = new Api(Environment.get().OSU_API_KEY, { this.client = null;
notFoundAsError: false,
completeScores: true,
parseNumeric: true
});
} }
public init(): void { public init(): void {