From e69806c1321f8d391b057d7c3febc6765df8275e Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Thu, 8 Feb 2024 19:44:16 +0100 Subject: [PATCH] remove self roels contextcommand and shared function --- commands/admin/self_roles/context/remove.js | 11 +++++++++++ commands/admin/self_roles/slash.js | 21 +------------------- shared.js | 22 +++++++++++++++++++++ 3 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 commands/admin/self_roles/context/remove.js diff --git a/commands/admin/self_roles/context/remove.js b/commands/admin/self_roles/context/remove.js new file mode 100644 index 0000000..497aa8b --- /dev/null +++ b/commands/admin/self_roles/context/remove.js @@ -0,0 +1,11 @@ +import { removeSelfRoles } from '../../../../shared.js'; +import { ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js'; + +export const data = new ContextMenuCommandBuilder() + .setDMPermission(false) + .setName('Remove self roles') + .setType(ApplicationCommandType.Message); +export async function execute(interaction) { + const id = interaction.targetMessage.id; + await removeSelfRoles(interaction, id); +} diff --git a/commands/admin/self_roles/slash.js b/commands/admin/self_roles/slash.js index 1326ff9..c539059 100644 --- a/commands/admin/self_roles/slash.js +++ b/commands/admin/self_roles/slash.js @@ -66,26 +66,7 @@ const removeSelfRoles = async (interaction, msgID) => { return; } - // Try deleting message from database - const count = await Message.destroy({ - where: { - id: msgID - } - }); - - // 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} self roles from message!`, - ephemeral: true - }); - - console.info(`[INFO] Removed self roles from message with ID '${msgID}'.`); - + await removeSelfRoles(interaction, msgID); }; export const data = new SlashCommandBuilder() diff --git a/shared.js b/shared.js index 2676949..1ead0ea 100644 --- a/shared.js +++ b/shared.js @@ -3,6 +3,28 @@ import { Op } from 'sequelize'; import { readdir } from 'fs/promises'; import { Message, RoleEmojiPair } from './database.js'; +export const removeSelfRoles = async (interaction, id) => { + // Try deleting message from database + const count = await Message.destroy({ + where: { + id: id + } + }); + + // 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} self roles from message!`, + ephemeral: true + }); + + console.info(`[INFO] Removed self roles from message with ID '${id}'.`); +}; + const saveMessageData = async (id, role, emoji) => { // Try finding message const msg = await Message.findOne({ where: { id } });