2024-02-05 21:57:08 +00:00
|
|
|
import { TextInputBuilder, TextInputStyle } from 'discord.js';
|
|
|
|
import { RoleEmojiPair } from '../../database.js';
|
|
|
|
import {
|
|
|
|
ComponentType,
|
|
|
|
ActionRowBuilder,
|
|
|
|
RoleSelectMenuBuilder,
|
|
|
|
ApplicationCommandType,
|
|
|
|
ContextMenuCommandBuilder
|
|
|
|
} from 'discord.js';
|
|
|
|
import { ModalBuilder } from 'discord.js';
|
2024-02-04 23:16:52 +00:00
|
|
|
|
2024-02-05 21:57:08 +00:00
|
|
|
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;
|
2024-02-04 23:16:52 +00:00
|
|
|
|
2024-02-05 21:57:08 +00:00
|
|
|
let step = 'fetch';
|
2024-02-04 23:16:52 +00:00
|
|
|
try {
|
2024-02-05 21:57:08 +00:00
|
|
|
// 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);
|
2024-02-04 23:16:52 +00:00
|
|
|
|
|
|
|
// Reply successfully to acknowledge command
|
|
|
|
await interaction.reply({
|
2024-02-05 21:57:08 +00:00
|
|
|
content: 'Added new entry for self roles!',
|
2024-02-04 23:16:52 +00:00
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
|
2024-02-05 21:57:08 +00:00
|
|
|
console.info(`[INFO] Added new entry to get role with ID '${role.id}' using '${emoji}'.`);
|
2024-02-04 23:16:52 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
2024-02-05 21:57:08 +00:00
|
|
|
|
2024-02-04 23:16:52 +00:00
|
|
|
// Reply failed to acknowledge command
|
|
|
|
await interaction.reply({
|
2024-02-05 21:57:08 +00:00
|
|
|
content: `Failed to ${step} message!`,
|
2024-02-04 23:16:52 +00:00
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2024-02-05 21:57:08 +00:00
|
|
|
|
|
|
|
export const data = new ContextMenuCommandBuilder()
|
|
|
|
.setDMPermission(false)
|
|
|
|
.setName('Add role emoji pair')
|
|
|
|
.setType(ApplicationCommandType.Message);
|
|
|
|
export async function modalSubmit(interaction) {
|
|
|
|
console.log(interaction);
|
|
|
|
|
|
|
|
// await interaction.reply({
|
|
|
|
// content: 'Successfully submitted Form!',
|
|
|
|
// ephemeral: true
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
export async function execute(interaction) {
|
|
|
|
const modal = new ModalBuilder()
|
|
|
|
.setCustomId('Add role emoji pair-pair')
|
|
|
|
.setTitle('Role Emoji Pair');
|
|
|
|
|
|
|
|
const roleSelector = new ActionRowBuilder().addComponents(
|
|
|
|
new TextInputBuilder()
|
|
|
|
.setLabel('Enter exactly one role ID.')
|
|
|
|
.setStyle(TextInputStyle.Short)
|
|
|
|
.setCustomId('role')
|
|
|
|
.setRequired(true));
|
|
|
|
|
|
|
|
// const roles = await interaction.guild.roles.fetch();
|
|
|
|
// const selection = roles.find((r) =>
|
|
|
|
// roleInteraction.values.includes(r.id)
|
|
|
|
// );
|
|
|
|
|
|
|
|
const emojiTextInput = new ActionRowBuilder().addComponents(
|
|
|
|
new TextInputBuilder()
|
|
|
|
.setLabel('Enter exactly one emoji.')
|
|
|
|
.setStyle(TextInputStyle.Short)
|
|
|
|
.setCustomId('emoji')
|
|
|
|
.setRequired(true));
|
|
|
|
|
|
|
|
// const id = interaction.targetMessage.id;
|
|
|
|
// await addSelfRoles(interaction, id, emoji, role);
|
|
|
|
|
|
|
|
modal.addComponents(roleSelector, emojiTextInput);
|
|
|
|
|
|
|
|
await interaction.showModal(modal);
|
|
|
|
}
|