DiscordJS-Template/commands/admin/self_roles.js

94 lines
2.7 KiB
JavaScript
Raw Normal View History

2024-01-29 01:10:16 +00:00
import { SlashCommandBuilder } from 'discord.js';
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
.setRequired(true)
.setName('text')
.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 channel = interaction.channel;
2024-01-29 01:35:29 +00:00
let createNew = false;
let id = interaction.options.getString('id');
2024-01-29 01:10:16 +00:00
switch (interaction.options.getSubcommand()) {
case 'create':
// Create message with text input
const text = interaction.options.getString('text');
2024-01-29 01:35:29 +00:00
id = (await channel.send(text)).id;
2024-01-29 01:10:16 +00:00
// Reply and delete to acknowledge command
interaction.deferReply();
interaction.deleteReply();
2024-01-29 01:35:29 +00:00
// Flag to create new database entry
createNew = true;
break;
case 'register':
try {
// Get message by id
await channel.messages.fetch(id);
// Reply successfully to acknowledge command
await interaction.reply({
content: 'Successfully fetched message!',
ephemeral: true,
});
// Flag to create new database entry
createNew = true;
} catch (_) {
// Reply failed to acknowledge command
await interaction.reply({
content: 'Failed to fetch message!',
ephemeral: true,
});
}
2024-01-29 01:10:16 +00:00
break;
case 'add':
2024-01-29 01:35:29 +00:00
const role = interaction.options.getRole('role');
const emoji = interaction.options.getString('emoji');
// Reply successfully to acknowledge command
2024-01-29 01:10:16 +00:00
await interaction.reply({
2024-01-29 01:35:29 +00:00
content: 'Added new entry for self roles!',
2024-01-29 01:10:16 +00:00
ephemeral: true,
});
2024-01-29 01:35:29 +00:00
console.debug(`[DEBUG] Added new entry to get role with ID '${role.id}' using '${emoji}'.`);
2024-01-29 01:10:16 +00:00
break;
}
2024-01-29 01:35:29 +00:00
if (createNew)
console.debug(`[DEBUG] New self roles on message with ID: '${id}'.`);
2024-01-29 01:10:16 +00:00
}