DiscordJS-Example/commands/admin/custom_vc/slash.js

86 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-02-07 19:04:16 +00:00
import { ChannelType, SlashCommandBuilder } from 'discord.js';
2024-02-07 18:30:14 +00:00
import { VoiceChannel } from '../../../database.js';
2024-02-06 20:26:29 +00:00
export const data = new SlashCommandBuilder()
.setName('custom_vc')
.setDMPermission(false)
.setDescription('Manages reactions for self roles.')
.addSubcommand((subcommand) =>
subcommand
.setName('create')
.setDescription('Creates new voice channel.')
.addStringOption((option) =>
option
.setName('name')
.setRequired(true)
.setDescription('The name to use for the voice channel.')
)
)
.addSubcommand((subcommand) =>
subcommand
.setName('register')
.setDescription('Registers an existing voice channel.')
2024-02-08 17:24:47 +00:00
.addChannelOption((option) =>
2024-02-06 20:26:29 +00:00
option
.setRequired(true)
2024-02-08 17:24:47 +00:00
.setName('channel')
.setDescription('The voice channel to be used.')
2024-02-06 20:26:29 +00:00
)
);
export async function execute(interaction) {
2024-02-07 18:30:14 +00:00
const { guild, options } = interaction;
2024-02-07 16:35:30 +00:00
2024-02-07 18:30:14 +00:00
let step;
try {
switch (options.getSubcommand()) {
case 'create': {
// Get channel name from user input
const name = options.getString('name');
step = 'create';
// Create new channel
2024-02-07 19:04:16 +00:00
const channel = await guild.channels.create({
2024-02-07 18:30:14 +00:00
name, type: ChannelType.GuildVoice
});
// Save channel data
step = 'save';
2024-02-07 19:04:16 +00:00
await VoiceChannel.create({
id: channel.id,
create: true
});
2024-02-07 18:34:18 +00:00
// Reply success to acknowledge command
await interaction.reply({
content: `Successfully created channel!`,
ephemeral: true
});
2024-02-07 18:30:14 +00:00
break;
}
case 'register': {
// Get channel id from user input
2024-02-08 17:24:47 +00:00
const id = options.getChannel('channel');
2024-02-07 18:30:14 +00:00
// Save channel data
step = 'save';
2024-02-07 19:04:16 +00:00
await VoiceChannel.create({ id, create: true });
2024-02-07 18:34:18 +00:00
// Reply success to acknowledge command
await interaction.reply({
content: `Successfully registered channel!`,
ephemeral: true
});
2024-02-07 18:30:14 +00:00
break;
}
}
} catch (error) {
console.error(error);
// Reply failed to acknowledge command
await interaction.reply({
2024-02-07 22:20:10 +00:00
content: `Failed to ${step} channel!`,
2024-02-07 18:30:14 +00:00
ephemeral: true
});
2024-02-07 16:35:30 +00:00
}
2024-02-06 20:26:29 +00:00
}