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

问题描述 投票:0回答:2

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

        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

c# discord
2个回答
0
投票

是的,您可以在类别中创建频道。该函数的

CreateTextChannelAsync
(包括 CreateAudioChannelAsync)第二个参数允许您设置 CategoryId。

您需要获取类别的 ulong id,您可以从 Guild.CategoryChannels 获取该类别。然后,您可以将该 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);
    });
}

0
投票

获取频道列表,然后检查是否标记为类别并检查项目名称。然后在创建新通道时作为父通道传递。使用 dSharp https://dsharpplus.github.io/DSharpPlus/index.html

foreach (var item in ctx.Guild.OrderedChannels){
if (item.IsCategory == true)
{
    catList = catList + " " + item.Name + "\n";
    if (item.Name.Contains("games"))
    {
        DiscordChannel CatToUse = item;
        var channel = await ctx.Guild.CreateTextChannelAsync(member.Username, CatToUse);
        await channel.SendMessageAsync($"Welcome to the world of Axitst Channel list \n {channelList} \n Cat List \n {catList}");
    }
}
else
{
    channelList = channelList + " " + item.Name + "\n";

}
© www.soinside.com 2019 - 2024. All rights reserved.