I wanted to create a script that when a person connects to specific voice-channel it creates a room which only that person can join and others can't.
The script's logic is simple; it creates permissions for the person who connected to the channel (speak, voice activity, etc.) and denies CONNECT
permission from the @everyone
role.
The problem is that my bot gives permissions to the person who connected to the voice channel but does not change any permission of the @everyone
role. How can I solve that?
const { VoiceState } = require('discord.js');
const SettingsJSON = require('../../Configuration/Settings.json');
const Settings = SettingsJSON.PrivHub;
module.exports = async (oldState, newState) => {
let mainChannel = oldState.guild.channels.cache.get(Settings.Room);
if (!mainChannel) return;
if (
!oldState.channelID &&
newState.channelID &&
newState.channel.parentID == mainChannel.parentID &&
newState.channelID == mainChannel.id
) {
newState.guild.channels
.create(
`${Settings.Symbol} ${newState.member.displayName} kişisinin odası`,
{
type: 'voice',
parent: mainChannel.parentID,
permissionOverwrites: [
mainChannel.permissionOverwrites.clone().set(
newState.member.id,
{
id: newState.member.id,
allow: [
'MANAGE_CHANNELS',
'STREAM',
'VIEW_CHANNEL',
'CONNECT',
'SPEAK',
'USE_VAD',
],
},
{
id: oldState.guild.roles.everyone.id,
deny: ["CONNECT"],
},
),
],
},
)
.then((channel) => {
if (newState.member && newState.member.voice.channelID)
newState.member.voice.setChannel(channel);
});
return;
} else if (oldState.channelID && newState.channelID) {
let oldChannel = oldState.channel;
if (
oldChannel.position > mainChannel.position &&
oldChannel.parentID == mainChannel.parentID &&
oldChannel.members.size <= 0 &&
!oldChannel.deleted
)
oldChannel.delete().catch(undefined);
if (
newState.channelID == mainChannel.id &&
newState.channel.parentID == mainChannel.parentID
) {
newState.guild.channels
.create(
`${Settings.Symbol} ${newState.member.displayName} kişisinin odası`,
{
type: 'voice',
parent: mainChannel.parentID,
permissionOverwrites: [
mainChannel.permissionOverwrites.clone().set(
newState.member.id,
{
id: newState.member.id,
allow: [
'MANAGE_CHANNELS',
'STREAM',
'VIEW_CHANNEL',
'CONNECT',
'SPEAK',
'USE_VAD',
],
},
{
id: oldState.guild.roles.everyone.id,
deny: ["CONNECT"],
},
),
],
},
)
.then((channel) => {
if (newState.member && newState.member.voice.channelID)
newState.member.voice.setChannel(channel);
});
}
return;
} else if (
oldState.channelID &&
oldState.channel.parentID == mainChannel.parentID &&
!newState.channelID
) {
let oldChannel = oldState.channel;
if (
oldChannel.position > mainChannel.position &&
oldChannel.members.size <= 0 &&
!oldChannel.deleted
)
oldChannel.delete().catch(undefined);
}
};
module.exports.config = {
Event: 'voiceStateUpdate',
};