The-WebGuy
The-WebGuy

Reputation: 937

Discord.js creating a private voice channel and role

I am trying to make a discord bot with Discord.js, that can create a private channel and a new role and assign that role to the channel. So any users with the new role can access the channel.

The following code, creates a channel from the name i give it through 'eventName' it also creates a role of the same name.

But how do i make the voice channel private, set a role to exclusively access it and set some permissions to the new role?

function addChannel(message,args,eventName){
var server = message.guild;
var permsName = eventName+"-"+message.author.username;
message.guild.createRole({
    //data: {
        name: permsName,
        permissions: []
    //},
    //reason: 'new Event'
}).then(role => {
    message.member.addRole(role,permsName)
    .catch(error => client.catch(error))
}).catch(error => client.catch(error))
server.createChannel(eventName, 'voice').then( // Create the actual voice channel.
    (chan) => {
        chan.setParent("427382662240534535").then( // Move the voice channel to the current message's parent category.
            (chan2) => {
                console.log("stage 3");
                console.log(chan2);
                //console.log(`Set the category of ${chan2.name} to ${chan2.parent.name}`);
                chan2.overwritePermissions(message.guild.roles.find('name', '@everyone'), { 'CREATE_INSTANT_INVITE' : false }); // Give the channel some standard permissions.
                chan2.overwritePermissions(message.guild.roles.find('name', permsName), {
                    'CREATE_INSTANT_INVITE' : false,        'ADD_REACTIONS': true,
                    'READ_MESSAGES': true,                  'SEND_MESSAGES': true,
                    'SEND_TTS_MESSAGES': true,              'MANAGE_MESSAGES': true,
                    'EMBED_LINKS': true,                    'ATTACH_FILES': true,
                    'READ_MESSAGE_HISTORY': true,           'MENTION_EVERYONE': true,
                    'EXTERNAL_EMOJIS': true,                'CONNECT': true,
                    'SPEAK': true
                  });
                  console.log("stage 4");
            }
        ).catch(console.error);
    }
).catch(console.error);
return '```Added```';

}

Upvotes: 1

Views: 16849

Answers (1)

Thotht
Thotht

Reputation: 71

Theres two problems here, for one you are using a lot of unnessecary permissions here, for Voice obviously reactions reading and sending messages etc are not important, important are only VIEW_CHANNEL, SPEAK, CONNECT, and CREATE_INSTANT_INVITE, and you have to explicitely forbid everyone for a standard discord server setup since everyone usually has these rights Serverwide unless locally overwritten.

So what you want for your overwrite permissions is:

chan2.overwritePermissions(message.guild.roles.find('name', '@everyone'), { // Disallow Everyone to see, join, invite, or speak
   'CREATE_INSTANT_INVITE' : false,        'VIEW_CHANNEL': false,
   'CONNECT': false,                       'SPEAK': false
});
chan2.overwritePermissions(message.guild.roles.find('name', permsName),   {//Explicitely allow the role to see, join and speak
    'VIEW_CHANNEL': true,                   'CONNECT': true,            'SPEAK': true,
});

Note that you do not have to explicitely disallow CREATE_INSTANT_INVITE since that is inherited from everyone if not explicitely changed.

Upvotes: 7

Related Questions