Add globalbeatlist command

This commit is contained in:
baz 2024-02-16 17:04:29 +00:00
parent 3b86e362b5
commit e5e6147d60
2 changed files with 65 additions and 0 deletions

View File

@ -0,0 +1,41 @@
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getAllBeatenGames, checkGameStorageId, getUserFromId } = require('../../databaseHelperFunctions.js');
const { getGameJson } = require('../../igdbHelperFunctions.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('globalbeatlist')
.setDescription('Show a list of all games beaten for the 100 games challenge in chronological order.'),
async execute(interaction) {
await interaction.reply({ content: 'Searching for games...', ephemeral: true });
const user = interaction.user;
const beatenGamesDatabaseEntries = await getAllBeatenGames();
let desc = '';
if (!beatenGamesDatabaseEntries || beatenGamesDatabaseEntries.length == 0) {
desc = 'No games beaten yet.';
} else {
for (let i = 0; i < beatenGamesDatabaseEntries.length; i++) {
const gameid = await checkGameStorageId(beatenGamesDatabaseEntries[i].gameId);
const res = await getGameJson(`where id = ${ gameid.igdb_id }; fields *;`);
const game = res[0];
const userentry = await getUserFromId(beatenGamesDatabaseEntries[i].userId);
const date = beatenGamesDatabaseEntries[i].updatedAt.toLocaleDateString('en-GB', { year: 'numeric', month: '2-digit', day: '2-digit' }).replace(/\//g, '/');
desc = desc.concat('**', date, '**: \t', game.name, ' \t*(', userentry.username, ')*\n');
}
}
const embed = new EmbedBuilder()
.setColor(0x6441a5)
.setAuthor({ name: `${user.displayName}`, iconURL: user.avatarURL() })
.setThumbnail(interaction.client.user.avatarURL())
.setTitle('Global Beat List')
.setDescription(desc)
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })
.setTimestamp();
return interaction.followUp({ embeds: [embed] });
},
};

View File

@ -35,6 +35,17 @@ async function getUserRegistration(user) {
return null; return null;
} }
async function getUserFromId(id) {
const u = await Users.findOne({ where: { id: id } })
.catch((err) => {
console.log(err);
});
if (u) return u;
return null;
}
async function checkGameStorage(game) { async function checkGameStorage(game) {
let g = await Games.findOne({ where: { igdb_id: game.id, name: game.name } }) let g = await Games.findOne({ where: { igdb_id: game.id, name: game.name } })
.catch((err) => { .catch((err) => {
@ -292,6 +303,17 @@ async function getGames(id, status) {
return false; return false;
} }
async function getAllBeatenGames() {
const gameEntries = await LoggedGames.findAll({ where: { status: 'beat' }, order: [ [ 'updatedAt', 'ASC' ]] })
.catch((err) => {
console.log(err);
});
if (gameEntries) return gameEntries;
return false;
}
async function backupDatabase() { async function backupDatabase() {
const date = new Date().toJSON().slice(0, 10); const date = new Date().toJSON().slice(0, 10);
@ -306,6 +328,7 @@ async function backupDatabase() {
module.exports = { module.exports = {
checkUserRegistration, checkUserRegistration,
getUserRegistration, getUserRegistration,
getUserFromId,
checkGameStorage, checkGameStorage,
createPlanningGameEntry, createPlanningGameEntry,
createPlayingGameEntry, createPlayingGameEntry,
@ -331,5 +354,6 @@ module.exports = {
getPlayingGames, getPlayingGames,
getBeatenGames, getBeatenGames,
getGames, getGames,
getAllBeatenGames,
backupDatabase, backupDatabase,
}; };