feat: add birthday system

This commit is contained in:
2024-07-07 12:47:29 -07:00
parent 1d2ae0e5a1
commit 1db7d33e22
9 changed files with 342 additions and 13 deletions

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

@ -0,0 +1,143 @@
import { SlashCommandBuilder } from "discord.js";
import { Command } from "../interfaces/Command";
import { errorHandler } from "../utils/errorHandler";
/**
* Validates that the day provided is a valid day of the month.
*
* @param {string} month The month to validate.
* @param {number} day The day to validate.
* @returns {boolean} True if the day is within the month's range.
*/
const validateDate = (month: string, day: number): boolean => {
switch (month) {
case "Jan":
case "Mar":
case "May":
case "Jul":
case "Aug":
case "Oct":
case "Dec":
return day >= 1 && day <= 31;
case "Feb":
return day >= 1 && day <= 29;
case "Apr":
case "Jun":
case "Sep":
case "Nov":
return day >= 1 && day <= 30;
default:
return false;
}
};
export const bbset: Command = {
data: new SlashCommandBuilder()
.setName("bbset")
.setDescription("Set your birthday!")
.addStringOption((option) =>
option
.setName("month")
.setDescription("Your Birth Month")
.setRequired(true)
.setChoices(
{
name: "January",
value: "Jan"
},
{
name: "February",
value: "Feb"
},
{
name: "March",
value: "Mar"
},
{
name: "April",
value: "Apr"
},
{
name: "May",
value: "May"
},
{
name: "June",
value: "Jun"
},
{
name: "July",
value: "Jul"
},
{
name: "August",
value: "Aug"
},
{
name: "September",
value: "Sep"
},
{
name: "October",
value: "Oct"
},
{
name: "November",
value: "Nov"
},
{
name: "December",
value: "Dec"
}
)
)
.addIntegerOption((option) =>
option
.setName("day")
.setDescription("Your Birth Day (1-31)")
.setRequired(true)
.setMinValue(1)
.setMaxValue(31)
),
run: async (bot, interaction) => {
try {
await interaction.deferReply();
const month = interaction.options.getString("month", true);
const day = interaction.options.getInteger("day", true);
if (!validateDate(month, day)) {
await interaction.editReply({
content: `${month} ${day} is not a valid date!`
});
return;
}
await bot.db.birthdays.upsert({
where: {
serverId_userId: {
serverId: interaction.guild.id,
userId: interaction.user.id
}
},
update: {
birthday: new Date(`${month}-${day}-2000`)
},
create: {
serverId: interaction.guild.id,
userId: interaction.user.id,
birthday: new Date(`${month}-${day}-2000`)
}
});
await interaction.editReply(
`Your birthday has been set to ${month}-${day}!`
);
} catch (err) {
const id = await errorHandler(bot, "birthday command", err);
await interaction.editReply({
content: `Something went wrong. Please [join our support server](https://chat.naomi.lgbt) and provide this ID: \`${id}\``
});
}
}
};

View File

@ -11,6 +11,7 @@ import { Command } from "../interfaces/Command";
import { CommandHandler } from "../interfaces/CommandHandler";
import { getConfig } from "../modules/data/getConfig";
import { handleAppealLink } from "../modules/subcommands/config/handleAppealLink";
import { handleBirthdayChannel } from "../modules/subcommands/config/handleBirthdayChannel";
import { handleInviteLink } from "../modules/subcommands/config/handleInviteLink";
import { handleJoinRole } from "../modules/subcommands/config/handleJoinRole";
import { handleList } from "../modules/subcommands/config/handleList";
@ -24,7 +25,8 @@ const handlers: { [key: string]: CommandHandler } = {
"appeal-link": handleAppealLink,
logging: handleLogging,
role: handleRole,
"join-role": handleJoinRole
"join-role": handleJoinRole,
"birthday-channel": handleBirthdayChannel
};
export const config: Command = {
@ -101,6 +103,19 @@ export const config: Command = {
.setDescription("The role to assign.")
.setRequired(true)
)
)
.addSubcommand(
new SlashCommandSubcommandBuilder()
.setName("birthday-channel")
.setDescription(
"Configure a channel where members can be wished a happy birthday."
)
.addChannelOption((o) =>
o
.setName("channel")
.setDescription("The channel to send birthday messages in.")
.setRequired(true)
)
),
run: async (bot, interaction) => {
try {