Compare commits

..

3 Commits

Author SHA1 Message Date
baz dff683366c Add planGame command. 2024-02-11 20:23:19 +00:00
baz e9eb062c40 Update beatGame command 2024-02-11 20:23:08 +00:00
baz cc21bfd377 add functionality for updating game states 2024-02-11 20:23:02 +00:00
3 changed files with 114 additions and 26 deletions

View File

@ -1,6 +1,6 @@
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getCoverURL, getGameJson } = require('../../igdbHelperFunctions.js');
const { checkGameStorage, getUserRegistration, createBeatenGameEntry, getBeatenGameCount } = require('../../databaseHelperFunctions.js');
const { checkGameStorage, getUserRegistration, createBeatenGameEntry, getBeatenGameCount, getPlanningGameCount, getPlayingGameCount } = require('../../databaseHelperFunctions.js');
module.exports = {
data: new SlashCommandBuilder()
@ -46,7 +46,9 @@ module.exports = {
if (!(await createBeatenGameEntry(userDatabaseEntry, gameDatabaseEntry))) return interaction.followUp({ content: `${game.name} already beaten.`, ephemeral: true });
const num = await getBeatenGameCount(userDatabaseEntry);
const beatNum = await getBeatenGameCount(userDatabaseEntry);
const planNum = await getPlanningGameCount(userDatabaseEntry);
const playNum = await getPlayingGameCount(userDatabaseEntry);
const coverUrl = await getCoverURL(game.cover);
const embed = new EmbedBuilder()
@ -54,13 +56,13 @@ module.exports = {
.setAuthor({ name: `${interaction.user.displayName} beat a new game!`, iconURL: interaction.user.avatarURL() })
.setTitle(`${game.name} beaten!`)
.setThumbnail(`${coverUrl}`)
.setDescription(`${interaction.user.displayName} has beaten ${num} games, they have ${100 - num} games remaining.`)
.setDescription(`${interaction.user.displayName} has ${planNum} games planned, they are playing ${playNum} games, they have beaten ${beatNum} games, they have ${100 - beatNum} games remaining.`)
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })
.setTimestamp();
await interaction.followUp({ embeds: [embed] });
if (num == 100) {
if (beatNum == 100) {
const challengeCompletedEmbed = new EmbedBuilder()
.setColor(0xFFD700)
.setAuthor({ name: `${interaction.user.displayName} has completed the 100 Game Challenge!`, iconURL: interaction.user.avatarURL() })

View File

@ -0,0 +1,62 @@
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js');
const { getCoverURL, getGameJson } = require('../../igdbHelperFunctions.js');
const { checkGameStorage, getUserRegistration, createPlanningGameEntry, getBeatenGameCount, getPlanningGameCount, getPlayingGameCount } = require('../../databaseHelperFunctions.js');
module.exports = {
data: new SlashCommandBuilder()
.setName('plangame')
.setDescription('Log a game that you plan beat towards the 100 game challenge!')
.addStringOption(option => option.setName('gamename').setDescription('The name of the game.').setRequired(true))
.addNumberOption(option => option.setName('gameid').setDescription('The IGDB game id.').setMinValue(0)),
async execute(interaction) {
await interaction.reply({ content: 'Attempting to log game.', ephemeral: true });
const userDatabaseEntry = await getUserRegistration(interaction.user);
if (!userDatabaseEntry) return interaction.followUp({ content: `Issue checking registration with "${interaction.user.username}".`, ephemeral: true });
const gamename = interaction.options.getString('gamename');
const gameid = interaction.options.getNumber('gameid');
if (!gamename && !gameid) return interaction.followUp({ content: 'No gamename or gameid supplied, please supply an option to register a game!', ephemeral: true });
let body = '';
if (gameid) {
body = body.concat('where id = ', gameid, '; ');
} else if (gamename) {
body = body.concat('search "', gamename, '"; ');
body = body.concat('limit 25; where (category = 0 | category = 4 | category = 8 | category = 9) & version_parent = null;');
}
body = body.concat('fields *;');
const res = await getGameJson(body);
if (!res[0]) return interaction.followUp({ content: 'No game found for the options supplied.', ephemeral: true });
const game = res[0];
const release_date = game.first_release_date;
if (!release_date || (release_date * 1000) > Date.now()) return interaction.followUp({ content: `${game.name} is not yet released.`, ephemeral: true });
const gameDatabaseEntry = await checkGameStorage(game);
if (!(await createPlanningGameEntry(userDatabaseEntry, gameDatabaseEntry))) return interaction.followUp({ content: `${game.name} already planned.`, ephemeral: true });
const beatNum = await getBeatenGameCount(userDatabaseEntry);
const planNum = await getPlanningGameCount(userDatabaseEntry);
const playNum = await getPlayingGameCount(userDatabaseEntry);
const coverUrl = await getCoverURL(game.cover);
const embed = new EmbedBuilder()
.setColor(0xFFD700)
.setAuthor({ name: `${interaction.user.displayName} is planning to beat a game!`, iconURL: interaction.user.avatarURL() })
.setTitle(`${game.name}!`)
.setThumbnail(`${coverUrl}`)
.setDescription(`${interaction.user.displayName} has ${planNum} games planned, they are playing ${playNum} games, they have beaten ${beatNum} games, they have ${100 - beatNum} games remaining.`)
.setFooter({ text: 'The Ochulus • 100 Games Challenge', iconURL: interaction.client.user.avatarURL() })
.setTimestamp();
await interaction.followUp({ embeds: [embed] });
},
};

View File

@ -68,26 +68,56 @@ async function checkGameStorageId(id) {
}
async function createPlanningGameEntry(user, game) {
return createLoggedGameEntry(user, game, 'planning');
const entry = await checkLoggedGameEntry(user, game);
if (!entry) return await createLoggedGameEntry(user, game, 'planning');
if (entry.status == 'planning') return false;
entry.update({ status: 'planning' });
return entry;
}
async function createPlayingGameEntry(user, game) {
return createLoggedGameEntry(user, game, 'playing');
const entry = await checkLoggedGameEntry(user, game);
if (!entry) return await createLoggedGameEntry(user, game, 'playing');
if (entry.status == 'playing') return false;
entry.save({ status: 'playing' });
return entry;
}
async function createBeatenGameEntry(user, game) {
return createLoggedGameEntry(user, game, 'beat');
const entry = await checkLoggedGameEntry(user, game);
if (!entry) return await createLoggedGameEntry(user, game, 'beat');
if (entry.status == 'beat') return false;
entry.update({ status: 'beat' });
return entry;
}
async function createLoggedGameEntry(user, game, status) {
let bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id, status: status } })
async function checkLoggedGameEntry(user, game) {
const bg = await LoggedGames.findOne({ where: { userId: user.id, gameId: game.id } })
.catch((err) => {
console.log(err);
});
if (bg) return false;
if (!bg) return false;
await LoggedGames.create({ userId: user.id, gameId: game.id })
return bg;
}
async function createLoggedGameEntry(user, game, status) {
let bg;
await LoggedGames.create({ userId: user.id, gameId: game.id, status: status })
.then((data) => {
bg = data;
})
@ -101,15 +131,15 @@ async function createLoggedGameEntry(user, game, status) {
}
async function getPlanningGameCount(user) {
return getLoggedGameCount(user, 'planning');
return await getLoggedGameCount(user, 'planning');
}
async function getPlayingGameCount(user) {
return getLoggedGameCount(user, 'playing');
return await getLoggedGameCount(user, 'playing');
}
async function getBeatenGameCount(user) {
return getLoggedGameCount(user, 'beat');
return await getLoggedGameCount(user, 'beat');
}
async function getLoggedGameCount(user, status) {
@ -126,15 +156,15 @@ async function getLoggedGameCount(user, status) {
}
async function deletePlanningGameId(id, user) {
return deleteLoggedGameId(id, user, 'planning');
return await deleteLoggedGameId(id, user, 'planning');
}
async function deletePlayingGameId(id, user) {
return deleteLoggedGameId(id, user, 'playing');
return await deleteLoggedGameId(id, user, 'playing');
}
async function deleteBeatenGameId(id, user) {
return deleteLoggedGameId(id, user, 'beat');
return await deleteLoggedGameId(id, user, 'beat');
}
async function deleteLoggedGameId(id, user, status) {
@ -204,21 +234,15 @@ async function getLeaderboardEntries() {
}
async function getRecentPlanningGameEntry(userId) {
return getRecentGameEntry(userId, 'planning');
return await getRecentGameEntry(userId, 'planning');
}
async function getRecentPlayingGameEntry(userId) {
return getRecentGameEntry(userId, 'playing');
return await getRecentGameEntry(userId, 'playing');
}
async function getRecentBeatenGameEntry(userId) {
return getRecentGameEntry(userId, 'beat');
return await getRecentGameEntry(userId, 'beat');
}
async function getRecentGameEntry(userId, status) {