DiscordJS-Template/commands/admin/slash_self_roles.js

192 lines
4.6 KiB
JavaScript
Raw Normal View History

import { Op } from 'sequelize';
2024-01-29 01:10:16 +00:00
import { SlashCommandBuilder } from 'discord.js';
import { Message, RoleEmojiPair } from './../../database.js';
2024-01-29 01:10:16 +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
await interaction.deferReply();
await interaction.deleteReply();
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;
} catch (error) {
console.error(error);
// Reply failed to acknowledge command
await interaction.reply({
content: 'Failed to fetch message!',
ephemeral: true,
});
}
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');
2024-02-05 01:58:08 +00:00
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,
});
}
}
2024-01-29 01:10:16 +00:00
export const data = new SlashCommandBuilder()
.setName('self_roles')
.setDescription('Manages reactions for self roles.')
.addSubcommand(subcommand =>
subcommand
.setName('create')
.setDescription('Creates new message in channel.')
.addStringOption(option =>
option
.setName('text')
.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) {
const { options } = interaction;
2024-01-29 01:10:16 +00:00
let createNew = false, id;
switch (options.getSubcommand()) {
2024-01-29 01:10:16 +00:00
case 'create':
id = await createSelfRoles(interaction);
2024-01-29 01:35:29 +00:00
// Flag to create new database entry
createNew = true;
break;
case 'register':
const { success, msgID } = await registerSelfRoles(interaction);
id = msgID ?? id;
// Flag to create new database entry
createNew = success;
2024-01-29 01:10:16 +00:00
break;
case 'add':
await addSelfRoles(interaction);
2024-01-29 01:10:16 +00:00
break;
}
if (createNew) {
try {
// Create database entry
await Message.create({ id });
} catch (error) {
console.error(error);
// Reply failed to acknowledge command
await interaction.followUp({
content: 'Failed to save data from message!',
ephemeral: true,
});
}
console.info(`[INFO] New self roles on message with ID: '${id}'.`);
}
2024-01-29 01:10:16 +00:00
}