0
votes

I'm currently creating a discord bot. I know and can create both a Role and Voice channel however I'm trying to make the bot create the channel as private so only people from the role I just created (and admins) can connect to that server meaning it is a secure chat for the role. If anyone could give me a hand on how to do this is would be greatly appreciated! Here is the code I have so far:

public class GuildService
{
    private Color[] colors = new Color[] {Color.Blue, Color.DarkBlue, Color.DarkerGrey, Color.DarkGreen, Color.DarkGrey, Color.DarkMagenta, Color.DarkOrange, Color.DarkPurple, Color.DarkRed, Color.DarkTeal, Color.Default, Color.Gold, Color.Green, Color.LighterGrey, Color.LightGrey,
            Color.LightOrange, Color.Magenta, Color.Orange, Color.Purple, Color.Red, Color.Teal};
    public async Task SetupGroup(SocketGuildUser user, string Ext)
    {
        Random ran = new Random();
        ulong origin = (ulong)GuildPermission.Speak + (ulong)GuildPermission.SendTTSMessages + (ulong)GuildPermission.SendMessages + (ulong)GuildPermission.ReadMessages + (ulong)GuildPermission.EmbedLinks + (ulong)GuildPermission.Connect + (ulong)GuildPermission.AttachFiles + (ulong)GuildPermission.AddReactions;
        GuildPermissions perms = new GuildPermissions(origin);
        var guild = await user.Guild.CreateRoleAsync(Ext, perms, colors[ran.Next(colors.Length)], true);

        var vChan = await user.Guild.CreateVoiceChannelAsync(Ext);
    }
}

Cheers again in advance!

1

1 Answers

1
votes

In order to make a private channel based on roles, just like making private channels through discord itself you will need to give the channel explicit permissions for the role you want it to have access, and deny access for the other roles in the server.

You can apply permissions for a channel to a role AFTER the channel has been created.

You have already done the first half of this which is creating the channel and storing it in the variable vChan. You will then need to call the method vChan.AddPermissionOverwriteAsync(). This will take 2 parameters:

  1. IRole - the role you want these new permissions on the channel applied to. You will need to retrieve the role via user.Roles
  2. OvewritePermission - This is a struct provided in the using Discord; namespace that contains a whole bunch of properties for the permissions on different actions. You can see more details here. In my own code, I have a function that returns Admin permissions which looks like:
public static OverwritePermissions GetFullAdminPermissions()
        {
            return new OverwritePermissions(PermValue.Deny,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow, // This parameter is for the 'viewChannel' permission
                                            PermValue.Allow,
                                            PermValue.Deny,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Allow,
                                            PermValue.Deny);
        }

For roles that you want this channel to allow, set Allow for that viewChannel parameter. For roles that you don't want this channel to allow, set Deny for that viewChannel parameter.