implement working version of custom channel permissions

This commit is contained in:
Baipyrus 2024-03-23 22:43:21 +01:00
parent c7aad7190e
commit b8adefd093

View File

@ -5,7 +5,7 @@ import {
GuildChannelManager, GuildChannelManager,
GuildChannel, GuildChannel,
VoiceState, VoiceState,
PermissionOverwrites PermissionsBitField
} from 'discord.js'; } from 'discord.js';
import { VoiceChannel } from '../../database.js'; import { VoiceChannel } from '../../database.js';
@ -28,20 +28,10 @@ const getChannel = async (member, guildChs, channel) => {
// Create private channel with all permissions // Create private channel with all permissions
const name = member.user.username; const name = member.user.username;
const chName = `${name}${name.toLowerCase().endsWith('s') ? "'" : "'s"} channel`; const chName = `${name}${name.toLowerCase().endsWith('s') ? "'" : "'s"} channel`;
// Get permissions from parent
/** @type {PermissionOverwrites} */
const { allow, deny } = channel.parent.permissionOverwrites.cache.get(member.client.user.id);
const privCh = await guildChs.create({ const privCh = await guildChs.create({
name: chName, name: chName,
parent: channel.parent, parent: channel.parent,
type: ChannelType.GuildVoice, type: ChannelType.GuildVoice
permissionOverwrites: [
{
id: member.id,
allow,
deny
}
]
}); });
// Save newly created channel // Save newly created channel
@ -107,6 +97,19 @@ export async function execute(oldState, newState) {
const channels = newState.guild.channels; const channels = newState.guild.channels;
const privCh = await getChannel(member, channels, channel); const privCh = await getChannel(member, channels, channel);
step = 'edit permissions for';
// Edit permissionOverwrites on channel for user
await privCh.permissionOverwrites.set([
{
id: member.id,
allow: [
PermissionsBitField.Flags.ViewChannel,
PermissionsBitField.Flags.ManageChannels,
PermissionsBitField.Flags.ManageRoles
]
}
]);
step = 'move to'; step = 'move to';
// Move user to private channel // Move user to private channel
await newState.setChannel(privCh); await newState.setChannel(privCh);