SimpleCoder
SimpleCoder

Reputation: 501

Discord.net bot, how to create a channel that only administrator roles have access to?

I want to create a private channel with my bot that only administrator roles have access to. I have read through this article on how to let your bot create a private channel specific roles can view/join.

As of now discord has a feature where you can skip the roles step completely and just let all administrator roles view it.

Is there a way to do this in discord.net C#?

Here is my code of creating the channel:

var channel = Context.Guild.Channels.SingleOrDefault(x => x.Name == "log");

if (channel == null) // there is no channel with the name of 'log'
{
     // create the channel
     var newChannel = await Context.Guild.CreateTextChannelAsync("log");

     // If you need the newly created channels id
     var newChannelId = newChannel.Id;
     await ReplyAsync("Created channel ``'log'``");
}
else // reply that the channel already exists
{
     await ReplyAsync("Unable to create channel ``log`` channel already exists.");
}

Keep in mind this is not a duplicate as the other question mentions adding roles which can view the channel and not just skipping it like it is possible when manually creating a channel in discord.

log reffers to a text channel, Discord.net NuGet package version 2.1.1, Compiling for debug 32-bit with Visual Studio 2019 latest as of now.

Upvotes: 0

Views: 2074

Answers (1)

Auditive
Auditive

Reputation: 2049

Worked for me with:

SocketGuild guild = Bot.GetGuild(123456789012345678);
RestTextChannel newChannel = await guild.CreateTextChannelAsync("test-log");
await newChannel.AddPermissionOverwriteAsync(guild.EveryoneRole, OverwritePermissions.DenyAll(newChannel));

In fact that users with Administrative privilege can access any channel on the server, we can simply overwrite channel permissions after creation with DenyAll.

Upvotes: 2

Related Questions