Discord.net bot,如何创建只有管理员角色才能访问的频道?

问题描述

我想用我的机器人创建一个只有管理员角色才能访问的私人频道。 我已阅读this article,了解如何让您的机器人创建特定角色可以查看/加入的私人频道。

现在 discord 有一个功能,您可以完全跳过角色步骤,让所有管理员角色查看它。

discord.net C# 中有没有办法做到这一点?

这是我创建频道的代码

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.");
}

请记住,这不是重复的,因为另一个问题提到添加可以查看频道的角色,而不是像在不和谐中手动创建频道时那样跳过它。

日志指向文本通道,discord.net NuGet 包版本 2.1.1,使用最新的 Visual Studio 2019 编译调试 32 位。

解决方法

为我工作:

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

事实上,具有管理权限的用户可以访问服务器上的任何频道,我们可以在创建后使用 DenyAll 简单地覆盖频道权限。