Skip to content

Commit

Permalink
feat: check if birthday exists before update, remove or show
Browse files Browse the repository at this point in the history
  • Loading branch information
willy-r committed Apr 27, 2024
1 parent 145efd9 commit b52bdb9
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
9 changes: 8 additions & 1 deletion src/commands/birthday/remove.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { SlashCommandBuilder } = require('discord.js');

const { deleteByUserAndGuild } = require('../../repositories/birthdayRepository');
const { deleteByUserAndGuild, findByUserAndGuild } = require('../../repositories/birthdayRepository');

module.exports = {
data: new SlashCommandBuilder()
Expand All @@ -20,6 +20,13 @@ module.exports = {
const { id: guildId } = interaction.guild;

try {
const birthday = await findByUserAndGuild(userId, guildId);

if (birthday === null) {
await interaction.reply('I didn\'t find birthday information for this user on this server to update 😥');
return;
}

await deleteByUserAndGuild(userId, guildId);
await interaction.reply('From now, I can\'t remember your birthday 😥');
}
Expand Down
11 changes: 9 additions & 2 deletions src/commands/birthday/show.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,19 @@ module.exports = {

try {
const birthdayData = await findByUserAndGuild(userId, guildId);

if (birthdayData === null) {
await interaction.reply('I didn\'t find birthday information for this user on this server to show 😥');
return;
}

const options = {
month: 'numeric',
day: 'numeric',
};
const formattedDate = birthdayData.birthdate.toLocaleDateString('pt-BR', options);
const formattedTimeUntilBirthday = timeUntilBirthday(birthdayData.birthdate);
const { birthdate } = birthdayData;
const formattedDate = birthdate.toLocaleDateString('pt-BR', options);
const formattedTimeUntilBirthday = timeUntilBirthday(birthdate);

let message = `According to my records, your birthday is ${formattedTimeUntilBirthday} away as it falls on ${formattedDate}`;

Expand Down
11 changes: 9 additions & 2 deletions src/commands/birthday/update.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { SlashCommandBuilder } = require('discord.js');

const { updateByUserAndGuild } = require('../../repositories/birthdayRepository');
const { updateByUserAndGuild, findByUserAndGuild } = require('../../repositories/birthdayRepository');
const { parseDateStringToDate, isDateInFuture } = require('../../utils/date');

module.exports = {
Expand Down Expand Up @@ -44,9 +44,16 @@ module.exports = {

const birthdayData = {
show_age: showAge,
birthdate,
birthdate: birthdateToDate,
};
try {
const birthday = await findByUserAndGuild(userId, guildId);

if (birthday === null) {
await interaction.reply('I didn\'t find birthday information for this user on this server to update 😥');
return;
}

await updateByUserAndGuild(userId, guildId, birthdayData);
await interaction.reply('Your birthday has been updated successfully! 🎉');
}
Expand Down

0 comments on commit b52bdb9

Please sign in to comment.