From 580b54c54989d179bdf19ca358ee6cf40e1d6778 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Sun, 3 Mar 2024 00:41:37 +0100 Subject: [PATCH] implement role assignment slash command --- commands/admin/member_roles/slash.js | 54 ++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/commands/admin/member_roles/slash.js b/commands/admin/member_roles/slash.js index 0a355d8..95cfeb9 100644 --- a/commands/admin/member_roles/slash.js +++ b/commands/admin/member_roles/slash.js @@ -1,4 +1,5 @@ import { SlashCommandBuilder, PermissionFlagsBits } from 'discord.js'; +import { Role } from '../../../database.js'; export const data = new SlashCommandBuilder() .setName('member_roles') @@ -31,4 +32,57 @@ export const data = new SlashCommandBuilder() /** @param {ChatInputCommandInteraction} interaction */ export async function execute(interaction) { const { options } = interaction; + + // Get command options + const role = options.getRole('role'); + switch (options.getSubcommand()) { + case 'add': + // 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 Role.create({ + id: role.id, + assign: true + }); + + // 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; + case 'remove': + // 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; + } }