Compare commits

..

4 Commits

Author SHA1 Message Date
baz 4be5e867cc Create leaderboard command 2023-12-19 22:19:08 +00:00
baz 0af7fc65ea Remove unnecessary databaseEntry checks 2023-12-19 22:18:59 +00:00
baz 826dca89d2 Create deletegameentry command 2023-12-19 20:42:54 +00:00
baz 8c19d849bf Add checks 2023-12-19 19:47:26 +00:00
4 changed files with 146 additions and 10 deletions

View File

@ -1,6 +1,6 @@
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getCoverURL, getGameJson } = require('../../igdbHelperFunctions.js'); const { getCoverURL, getGameJson } = require('../../igdbHelperFunctions.js');
const { checkUserRegistration, checkGameStorage, getUserRegistration, createBeatenGameEntry, getBeatenGameCount } = require('../../databaseHelperFunctions.js'); const { checkGameStorage, getUserRegistration, createBeatenGameEntry, getBeatenGameCount } = require('../../databaseHelperFunctions.js');
module.exports = { module.exports = {
data: new SlashCommandBuilder() data: new SlashCommandBuilder()
@ -8,17 +8,14 @@ module.exports = {
.setDescription('Log a game that you have beat towards the 100 game challenge!') .setDescription('Log a game that you have beat towards the 100 game challenge!')
.addStringOption(option => option.setName('gamename').setDescription('The name of the game.')) .addStringOption(option => option.setName('gamename').setDescription('The name of the game.'))
.addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)) .addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0))
.addStringOption(option => option.setName('datestarted').setDescription('The date you started playing the game (today if empty).')) .addStringOption(option => option.setName('datebeaten').setDescription('The date you beat the game (today if empty).')),
.addStringOption(option => option.setName('datebeaten').setDescription('The date you beat the game (today if empty).'))
.addStringOption(option => option.setName('platform').setDescription('The platform the game was released on.')),
async execute(interaction) { async execute(interaction) {
if (!checkUserRegistration(interaction.user)) return interaction.reply(`Issue checking registration with "${interaction.user.username}".`);
const userDatabaseEntry = await getUserRegistration(interaction.user); const userDatabaseEntry = await getUserRegistration(interaction.user);
if (!userDatabaseEntry) return interaction.reply(`Issue checking registration with "${interaction.user.username}".`);
const gamename = interaction.options.getString('gamename'); const gamename = interaction.options.getString('gamename');
const gameid = interaction.options.getNumber('gameid'); const gameid = interaction.options.getNumber('gameid');
const platform = interaction.options.getString('platform');
if (!gamename && !gameid) return interaction.reply('No gamename or gameid supplied, please supply an option to register a game!'); if (!gamename && !gameid) return interaction.reply('No gamename or gameid supplied, please supply an option to register a game!');
@ -28,6 +25,7 @@ module.exports = {
body = body.concat('where id = ', gameid, '; '); body = body.concat('where id = ', gameid, '; ');
} else if (gamename) { } else if (gamename) {
body = body.concat('search "', gamename, '"; '); body = body.concat('search "', gamename, '"; ');
body = body.concat('limit 25; where (category = 0 | category = 4) & version_parent = null;');
} }
body = body.concat('fields *;'); body = body.concat('fields *;');
@ -36,16 +34,21 @@ module.exports = {
if (!res[0]) return interaction.reply('No game found for the options supplied.'); if (!res[0]) return interaction.reply('No game found for the options supplied.');
const gameDatabaseEntry = await checkGameStorage(res[0]); const game = res[0];
const release_date = game.first_release_date;
if (!release_date || (release_date * 1000) > Date.now()) return interaction.reply(`${game.name} is not yet released.`);
const gameDatabaseEntry = await checkGameStorage(game);
if (!(await createBeatenGameEntry(userDatabaseEntry, gameDatabaseEntry))) return interaction.reply(`${game.name} already beaten.`);
await createBeatenGameEntry(userDatabaseEntry, gameDatabaseEntry);
const num = await getBeatenGameCount(userDatabaseEntry); const num = await getBeatenGameCount(userDatabaseEntry);
const coverUrl = await getCoverURL(res[0].cover); const coverUrl = await getCoverURL(game.cover);
const embed = new EmbedBuilder() const embed = new EmbedBuilder()
.setColor(0xFFD700) .setColor(0xFFD700)
.setAuthor({ name: `${interaction.user.displayName} beat a new game!`, iconURL: interaction.user.avatarURL() }) .setAuthor({ name: `${interaction.user.displayName} beat a new game!`, iconURL: interaction.user.avatarURL() })
.setTitle(`${res[0].name} beaten!`) .setTitle(`${game.name} beaten!`)
.setThumbnail(`${coverUrl}`) .setThumbnail(`${coverUrl}`)
.setDescription(`${interaction.user.displayName} has beaten ${num} games, they have ${100 - num} games remaining.`) .setDescription(`${interaction.user.displayName} has beaten ${num} games, they have ${100 - num} games remaining.`)
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() }) .setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })

