2024-02-04 22:40:07 +00:00
|
|
|
import { Op } from 'sequelize';
|
2024-01-29 01:10:16 +00:00
|
|
|
import { SlashCommandBuilder } from 'discord.js';
|
2024-01-29 14:00:30 +00:00
|
|
|
import { Message, RoleEmojiPair } from './../../database.js';
|
2024-02-05 22:26:49 +00:00
|
|
|
import { addSelfRoles } from '../../shared.js';
|
2024-01-29 01:10:16 +00:00
|
|
|
|
2024-02-04 22:40:07 +00:00
|
|
|
const createSelfRoles = async (interaction) => {
|
|
|
|
const { options, channel } = interaction;
|
|
|
|
|
|
|
|
// Create message with text input
|
|
|
|
const text = options.getString('text');
|
|
|
|
const id = (await channel.send(text)).id;
|
|
|
|
|
|
|
|
// Reply and delete to acknowledge command
|
2024-02-05 02:01:48 +00:00
|
|
|
await interaction.deferReply();
|
|
|
|
await interaction.deleteReply();
|
2024-02-04 22:40:07 +00:00
|
|
|
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
|
|
|
const registerSelfRoles = async (interaction) => {
|
|
|
|
const { options, channel } = interaction;
|
|
|
|
const id = options.getString('id');
|
|
|
|
const response = {
|
|
|
|
success: false,
|
|
|
|
msgID: null,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get message by id
|
|
|
|
await channel.messages.fetch(id);
|
|
|
|
|
|
|
|
// Reply successfully to acknowledge command
|
|
|
|
await interaction.reply({
|
|
|
|
content: 'Successfully fetched message!',
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
response.success = true;
|
|
|
|
response.msgID = id;
|
|
|
|
|
|
|
|
return response;
|
2024-02-04 23:16:06 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
|
2024-02-04 22:40:07 +00:00
|
|
|
// Reply failed to acknowledge command
|
|
|
|
await interaction.reply({
|
|
|
|
content: 'Failed to fetch message!',
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2024-01-29 01:10:16 +00:00
|
|
|
export const data = new SlashCommandBuilder()
|
|
|
|
.setName('self_roles')
|
2024-02-05 20:26:47 +00:00
|
|
|
.setDMPermission(false)
|
2024-01-29 01:10:16 +00:00
|
|
|
.setDescription('Manages reactions for self roles.')
|
|
|
|
.addSubcommand(subcommand =>
|
|
|
|
subcommand
|
|
|
|
.setName('create')
|
|
|
|
.setDescription('Creates new message in channel.')
|
|
|
|
.addStringOption(option =>
|
|
|
|
option
|
|
|
|
.setName('text')
|
2024-02-05 02:01:48 +00:00
|
|
|
.setRequired(true)
|
2024-01-29 01:10:16 +00:00
|
|
|
.setDescription('The text to be displayed in the message.')))
|
2024-01-29 01:35:29 +00:00
|
|
|
.addSubcommand(subcommand =>
|
|
|
|
subcommand
|
|
|
|
.setName('register')
|
|
|
|
.setDescription('Registers an existing message.')
|
|
|
|
.addStringOption(option =>
|
|
|
|
option
|
|
|
|
.setName('id')
|
|
|
|
.setRequired(true)
|
|
|
|
.setDescription('The ID to reference the message to be used.')))
|
2024-01-29 01:10:16 +00:00
|
|
|
.addSubcommand(subcommand =>
|
|
|
|
subcommand
|
|
|
|
.setName('add')
|
2024-01-29 01:35:29 +00:00
|
|
|
.setDescription('Add a role-emoji-pair to a message.')
|
2024-01-29 01:10:16 +00:00
|
|
|
.addStringOption(option =>
|
|
|
|
option
|
|
|
|
.setName('id')
|
|
|
|
.setRequired(true)
|
2024-01-29 01:35:29 +00:00
|
|
|
.setDescription('The ID to reference the message to be used.'))
|
|
|
|
.addRoleOption(option =>
|
|
|
|
option
|
|
|
|
.setName('role')
|
|
|
|
.setRequired(true)
|
|
|
|
.setDescription('The role be assigned to.'))
|
|
|
|
.addStringOption(option =>
|
|
|
|
option
|
|
|
|
.setName('emoji')
|
|
|
|
.setRequired(true)
|
|
|
|
.setDescription('The emoji to be reacted with.')));
|
2024-01-29 01:10:16 +00:00
|
|
|
export async function execute(interaction) {
|
2024-02-04 22:40:07 +00:00
|
|
|
const { options } = interaction;
|
2024-01-29 01:10:16 +00:00
|
|
|
|
2024-02-04 22:40:07 +00:00
|
|
|
let createNew = false, id;
|
|
|
|
switch (options.getSubcommand()) {
|
2024-01-29 01:10:16 +00:00
|
|
|
case 'create':
|
2024-02-04 22:40:07 +00:00
|
|
|
id = await createSelfRoles(interaction);
|
2024-01-29 01:35:29 +00:00
|
|
|
// Flag to create new database entry
|
|
|
|
createNew = true;
|
|
|
|
break;
|
|
|
|
case 'register':
|
2024-02-05 22:26:49 +00:00
|
|
|
const response = await registerSelfRoles(interaction);
|
|
|
|
id = response.msgID ?? id;
|
2024-02-04 22:40:07 +00:00
|
|
|
// Flag to create new database entry
|
2024-02-05 22:26:49 +00:00
|
|
|
createNew = response.success;
|
2024-01-29 01:10:16 +00:00
|
|
|
break;
|
|
|
|
case 'add':
|
2024-02-05 22:26:49 +00:00
|
|
|
// 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);
|
2024-01-29 01:10:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2024-01-29 14:00:30 +00:00
|
|
|
if (createNew) {
|
2024-02-04 23:16:06 +00:00
|
|
|
try {
|
|
|
|
// Create database entry
|
2024-02-05 02:01:48 +00:00
|
|
|
await Message.create({ id });
|
2024-02-04 23:16:06 +00:00
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
|
|
|
|
// Reply failed to acknowledge command
|
|
|
|
await interaction.followUp({
|
|
|
|
content: 'Failed to save data from message!',
|
|
|
|
ephemeral: true,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-02-04 22:40:07 +00:00
|
|
|
console.info(`[INFO] New self roles on message with ID: '${id}'.`);
|
2024-01-29 14:00:30 +00:00
|
|
|
}
|
2024-01-29 01:10:16 +00:00
|
|
|
}
|