Discord Bot,在现有渠道类别中创建新的公共渠道

时间:2020-02-14 04:36:53

标签: c# discord

我正在尝试运行一个删除通道的命令,然后在相同的通道类别中而不是在行会(而不是任何类别)中重新创建它。

        public Task ExecuteCommand(BotMessageInformation botMessageInfo)
        {
            return Task.Run(
            async
            ()
            =>
            {
                ulong messageChannelId = botMessageInfo.Message.Channel.Id;
                var textChannel = botMessageInfo.Client
                                  .GetChannel(messageChannelId) as SocketTextChannel;
                string textChannelName = textChannel.Name;
                // ulong textChannelCategory = textChannel.CategoryId;
                await textChannel.DeleteAsync();
                await botMessageInfo.Client.GetGuild(botMessageInfo.GuildId)
                                           .CreateTextChannelAsync(textChannelName);
            });
        }

botMessageInfo.Client的类型为DiscordSocketClient

如何在给定类别中创建此频道?我想使用textChannel.CategoryId

1 个答案:

答案 0 :(得分:1)

是的,您可以在类别中创建频道。函数的CreateTextChannelAsync(包括CreateAudioChannelAsync)的第二个参数使您可以设置CategoryId。

您将需要从Guild.CategoryChannels获取类别的ulong ID。然后,您可以将该ID传递到第二个参数中,以为该通道分配该类别。

// in using statements area
using System.Linq;

public Task ExecuteCommand(BotMessageInformation botMessageInfo)
{
    return Task.Run(
    async
    ()
    =>
    {
        ulong messageChannelId = botMessageInfo.Message.Channel.Id;
        var textChannel = botMessageInfo.Client
                          .GetChannel(messageChannelId) as SocketTextChannel;
        string textChannelName = textChannel.Name;
                // ulong textChannelCategory = textChannel.CategoryId;
                await textChannel.DeleteAsync();

        var guild = botMessageInfo.Client.GetGuild(botMessageInfo.GuildId);
        var categoryId = guild.CategoryChannels.First(c => c.Name == "Name of Category").Id;

        await guild.CreateTextChannelAsync(textChannelName, tcp => tcp.CategoryId = categoryId);
    });
}