Add option to view games with missing length info to totalbeatlength

This commit is contained in:
baz 2025-06-10 15:49:24 +01:00
parent 5b9e7e1132
commit 05b8cfa228

View File

@ -6,12 +6,14 @@ module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
.setName('totalbeatlength') .setName('totalbeatlength')
.setDescription('Calculate the time taken to beat every game on a users beat games list.') .setDescription('Calculate the time taken to beat every game on a users beat games list.')
.addUserOption(option => option.setName('user').setDescription('The user to check')), .addUserOption(option => option.setName('user').setDescription('The user to check'))
.addBooleanOption(option => option.setName('showmissing').setDescription('Replaces the output with a list of games with missing length information')),
async execute(interaction) { async execute(interaction) {
await interaction.deferReply(); await interaction.deferReply();
let user = interaction.user; let user = interaction.user;
const userOption = interaction.options.getUser('user'); const userOption = interaction.options.getUser('user');
const showmissingOption = interaction.options.getBoolean('showmissing');
if (userOption) { if (userOption) {
user = userOption; user = userOption;
@ -40,7 +42,7 @@ module.exports = {
gameIds.push(game.igdb_id); gameIds.push(game.igdb_id);
} }
const beatGameIGDBEntries = await getGameJson(String.prototype.concat(`where id = (${gameIds}); fields *; limit ${gameIds.length};`)); let beatGameIGDBEntries = await getGameJson(String.prototype.concat(`where id = (${gameIds}); fields *; limit ${gameIds.length};`));
const timings = []; const timings = [];
const timeData = await getTimesToBeat(`where game_id = (${gameIds}); fields *; limit ${gameIds.length};`); const timeData = await getTimesToBeat(`where game_id = (${gameIds}); fields *; limit ${gameIds.length};`);
@ -56,6 +58,10 @@ module.exports = {
return interaction.editReply({ embeds: [embed] }); return interaction.editReply({ embeds: [embed] });
} }
let average;
let desc;
if (!showmissingOption) {
for (let i = 0; i < timeData.length; i++) for (let i = 0; i < timeData.length; i++)
{ {
if (timeData[i]) if (timeData[i])
@ -65,8 +71,19 @@ module.exports = {
} }
} }
const average = Math.round(timings.reduce((sum, num) => sum + num, 0)); average = Math.round(timings.reduce((sum, num) => sum + num, 0));
const desc = `${user.displayName} has spent an estimated **${average} hours** beating games for the 100 games challenge *(${timings.length} of ${beatenGamesDatabaseEntries.length} included)*. `; desc = `${user.displayName} has spent an estimated **${average} hours** beating games for the 100 games challenge *(${timings.length} of ${beatenGamesDatabaseEntries.length} included)*. `;
} else {
for (let i = 0; i < timeData.length; i++)
{
if (timeData[i].normally || timeData[i].hastily)
{
beatGameIGDBEntries = beatGameIGDBEntries.filter(item => item.id !== timeData[i].game_id);
}
}
desc = beatGameIGDBEntries.map(item => `[${item.name}](<${item.url}>)`).join('\n');
}
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor(0x6441a5) .setColor(0x6441a5)