View File

@ -0,0 +1,31 @@
const { SlashCommandBuilder } = require('discord.js');
const { deleteBeatenGameId, getUserRegistration, deleteBeatenGameNum, checkGameStorageId } = require('../../databaseHelperFunctions.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('deletegameentry')
.setDescription('Delete a game that you have beaten from the 100 game challenge!')
.addNumberOption(option => option.setName('databaseentryid').setDescription('The beat game id.').setMinValue(1))
.addNumberOption(option => option.setName('beatgamenumber').setDescription('Index of the game to delete in the list of beaten games.').setMinValue(1).setMaxValue(100)),
async execute(interaction) {
const databaseEntryId = interaction.options.getNumber('databaseentryid');
const beatGameNumber = interaction.options.getNumber('beatgamenumber');
if (!databaseEntryId && !beatGameNumber) return interaction.reply('No parameters supplied');
const userDatabaseEntry = await getUserRegistration(interaction.user);
let result;
if (databaseEntryId) {
result = await deleteBeatenGameId(databaseEntryId, userDatabaseEntry);
} else if (beatGameNumber) {
result = await deleteBeatenGameNum(beatGameNumber, userDatabaseEntry);
}
if (result) {
const game = await checkGameStorageId(result.gameId);
return interaction.reply(`${game.name} successfully deleted`);
}
return interaction.reply('Unable to delete entry / No entry found.');
},
};

View File

@ -0,0 +1,33 @@
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getLeaderboardEntries } = require('../../databaseHelperFunctions.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('leaderboard')
.setDescription('Show the leaderboard!'),
async execute(interaction) {
const leaderboard = await getLeaderboardEntries();
if (!leaderboard) return interaction.reply('There was a problem!');
await leaderboard.sort((a, b) => parseInt(b.count) - parseInt(a.count));
let desc = '';
for (let i = 0; i < leaderboard.length; i++) {
let newLine = '';
newLine = newLine.concat((i + 1), '. \t', leaderboard[i].username, ' - ', leaderboard[i].count, ' games\n');
desc = desc.concat(newLine);
}
const embed = new EmbedBuilder()
.setColor(0x6441a5)
.setTitle('The 100 Games Challenge Leaderboard!')
.setDescription(desc)
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })
.setTimestamp();
return interaction.reply({ embeds: [embed] });
},
};

View File

@ -65,6 +65,17 @@ async function checkGameStorage(game) {
return null; return null;
} }
async function checkGameStorageId(id) {
const g = await Games.findOne({ where: { id: id } })
.catch((err) => {
console.log(err);
});
if (g) return g;
return null;
}
async function createBeatenGameEntry(user, game) { async function createBeatenGameEntry(user, game) {
let bg = await BeatenGames.findOne({ where: { userId: user.id, gameId: game.id } }) let bg = await BeatenGames.findOne({ where: { userId: user.id, gameId: game.id } })
.catch((err) => { .catch((err) => {
@ -99,10 +110,68 @@ async function getBeatenGameCount(user) {
return count; return count;
} }
async function deleteBeatenGameId(id, user) {
const bg = await BeatenGames.findOne({ where: { id: id, userId: user.id } })
.catch((err) => {
console.log(err);
});
if (!bg) return false;
const entry = bg;
await bg.destroy();
return entry;
}
async function deleteBeatenGameNum(num, user) {
const bg = await BeatenGames.findAll({ where: { userId: user.id } })
.catch((err) => {
console.log(err);
});
if (!bg) return false;
if (bg.length < num) return false;
const entry = bg[num - 1];
await bg[num - 1].destroy();
return entry;
}
async function getLeaderboardEntries() {
const users = await Users.findAll()
.catch((err) => {
console.log(err);
});
const results = [];
for (let i = 0; i < users.length; i++) {
const count = await BeatenGames.count({ where: { userId: users[i].id } });
const res = await Users.findOne({ where: { id: users[i].id } })
.catch((err) => {
console.log(err);
});
const username = res.username;
const fun = { username, count };
results.push(fun);
}
return results;
}
module.exports = { module.exports = {
checkUserRegistration, checkUserRegistration,
getUserRegistration, getUserRegistration,
checkGameStorage, checkGameStorage,
createBeatenGameEntry, createBeatenGameEntry,
getBeatenGameCount, getBeatenGameCount,
deleteBeatenGameId,
deleteBeatenGameNum,
checkGameStorageId,
getLeaderboardEntries,
}; };