2024-02-11 01:04:12 +00:00
|
|
|
import { PermissionFlagsBits, SlashCommandBuilder, ChatInputCommandInteraction } from 'discord.js';
|
2024-04-05 08:30:41 +00:00
|
|
|
import { addSelfRoles, removeSelfRoles } from '../../shared.js';
|
|
|
|
import { Guilds, Messages } from '../../database.js';
|
2024-01-29 01:10:16 +00:00
|
|
|
|
2024-02-11 01:04:12 +00:00
|
|
|
/**
|
|
|
|
* Sends a `Message` in the current channel and registers for self roles.
|
|
|
|
* @param {ChatInputCommandInteraction} interaction
|
2024-02-13 19:29:02 +00:00
|
|
|
* @returns {Promise<string>}
|
2024-02-11 01:04:12 +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;
|
|
|
|
|
2024-02-16 16:37:53 +00:00
|
|
|
// Reply successfully to acknowledge command
|
|
|
|
await interaction.reply({
|
|
|
|
content: `Successfully sent message! Add roles to it with reference ID '${id}'.`,
|
|
|
|
ephemeral: true
|
|
|
|
});
|
2024-02-04 22:40:07 +00:00
|
|
|
|
|
|
|
return id;
|
|
|
|
};
|
|
|
|
|
2024-02-11 01:04:12 +00:00
|
|
|
/**
|
|
|
|
* @typedef {Object} SelfRoleResponse
|
|
|
|
* @property {boolean} success Whether or not the operation was successful.
|
|
|
|
* @property {string|null} msgID A Discord message ID, if successful.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registers a `Message` for self roles.
|
|
|
|
* @param {ChatInputCommandInteraction} interaction
|
|
|
|
* @returns {Promise<SelfRoleResponse>}
|
|
|
|
*/
|
2024-02-04 22:40:07 +00:00
|
|
|
const registerSelfRoles = async (interaction) => {
|
|
|
|
const { options, channel } = interaction;
|
|
|
|
const id = options.getString('id');
|
2024-04-03 16:44:20 +00:00
|
|
|
/** @type {SelfRoleResponse} */
|
2024-02-04 22:40:07 +00:00
|
|
|
const response = {
|
|
|
|
success: false,
|
2024-02-06 15:18:06 +00:00
|
|
|
msgID: null
|
2024-02-04 22:40:07 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get message by id
|
|
|
|
await channel.messages.fetch(id);
|
|
|
|
|
2024-03-03 00:44:27 +00:00
|
|
|
// Check if message is already registered
|
2024-04-04 07:55:14 +00:00
|
|
|
/** @type {import('../../../models/messages.js').Message|null} */
|
2024-04-04 06:59:37 +00:00
|
|
|
const found = await Messages.findOne({
|
2024-03-03 00:44:27 +00:00
|
|
|
where: { id }
|
|
|
|
});
|
|
|
|
|
2024-04-04 07:47:00 +00:00
|
|
|
if (found !== null) throw new Error('Messages already registered!');
|
2024-03-03 00:44:27 +00:00
|
|
|
|
2024-02-04 22:40:07 +00:00
|
|
|
// Reply successfully to acknowledge command
|
|
|
|
await interaction.reply({
|
|
|
|
content: 'Successfully fetched message!',
|
2024-02-06 15:18:06 +00:00
|
|
|
ephemeral: true
|
2024-02-04 22:40:07 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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!',
|
2024-02-06 15:18:06 +00:00
|
|
|
ephemeral: true
|
2024-02-04 22:40:07 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2024-02-11 01:04:12 +00:00
|
|
|
/**
|
|
|
|
* Main logic of the 'Self Roles remove' slash command to remove the functionality from a `Message`.
|
|
|
|
* @param {ChatInputCommandInteraction} interaction
|
|
|
|
* @param {string} msgID A Discord message ID.
|
|
|
|
*/
|
|
|
|
const removeReactionRoles = async (interaction, msgID) => {
|
2024-02-08 18:34:01 +00:00
|
|
|
const { channel } = interaction;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Try fetching message from channel
|
|
|
|
await channel.messages.fetch(msgID);
|
|
|
|
} catch (error) {
|
|
|
|
console.error(error);
|
|
|
|
// Reply to acknowledge command
|
|
|
|
await interaction.reply({
|
|
|
|
content: `Failed to fetch message!`,
|
|
|
|
ephemeral: true
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-02-11 01:04:12 +00:00
|
|
|
// Call shared method for further logic
|
2024-02-08 18:44:16 +00:00
|
|
|
await removeSelfRoles(interaction, msgID);
|
2024-02-08 18:34:01 +00:00
|
|
|
};
|
|
|
|
|
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.')
|
2024-02-09 21:41:58 +00:00
|
|
|
.setDefaultMemberPermissions(PermissionFlagsBits.ManageRoles)
|
2024-02-06 15:18:06 +00:00
|
|
|
.addSubcommand((subcommand) =>
|
2024-01-29 01:10:16 +00:00
|
|
|
subcommand
|
|
|
|
.setName('create')
|
|
|
|
.setDescription('Creates new message in channel.')
|
2024-02-06 15:18:06 +00:00
|
|
|
.addStringOption((option) =>
|
2024-01-29 01:10:16 +00:00
|
|
|
option
|
|
|
|
.setName('text')
|
2024-02-05 02:01:48 +00:00
|
|
|
.setRequired(true)
|
2024-02-06 15:18:06 +00:00
|
|
|
.setDescription('The text to be displayed in the message.')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.addSubcommand((subcommand) =>
|
2024-01-29 01:35:29 +00:00
|
|
|
subcommand
|
|
|
|
.setName('register')
|
|
|
|
.setDescription('Registers an existing message.')
|
2024-02-06 15:18:06 +00:00
|
|
|
.addStringOption((option) =>
|
2024-01-29 01:35:29 +00:00
|
|
|
option
|
|
|
|
.setName('id')
|
|
|
|
.setRequired(true)
|
2024-02-06 15:18:06 +00:00
|
|
|
.setDescription('The ID to reference the message to be used.')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.addSubcommand((subcommand) =>
|
2024-01-29 01:10:16 +00:00
|
|
|
subcommand
|
|
|
|
.setName('add')
|
2024-01-29 01:35:29 +00:00
|
|
|
.setDescription('Add a role-emoji-pair to a message.')
|
2024-02-06 15:18:06 +00:00
|
|
|
.addStringOption((option) =>
|
2024-01-29 01:10:16 +00:00
|
|
|
option
|
|
|
|
.setName('id')
|
|
|
|
.setRequired(true)
|
2024-02-06 15:18:06 +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-02-08 18:34:01 +00:00
|
|
|
)
|
|
|
|
.addSubcommand((subcommand) =>
|
|
|
|
subcommand
|
|
|
|
.setName('remove')
|
|
|
|
.setDescription('Remove self roles from a message.')
|
|
|
|
.addStringOption((option) =>
|
|
|
|
option
|
|
|
|
.setName('id')
|
|
|
|
.setRequired(true)
|
|
|
|
.setDescription('The ID to reference the message to be removed.')
|
|
|
|
)
|
2024-02-06 15:18:06 +00:00
|
|
|
);
|
2024-02-11 01:04:12 +00:00
|
|
|
/** @param {ChatInputCommandInteraction} interaction */
|
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-11 01:04:12 +00:00
|
|
|
/** @type {string=} */
|
|
|
|
let id;
|
|
|
|
let createNew = false;
|
2024-02-04 22:40:07 +00:00
|
|
|
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;
|
2024-02-06 15:18:06 +00:00
|
|
|
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;
|
2024-02-06 15:18:06 +00:00
|
|
|
}
|
|
|
|
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-02-06 15:18:06 +00:00
|
|
|
}
|
2024-02-08 18:34:01 +00:00
|
|
|
case 'remove': {
|
|
|
|
const msgID = options.getString('id');
|
2024-02-11 01:04:12 +00:00
|
|
|
await removeReactionRoles(interaction, msgID);
|
2024-02-08 18:34:01 +00:00
|
|
|
break;
|
|
|
|
}
|
2024-01-29 01:10:16 +00:00
|
|
|
}
|
|
|
|
|
2024-01-29 14:00:30 +00:00
|
|
|
if (createNew) {
|
2024-02-04 23:16:06 +00:00
|
|
|
try {
|
2024-03-03 00:44:27 +00:00
|
|
|
// Create guild if not exists
|
2024-04-04 18:22:28 +00:00
|
|
|
const guildData = { id: interaction.guildId };
|
2024-04-04 06:59:37 +00:00
|
|
|
await Guilds.findOrCreate({
|
2024-03-03 00:44:27 +00:00
|
|
|
where: guildData,
|
|
|
|
defaults: guildData
|
|
|
|
});
|
|
|
|
|
2024-02-04 23:16:06 +00:00
|
|
|
// Create database entry
|
2024-04-04 06:59:37 +00:00
|
|
|
await Messages.create({
|
2024-03-03 00:44:27 +00:00
|
|
|
id,
|
2024-04-04 18:22:28 +00:00
|
|
|
guild: interaction.guildId
|
2024-03-03 00:44:27 +00:00
|
|
|
});
|
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!',
|
2024-02-06 15:18:06 +00:00
|
|
|
ephemeral: true
|
2024-02-04 23:16:06 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|