Compare commits

...

4 Commits

Author SHA1 Message Date
d0ec7fbb6c basic custom vc slash command 2024-02-07 19:30:14 +01:00
b5079b6f40 basic error handling and comments 2024-02-07 17:40:29 +01:00
1a3e7ac96b basic chanel creation 2024-02-07 17:36:00 +01:00
a0eb75e4ee basic subcommand handling 2024-02-07 17:35:30 +01:00
2 changed files with 74 additions and 12 deletions

View File

@ -1,4 +1,5 @@
import { SlashCommandBuilder } from 'discord.js'; import { SlashCommandBuilder } from 'discord.js';
import { VoiceChannel } from '../../../database.js';
export const data = new SlashCommandBuilder() export const data = new SlashCommandBuilder()
.setName('custom_vc') .setName('custom_vc')
@ -27,6 +28,47 @@ export const data = new SlashCommandBuilder()
) )
); );
export async function execute(interaction) { export async function execute(interaction) {
const { options } = interaction; const { guild, options } = interaction;
// const id = options.getString('id');
let step;
try {
switch (options.getSubcommand()) {
case 'create': {
// Get channel name from user input
const name = options.getString('name');
step = 'create';
// Create new channel
const channel = await guild.create({
name, type: ChannelType.GuildVoice
});
// Save channel data
step = 'save';
await VoiceChannel.create({ id: channel.id });
break;
}
case 'register': {
// Get channel id from user input
const id = options.getString('id');
step = 'fetch';
// Try fetching channel by id
await guild.channels.fetch(id);
// Save channel data
step = 'save';
await VoiceChannel.create({ id });
break;
}
}
} catch (error) {
console.error(error);
// Reply failed to acknowledge command
await interaction.reply({
content: `Failed to ${step} message!`,
ephemeral: true
});
}
} }

View File

@ -1,16 +1,36 @@
import { Events } from 'discord.js'; import { ChannelType, Events, PermissionsBitField } from 'discord.js';
export const name = Events.VoiceStateUpdate; export const name = Events.VoiceStateUpdate;
export async function execute(oldState, newState) { export async function execute(_, state) {
console.debug('[DEBUG] Voice State Update'); if (!state.channel) return;
const change = !!oldState.channel ^ !!newState.channel; // Extract user data
if (!change) return; const member = state.member;
const name = member.user.username;
const guild = newState.guild.name; // Extract channel data
const member = newState.member.user.username; const channels = state.guild.channels;
const state = newState.channel ? 'joined' : 'left'; try {
const channel = (oldState.channel ?? newState.channel).name; // Create private channel with all permissions
const chName = `${name}${name.endsWith('s') ? "'" : "'s"} channel`;
const privCh = await channels.create({
name: chName,
type: ChannelType.GuildVoice,
permissionOverwrites: [
{
id: member.id,
allow: [
PermissionsBitField.All
]
}
]
});
console.debug(`[DEBUG] User '${member}' ${state} channel '${channel}' in guild '${guild}'.`); // Move user to private channel
await state.setChannel(privCh);
console.info(`[INFO] User '${name}' created private channel with ID ${privCh.id}.`);
} catch (error) {
console.error(error);
await member.send('Could no create or move to channel! Please contact server staff.');
}
} }