2024-03-23 21:49:42 +00:00
|
|
|
import { SlashCommandBuilder, PermissionFlagsBits, ChatInputCommandInteraction } from 'discord.js';
|
2024-03-23 21:47:24 +00:00
|
|
|
import { Role, Guild } from '../../../database.js';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {Guild} guild
|
|
|
|
* @param {Role} role
|
|
|
|
*/
|
|
|
|
const registerRole = async (guild, role) => {
|
|
|
|
// Check if guild exists in database, otherwise create it
|
|
|
|
const guildData = { id: guild.id };
|
|
|
|
await Guild.findOrCreate({
|
|
|
|
where: guildData,
|
|
|
|
defaults: guildData
|
|
|
|
});
|
|
|
|
|
|
|
|
// Register role in database
|
|
|
|
await Role.create({
|
|
|
|
guild: guild.id,
|
|
|
|
id: role.id,
|
|
|
|
assign: true
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const data = new SlashCommandBuilder()
|
|
|
|
.setName('member_roles')
|
|
|
|
.setDMPermission(false)
|
|
|
|
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
|
|
|
|
.setDescription('Assigns roles to new members.')
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
subcommand
|
|
|
|
.setName('add')
|
|
|
|
.setDescription('Registers a role to be assigned to new members.')
|
|
|
|
.addRoleOption((option) =>
|
|
|
|
option
|
|
|
|
.setName('role')
|
|
|
|
.setDescription('The role to assign to new members.')
|
|
|
|
.setRequired(true)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
subcommand
|
|
|
|
.setName('remove')
|
|
|
|
.setDescription('Unregisters a role from new member assignment.')
|
|
|
|
.addRoleOption((option) =>
|
|
|
|
option
|
|
|
|
.setName('role')
|
|
|
|
.setDescription('The role to unregister from assignmment.')
|
|
|
|
.setRequired(true)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
/** @param {ChatInputCommandInteraction} interaction */
|
|
|
|
export async function execute(interaction) {
|
|
|
|
const { options } = interaction;
|
|
|
|
|
|
|
|
// Get command options
|
|
|
|
const role = options.getRole('role');
|
|
|
|
switch (options.getSubcommand()) {
|
2024-03-23 21:49:57 +00:00
|
|
|
case 'add': {
|
2024-03-23 21:47:24 +00:00
|
|
|
// Search for role in database
|
|
|
|
const found = await Role.findOne({
|
|
|
|
where: {
|
|
|
|
id: role.id
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Toggle role assignment if found
|
|
|
|
if (found) {
|
|
|
|
found.assign = true;
|
|
|
|
await found.save();
|
|
|
|
// Otherwise create new database entry
|
|
|
|
} else await registerRole(interaction.guild, role);
|
|
|
|
// Reply successfully to acknowledge command
|
|
|
|
await interaction.reply({
|
|
|
|
content: 'Successfully registered role.',
|
|
|
|
ephemeral: true
|
|
|
|
});
|
|
|
|
|
|
|
|
console.info(`[INFO] Registered role to be assigned with ID '${role.id}'.`);
|
|
|
|
break;
|
2024-03-23 21:49:57 +00:00
|
|
|
}
|
|
|
|
case 'remove': {
|
2024-03-23 21:47:24 +00:00
|
|
|
// Remove role from database
|
|
|
|
const count = await Role.destroy({
|
|
|
|
where: {
|
|
|
|
id: role.id,
|
|
|
|
assign: true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Set reply based on result of deletion
|
|
|
|
let response = 'Successfully removed';
|
|
|
|
if (count === 0) response = 'Failed to remove';
|
|
|
|
|
|
|
|
// Reply to acknowledge command
|
|
|
|
await interaction.reply({
|
|
|
|
content: `${response} role from new member assignment!`,
|
|
|
|
ephemeral: true
|
|
|
|
});
|
|
|
|
|
|
|
|
console.info(`[INFO] Removed role to be assigned with ID '${role.id}'.`);
|
|
|
|
break;
|
2024-03-23 21:49:57 +00:00
|
|
|
}
|
2024-03-23 21:47:24 +00:00
|
|
|
}
|
|
|
|
}
|