2022-02-12 13:04:18 +00:00
|
|
|
const { SlashCommandBuilder } = require('@discordjs/builders');
|
|
|
|
const { MessageEmbed } = require('discord.js');
|
|
|
|
const help = require('../helpFunctions.js');
|
2023-02-17 08:33:56 +00:00
|
|
|
const bModel = require('../database/birthdaySchema.js');
|
2022-02-12 13:04:18 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
data: new SlashCommandBuilder()
|
|
|
|
.setName('birthday')
|
|
|
|
.setDescription('shows birthday')
|
|
|
|
.addSubcommand(subcommand =>
|
|
|
|
subcommand
|
|
|
|
.setName('check')
|
|
|
|
.setDescription('Checks who\'s birthday is the closest'))
|
|
|
|
.addSubcommand(subcommand =>
|
|
|
|
subcommand
|
|
|
|
.setName('add')
|
2022-02-18 12:54:33 +00:00
|
|
|
.setDescription('Adds user to birthday list')
|
2022-02-12 13:04:18 +00:00
|
|
|
.addUserOption(option => option.setName('user')
|
|
|
|
.setDescription('Select a user')
|
|
|
|
.setRequired(true))
|
|
|
|
.addIntegerOption(option =>
|
|
|
|
option.setName('day')
|
|
|
|
.setDescription('Day of birth')
|
|
|
|
.setRequired(true))
|
|
|
|
.addIntegerOption(option =>
|
|
|
|
option.setName('month')
|
|
|
|
.setDescription('Month of birth')
|
|
|
|
.setRequired(true))
|
|
|
|
.addStringOption(option =>
|
|
|
|
option.setName('nickname')
|
|
|
|
.setDescription('Nickname of birthday person')))
|
|
|
|
.addSubcommand(subcommand =>
|
|
|
|
subcommand
|
2022-02-18 12:54:33 +00:00
|
|
|
.setName('remove')
|
|
|
|
.setDescription('Removes user from birthday list')
|
2022-02-12 13:04:18 +00:00
|
|
|
.addUserOption(option => option.setName('user')
|
|
|
|
.setDescription('Select a user')
|
|
|
|
.setRequired(true)))
|
|
|
|
.addSubcommandGroup(subcommandGroup =>
|
|
|
|
subcommandGroup
|
|
|
|
.setName('change')
|
|
|
|
.setDescription('Change the birthday entry')
|
|
|
|
.addSubcommand(subcommand =>
|
|
|
|
subcommand
|
|
|
|
.setName('date')
|
2022-02-18 12:54:33 +00:00
|
|
|
.setDescription('Change date of a user')
|
2022-02-12 13:04:18 +00:00
|
|
|
.addUserOption(option => option.setName('user')
|
|
|
|
.setDescription('Select a user')
|
|
|
|
.setRequired(true))
|
|
|
|
.addIntegerOption(option =>
|
|
|
|
option.setName('day')
|
|
|
|
.setDescription('Day of birth')
|
|
|
|
.setRequired(true))
|
|
|
|
.addIntegerOption(option =>
|
|
|
|
option.setName('month')
|
|
|
|
.setDescription('Month of birth')
|
|
|
|
.setRequired(true)))
|
|
|
|
.addSubcommand(subcommand =>
|
|
|
|
subcommand
|
|
|
|
.setName('nickname')
|
2022-02-18 12:54:33 +00:00
|
|
|
.setDescription('Change nickname of a user')
|
2022-02-12 13:04:18 +00:00
|
|
|
.addUserOption(option => option.setName('user')
|
|
|
|
.setDescription('Select a user')
|
|
|
|
.setRequired(true))
|
|
|
|
.addStringOption(option =>
|
|
|
|
option.setName('nickname')
|
2022-02-18 12:54:33 +00:00
|
|
|
.setDescription('Nickname of birthday a user (can be empty to remove)')))),
|
2022-02-12 13:04:18 +00:00
|
|
|
async execute(interaction) {
|
2022-02-18 12:54:33 +00:00
|
|
|
const error = catchDateErrors(interaction.options);
|
2022-02-12 13:04:18 +00:00
|
|
|
if (error != null) {
|
|
|
|
await interaction.reply(error);
|
|
|
|
}
|
|
|
|
let subcommandGroup;
|
|
|
|
try {
|
|
|
|
subcommandGroup = interaction.options.getSubcommandGroup();
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
subcommandGroup = undefined;
|
|
|
|
}
|
|
|
|
const subcommand = interaction.options.getSubcommand();
|
|
|
|
if (subcommandGroup == undefined) {
|
|
|
|
switch (subcommand) {
|
|
|
|
case 'add':
|
2022-02-18 12:54:33 +00:00
|
|
|
await interaction.reply(await addBirthday(interaction.options));
|
2022-02-12 13:04:18 +00:00
|
|
|
break;
|
2022-02-18 12:54:33 +00:00
|
|
|
case 'remove':
|
|
|
|
await interaction.reply(await removeBirthday(interaction.options));
|
2022-02-12 13:04:18 +00:00
|
|
|
break;
|
|
|
|
case 'check':
|
2022-02-18 12:54:33 +00:00
|
|
|
await interaction.reply({ embeds: [await checkBirthday(interaction)] });
|
2022-02-12 13:04:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
switch (subcommand) {
|
|
|
|
case 'date':
|
2022-02-18 12:54:33 +00:00
|
|
|
await interaction.reply(await changeDate(interaction.options));
|
2022-02-12 13:04:18 +00:00
|
|
|
break;
|
|
|
|
case 'nickname':
|
2022-02-18 12:54:33 +00:00
|
|
|
await interaction.reply(await changeNickname(interaction.options));
|
2022-02-12 13:04:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
async function addBirthday(options) {
|
2022-02-12 13:04:18 +00:00
|
|
|
const userId = options.getUser('user').id;
|
|
|
|
const newDay = options.getInteger('day');
|
|
|
|
const newMonth = options.getInteger('month');
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
const nickname = options.getString('nickname');
|
|
|
|
|
2022-02-12 13:04:18 +00:00
|
|
|
try {
|
2022-02-18 12:54:33 +00:00
|
|
|
const dbEntry = await bModel.create({
|
|
|
|
id: userId,
|
|
|
|
day: newDay,
|
|
|
|
month: newMonth,
|
|
|
|
name: nickname,
|
|
|
|
});
|
|
|
|
dbEntry.save();
|
|
|
|
error = await sortTable();
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|
2022-02-18 12:54:33 +00:00
|
|
|
catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
return 'There was an error \n(user is probably already on the birthday list)';
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|
|
|
|
return `Successfuly added <@${userId}> to the birthday list`;
|
|
|
|
}
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
async function checkBirthday(interaction) {
|
|
|
|
const currDay = new Date().getDate();
|
2022-02-18 14:26:05 +00:00
|
|
|
const currMonth = new Date().getMonth() + 1;
|
2022-02-18 12:54:33 +00:00
|
|
|
|
2023-02-17 08:33:56 +00:00
|
|
|
const result = await bModel.find().exec();
|
2022-02-12 13:04:18 +00:00
|
|
|
|
|
|
|
let closestD;
|
|
|
|
let closestM;
|
2022-02-18 12:54:33 +00:00
|
|
|
const closest = [];
|
|
|
|
const guildMembers = interaction.guild.members;
|
2022-02-12 13:04:18 +00:00
|
|
|
let isFirst = true;
|
2022-02-18 12:54:33 +00:00
|
|
|
for (let i = 0; i < result.length; i++) {
|
|
|
|
const birthDay = result[i].day;
|
|
|
|
const birthMonth = result[i].month;
|
|
|
|
const userId = result[i].id;
|
|
|
|
const nick = result[i].nickname;
|
|
|
|
if ((currMonth == birthMonth && currDay <= birthDay) || currMonth < birthMonth) {
|
2022-02-18 14:26:05 +00:00
|
|
|
if (await isInGuild(guildMembers, userId)) {
|
|
|
|
if (isFirst) {
|
|
|
|
closestD = birthDay;
|
|
|
|
closestM = birthMonth;
|
|
|
|
isFirst = false;
|
|
|
|
}
|
|
|
|
if (!isFirst && (closestD == birthDay && closestM == birthMonth)) {
|
2022-02-18 12:54:33 +00:00
|
|
|
closest.push(`<@${userId}> ${nick}`);
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|
|
|
|
else {
|
2022-02-18 12:54:33 +00:00
|
|
|
const probably = getProbably();
|
|
|
|
const personList = closest.join('\n');
|
2022-02-12 13:04:18 +00:00
|
|
|
const embed = new MessageEmbed()
|
2022-02-18 14:26:05 +00:00
|
|
|
.setTitle(`Closest birthday is ${closestD}. ${closestM}.`)
|
|
|
|
.setDescription(`${personList} \n will celebrate... ${probably}`)
|
|
|
|
.setColor(help.randomColor());
|
2022-02-12 13:04:18 +00:00
|
|
|
return embed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-18 14:26:05 +00:00
|
|
|
|
|
|
|
if (closest.length > 0) {
|
2022-02-18 12:54:33 +00:00
|
|
|
const probably = getProbably();
|
|
|
|
const personList = closest.join('\n');
|
|
|
|
const embed = new MessageEmbed()
|
|
|
|
.setTitle(`Closest birthday is ${closestD}. ${closestM}.`)
|
2022-02-18 14:26:05 +00:00
|
|
|
.setDescription(`${personList} \n will celebrate... ${probably}`)
|
2022-02-18 12:54:33 +00:00
|
|
|
.setColor(help.randomColor());
|
|
|
|
return embed;
|
|
|
|
}
|
2022-02-12 13:04:18 +00:00
|
|
|
|
|
|
|
// ? if the closest is in next year -> closest is the first in list
|
2022-02-18 12:54:33 +00:00
|
|
|
closestD = result[0].day;
|
|
|
|
closestM = result[0].month;
|
2022-02-12 13:04:18 +00:00
|
|
|
// check if there are others with the same date just to be sure
|
2022-02-18 12:54:33 +00:00
|
|
|
for (let i = 0; i < result.length; i++) {
|
|
|
|
const birthDay = result[i].day;
|
|
|
|
const birthMonth = result[i].month;
|
|
|
|
const userId = result[i].id;
|
|
|
|
const nick = result[i].nickname;
|
2022-02-12 13:04:18 +00:00
|
|
|
|
|
|
|
if (closestD == birthDay && closestM == birthMonth) {
|
|
|
|
closest.push(`<@${userId}> ${nick}`);
|
|
|
|
}
|
|
|
|
else {
|
2022-02-18 12:54:33 +00:00
|
|
|
const probably = getProbably();
|
2022-02-12 13:04:18 +00:00
|
|
|
closest.join('\n');
|
|
|
|
const embed = new MessageEmbed()
|
2022-02-18 14:26:05 +00:00
|
|
|
.setTitle(`Closest birthday is ${closestD}. ${closestM}.`)
|
2022-02-12 13:04:18 +00:00
|
|
|
.setDescription(`${closest} \n will celebrate ${probably}`)
|
|
|
|
.setColor(help.randomColor());
|
|
|
|
return embed;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if noone from server is in the birthday list (and maybe something else)
|
|
|
|
const embed = new MessageEmbed()
|
|
|
|
.setTitle('Oh no...')
|
|
|
|
.setDescription('There was an error');
|
|
|
|
return embed;
|
|
|
|
}
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
async function removeBirthday(options) {
|
2022-02-12 13:04:18 +00:00
|
|
|
const userId = options.getUser('user').id;
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
let error = null;
|
|
|
|
await bModel.deleteOne({ id: userId }), function(err) {
|
|
|
|
if (err) error = err;
|
|
|
|
};
|
|
|
|
if (error) return 'There was an error';
|
|
|
|
return `Successfuly deleted <@${userId}> from birthday list`;
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
function catchDateErrors(options) {
|
2022-02-12 13:04:18 +00:00
|
|
|
const month = options.getInteger('month');
|
|
|
|
const day = options.getInteger('day');
|
|
|
|
if (month == null || day == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (month > 12 || month < 1) {
|
|
|
|
return 'Bruh, "month" kinda poopy';
|
|
|
|
}
|
|
|
|
if (day > checkMonth(month) || day < 1) {
|
|
|
|
return 'Bruh, "day" kinda poopy';
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
async function changeDate(options) {
|
2022-02-12 13:04:18 +00:00
|
|
|
const userId = options.getUser('user').id;
|
2022-02-18 12:54:33 +00:00
|
|
|
const newDay = options.getInteger('day');
|
|
|
|
const newMonth = options.getInteger('month');
|
2022-02-12 13:04:18 +00:00
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
try {
|
|
|
|
await bModel.findOneAndUpdate({ id: userId }, { $set: { day: newDay, month: newMonth } });
|
|
|
|
sortTable();
|
|
|
|
}
|
|
|
|
catch (err) {
|
|
|
|
console.log(err);
|
|
|
|
return 'There was an error while updating the birthday list';
|
|
|
|
}
|
|
|
|
return `Successfuly changed birthday date of <@${userId}> to ${newDay}. ${newMonth}.`;
|
|
|
|
}
|
2022-02-12 13:04:18 +00:00
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
async function changeNickname(options) {
|
|
|
|
const userId = options.getUser('user').id;
|
|
|
|
let nick = options.getString('nickname');
|
|
|
|
if (nick == null) nick = '';
|
|
|
|
try {
|
|
|
|
await bModel.findOneAndUpdate({ id: userId }, { $set: { nickname: nick } });
|
|
|
|
}
|
|
|
|
catch {
|
|
|
|
return 'There was an error';
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|
2022-02-18 12:54:33 +00:00
|
|
|
return `Succesfully change nickname of <@${userId}> to ${nick}`;
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
async function sortTable() {
|
|
|
|
const query = bModel.find({}).sort({ month: 'asc', day: 'asc' });
|
|
|
|
const result = await query.exec();
|
|
|
|
let error;
|
|
|
|
await bModel.deleteMany({}), function(err) {
|
|
|
|
if (err) error = err;
|
|
|
|
};
|
2022-02-12 13:04:18 +00:00
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
if (error) return error;
|
2022-02-12 13:04:18 +00:00
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
for (let i = 0; i < result.length; i++) {
|
|
|
|
const entry = await bModel.create({
|
|
|
|
id: result[i].id,
|
|
|
|
day: result[i].day,
|
|
|
|
month: result[i].month,
|
|
|
|
nickname: result[i].nickname,
|
|
|
|
});
|
|
|
|
entry.save();
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|
2022-02-18 12:54:33 +00:00
|
|
|
|
|
|
|
return null;
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|
|
|
|
|
2022-02-18 12:54:33 +00:00
|
|
|
function getProbably() {
|
|
|
|
const rng = help.RNG(6);
|
|
|
|
switch (rng) {
|
|
|
|
case 0:
|
|
|
|
return 'probably';
|
|
|
|
case 1:
|
|
|
|
return 'or not';
|
|
|
|
case 2:
|
|
|
|
return 'or will they?';
|
|
|
|
case 3:
|
2022-02-18 14:26:05 +00:00
|
|
|
return '\n You may be older, but I still love you the same don\'t worry';
|
2022-02-18 12:54:33 +00:00
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkMonth(month) {
|
2022-02-12 13:04:18 +00:00
|
|
|
switch (month) {
|
|
|
|
case 1:
|
|
|
|
return 31;
|
|
|
|
case 2:
|
|
|
|
return 28;
|
|
|
|
case 3:
|
|
|
|
return 31;
|
|
|
|
case 4:
|
|
|
|
return 30;
|
|
|
|
case 5:
|
|
|
|
return 31;
|
|
|
|
case 6:
|
|
|
|
return 30;
|
|
|
|
case 7:
|
|
|
|
return 31;
|
|
|
|
case 8:
|
|
|
|
return 31;
|
|
|
|
case 9:
|
|
|
|
return 30;
|
|
|
|
case 10:
|
|
|
|
return 31;
|
|
|
|
case 11:
|
|
|
|
return 30;
|
|
|
|
case 12:
|
|
|
|
return 31;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function isInGuild(guildMembers, userId) {
|
2022-02-18 14:26:05 +00:00
|
|
|
try {
|
|
|
|
await guildMembers.fetch(userId);
|
|
|
|
}
|
|
|
|
catch {
|
2022-02-18 12:54:33 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2022-02-12 13:04:18 +00:00
|
|
|
}
|