Convert to Typescript

This commit is contained in:
2024-09-16 09:56:40 +00:00
parent a89150fc2f
commit 6a59118f4a
42 changed files with 2916 additions and 785 deletions

68
src/commands/birthday.ts Normal file
View File

@@ -0,0 +1,68 @@
import { SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
import {
format,
differenceInYears,
differenceInMonths,
differenceInDays,
addYears,
isToday,
} from 'date-fns';
const BIRTHDAY_TIMESTAMP = 1582576229;
// Initialise the command data.
export const data = new SlashCommandBuilder()
.setName('birthday')
.setDescription("Returns ButlerBot's Birthday information.");
console.log(`Loaded ${data.name} command.`);
/**
* Responds with ButlerBot's age and the time remaining until the next birthday.
* @param interaction The interaction that triggered the command.
* @returns A promise that resolves when the command is finished executing.
*/
export async function execute(
interaction: ChatInputCommandInteraction
): Promise<void> {
const today = new Date();
const birthday = new Date(BIRTHDAY_TIMESTAMP * 1000);
const ageYears = differenceInYears(today, birthday);
const ageMonths = differenceInMonths(today, birthday) % 12;
const ageDays = differenceInDays(today, addYears(birthday, ageYears)) % 30;
let ageMessage = `I am ${ageYears} year`;
ageMessage += ageYears !== 1 ? 's, ' : ', ';
ageMessage += `${ageMonths} month`;
ageMessage += ageMonths !== 1 ? 's ' : ' ';
ageMessage += `and ${ageDays} day`;
ageMessage += ageDays !== 1 ? 's ' : ' ';
ageMessage += 'old!';
let birthdayMessage = `I was created on ${format(birthday, 'dd MMMM yyyy')}.`;
const nextBirthdayYear =
today.getMonth() > birthday.getMonth() ||
(today.getMonth() === birthday.getMonth() &&
today.getDate() >= birthday.getDate())
? today.getFullYear() + 1
: today.getFullYear();
const nextBirthday = new Date(
nextBirthdayYear,
birthday.getMonth(),
birthday.getDate()
);
const countdown = differenceInDays(nextBirthday, today);
if (isToday(nextBirthday)) {
birthdayMessage += " It's my Birthday today!";
} else if (countdown === 1) {
birthdayMessage += " It's my Birthday tomorrow!";
} else {
birthdayMessage += ` It's my Birthday in ${countdown} days!`;
}
const fullMessage = `${ageMessage} ${birthdayMessage}`;
await interaction.reply(fullMessage);
}