generated from nhcarrigan/template
feat: add birthday system
This commit is contained in:
41
src/modules/postBirthdays.ts
Normal file
41
src/modules/postBirthdays.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { ExtendedClient } from "../interfaces/ExtendedClient";
|
||||
import { errorHandler } from "../utils/errorHandler";
|
||||
|
||||
/**
|
||||
* Fetches the configs from the database, then for each config that
|
||||
* has a birthday channel set, fetch birthdays. If any are from today,
|
||||
* post!
|
||||
*
|
||||
* @param {ExtendedClient} bot The bot's Discord instance.
|
||||
*/
|
||||
export const postBirthdays = async (bot: ExtendedClient) => {
|
||||
try {
|
||||
const configs = await bot.db.configs.findMany();
|
||||
const withChannel = configs.filter((c) => c.birthdayChannel);
|
||||
for (const record of withChannel) {
|
||||
const guild = bot.guilds.cache.get(record.serverId);
|
||||
if (!guild) {
|
||||
continue;
|
||||
}
|
||||
const channel = guild.channels.cache.get(record.birthdayChannel);
|
||||
if (!channel || !("send" in channel)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const hasBirthdaySet = await bot.db.birthdays.findMany({
|
||||
where: { serverId: guild.id }
|
||||
});
|
||||
const today = new Date();
|
||||
const todayIn2000 = new Date(
|
||||
`2000-${today.getMonth() + 1}-${today.getDate()}`
|
||||
);
|
||||
const isBirthdayToday = hasBirthdaySet.filter(
|
||||
(r) => r.birthday === todayIn2000
|
||||
);
|
||||
const names = isBirthdayToday.map((r) => `<@${r.userId}>`).join(", ");
|
||||
await channel.send(`Happy birthday to these lovely people~!\n${names}`);
|
||||
}
|
||||
} catch (err) {
|
||||
await errorHandler(bot, "post birthdays", err);
|
||||
}
|
||||
};
|
52
src/modules/subcommands/config/handleBirthdayChannel.ts
Normal file
52
src/modules/subcommands/config/handleBirthdayChannel.ts
Normal file
@ -0,0 +1,52 @@
|
||||
import { PermissionFlagsBits } from "discord.js";
|
||||
|
||||
import { CommandHandler } from "../../../interfaces/CommandHandler";
|
||||
import { errorHandler } from "../../../utils/errorHandler";
|
||||
import { setConfig } from "../../data/setConfig";
|
||||
|
||||
/**
|
||||
* Sets the birthday channel for the server.
|
||||
*/
|
||||
export const handleBirthdayChannel: CommandHandler = async (
|
||||
bot,
|
||||
interaction
|
||||
) => {
|
||||
try {
|
||||
const channel = interaction.options.getChannel("channel", true);
|
||||
if (!("send" in channel)) {
|
||||
await interaction.editReply({
|
||||
content: "You must specify a text channel!"
|
||||
});
|
||||
return;
|
||||
}
|
||||
const me = await interaction.guild.members.fetchMe();
|
||||
if (!me.permissionsIn(channel).has(PermissionFlagsBits.SendMessages)) {
|
||||
await interaction.editReply({
|
||||
content: "I can't send messages there. :c"
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const success = await setConfig(
|
||||
bot,
|
||||
interaction.guild.id,
|
||||
"birthdayChannel",
|
||||
channel.id
|
||||
);
|
||||
|
||||
if (success) {
|
||||
await interaction.editReply({
|
||||
content: `Birthdays will be posted in ${channel.toString()}. Members can set their birthdays with the \`/birthday\` command.`
|
||||
});
|
||||
return;
|
||||
}
|
||||
await interaction.editReply({
|
||||
content: "Failed to set the settings."
|
||||
});
|
||||
} catch (err) {
|
||||
const id = await errorHandler(bot, "automod logging subcommand", err);
|
||||
await interaction.editReply({
|
||||
content: `Something went wrong. Please [join our support server](https://chat.naomi.lgbt) and provide this ID: \`${id}\``
|
||||
});
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user