【问题标题】:Discord Bot, Create New Public Channel in Existing Channel CategoryDiscord Bot,在现有频道类别中创建新的公共频道
【发布时间】:2020-04-07 14:15:42
【问题描述】:

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

        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

【问题讨论】:

  • 请注意您正在使用的 API 包装器,因为完成您想要的操作的方式取决于库的实现。
  • @Anu6is 我不明白你刚才说什么。
  • DSharpPlusDiscord.Net、其他一些 Discord API 包装器?

标签: c# discord


【解决方案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);
    });
}

【讨论】:

    猜你喜欢
    • 2019-05-05
    • 1970-01-01
    • 2018-07-27
    • 2021-02-06
    • 2017-12-25
    • 2019-12-11
    • 1970-01-01
    • 2021-01-12
    • 1970-01-01
    相关资源
    最近更新 更多