From 61b8a9a2ecded1ef705c8f9bf795d69e6b435611 Mon Sep 17 00:00:00 2001 From: Baipyrus Date: Mon, 5 Feb 2024 23:26:49 +0100 Subject: [PATCH] shared add self role pair function --- commands/admin/ctx_add_self_roles.js | 60 ++-------------------- commands/admin/slash_self_roles.js | 75 ++++------------------------ shared.js | 56 +++++++++++++++++++++ 3 files changed, 69 insertions(+), 122 deletions(-) create mode 100644 shared.js diff --git a/commands/admin/ctx_add_self_roles.js b/commands/admin/ctx_add_self_roles.js index 95171b4..5b6c512 100644 --- a/commands/admin/ctx_add_self_roles.js +++ b/commands/admin/ctx_add_self_roles.js @@ -1,65 +1,11 @@ import { TextInputBuilder, TextInputStyle } from 'discord.js'; -import { RoleEmojiPair } from '../../database.js'; import { + ModalBuilder, ActionRowBuilder, ApplicationCommandType, ContextMenuCommandBuilder } from 'discord.js'; -import { ModalBuilder } from 'discord.js'; - -const saveMessageData = async (id, role, emoji) => { - // Try finding existing entry - const rep = await RoleEmojiPair.findOne({ - where: { - [Op.or]: [ - { - message: id, - role: role.id - }, { - message: id, - emoji - } - ] - } - }); - if (rep !== null) throw new Error(`Failed to fetch RoleEmojiPair entry with data {message:${id},role:${role.id},emoji:${emoji}}!`); - - // Create database entry for pair - await RoleEmojiPair.create({ message: id, role: role.id, emoji }); -}; - -const addSelfRoles = async (interaction, msgID, emoji, role) => { - const { channel } = interaction; - - let step = 'fetch'; - try { - // Get message by id - const message = await channel.messages.fetch(msgID); - - step = 'save data from'; - await saveMessageData(id, role, emoji); - - step = 'react to'; - // React with emoji to message - await message.react(emoji); - - // Reply successfully to acknowledge command - await interaction.reply({ - content: 'Added new entry for self roles!', - ephemeral: true, - }); - - console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`); - } catch (error) { - console.error(error); - - // Reply failed to acknowledge command - await interaction.reply({ - content: `Failed to ${step} message!`, - ephemeral: true, - }); - } -} +import { addSelfRoles } from '../../shared.js'; export const data = new ContextMenuCommandBuilder() .setDMPermission(false) @@ -83,7 +29,7 @@ export async function modalSubmit(interaction) { return; } - await addSelfRoles(interaction, message, emoji, role); + await addSelfRoles(interaction, message, role, emoji); } export async function execute(interaction) { const modal = new ModalBuilder() diff --git a/commands/admin/slash_self_roles.js b/commands/admin/slash_self_roles.js index 8a28402..f9b2751 100644 --- a/commands/admin/slash_self_roles.js +++ b/commands/admin/slash_self_roles.js @@ -1,6 +1,7 @@ import { Op } from 'sequelize'; import { SlashCommandBuilder } from 'discord.js'; import { Message, RoleEmojiPair } from './../../database.js'; +import { addSelfRoles } from '../../shared.js'; const createSelfRoles = async (interaction) => { const { options, channel } = interaction; @@ -50,67 +51,6 @@ const registerSelfRoles = async (interaction) => { return response; }; -const saveMessageData = async (id, role, emoji) => { - // Try finding existing entry - const rep = await RoleEmojiPair.findOne({ - where: { - [Op.or]: [ - { - message: id, - role: role.id - }, { - message: id, - emoji - } - ] - } - }); - if (rep !== null) throw new Error(`Failed to fetch RoleEmojiPair entry with data {message:${id},role:${role.id},emoji:${emoji}}!`); - - // Create database entry for pair - await RoleEmojiPair.create({ message: id, role: role.id, emoji }); -}; - -const addSelfRoles = async (interaction) => { - const { options, channel } = interaction; - const id = options.getString('id'); - - let step = 'fetch'; - try { - // Get message by id - const message = await channel.messages.fetch(id); - - // Get user arguments - const role = options.getRole('role'); - const emoji = options - .getString('emoji') - .replace(/:.*?:/, ':_:'); - - step = 'save data from'; - await saveMessageData(id, role, emoji); - - step = 'react to'; - // React with emoji to message - await message.react(emoji); - - // Reply successfully to acknowledge command - await interaction.reply({ - content: 'Added new entry for self roles!', - ephemeral: true, - }); - - console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`); - } catch (error) { - console.error(error); - - // Reply failed to acknowledge command - await interaction.reply({ - content: `Failed to ${step} message!`, - ephemeral: true, - }); - } -} - export const data = new SlashCommandBuilder() .setName('self_roles') .setDMPermission(false) @@ -163,13 +103,18 @@ export async function execute(interaction) { createNew = true; break; case 'register': - const { success, msgID } = await registerSelfRoles(interaction); - id = msgID ?? id; + const response = await registerSelfRoles(interaction); + id = response.msgID ?? id; // Flag to create new database entry - createNew = success; + createNew = response.success; break; case 'add': - await addSelfRoles(interaction); + // Get command options + const msgID = options.getString('id'); + const role = options.getRole('role'); + const emoji = options.getString('emoji'); + // Try adding self role pair + await addSelfRoles(interaction, msgID, role, emoji); break; } diff --git a/shared.js b/shared.js new file mode 100644 index 0000000..4a5e067 --- /dev/null +++ b/shared.js @@ -0,0 +1,56 @@ +import { Op } from "sequelize"; +import { RoleEmojiPair } from "./database.js"; + +const saveMessageData = async (id, role, emoji) => { + // Try finding existing entry + const rep = await RoleEmojiPair.findOne({ + where: { + [Op.or]: [ + { + message: id, + role: role.id + }, { + message: id, + emoji + } + ] + } + }); + if (rep !== null) throw new Error(`Failed to fetch RoleEmojiPair entry with data {message:${id},role:${role.id},emoji:${emoji}}!`); + + // Create database entry for pair + await RoleEmojiPair.create({ message: id, role: role.id, emoji }); +}; + +export const addSelfRoles = async (interaction, msgID, role, emoji) => { + const { channel } = interaction; + + let step = 'fetch'; + try { + // Get message by id + const message = await channel.messages.fetch(msgID); + + step = 'save data from'; + await saveMessageData(msgID, role, emoji); + + step = 'react to'; + // React with emoji to message + await message.react(emoji); + + // Reply successfully to acknowledge command + await interaction.reply({ + content: 'Added new entry for self roles!', + ephemeral: true, + }); + + console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`); + } catch (error) { + console.error(error); + + // Reply failed to acknowledge command + await interaction.reply({ + content: `Failed to ${step} message!`, + ephemeral: true, + }); + } +}