创建频道后如何获取频道id

How to get the id of the channel after creating it

我正在创建一个制作临时频道的机器人,但我遇到了一个问题,如何在直接创建频道并将其分配给变量后获取频道 ID?

这是我的代码:

public void onGuildVoiceJoin(GuildVoiceJoinEvent event) {

            if(event.getChannelJoined().getIdLong() == 703255821508673546L) {
            event.getGuild().getCategoryById("704418165710651492").createVoiceChannel("%s".format("channel" + event.getMember().getEffectiveName())).addPermissionOverride(event.getMember().getGuild().getPublicRole(), null, EnumSet.of(Permission.VOICE_CONNECT, Permission.VIEW_CHANNEL)).addMemberPermissionOverride(event.getMember().getIdLong(), EnumSet.of(Permission.MANAGE_CHANNEL, Permission.VOICE_CONNECT, Permission.MANAGE_PERMISSIONS, Permission.VOICE_MOVE_OTHERS), null).queue();
            event.getGuild().getCategoryById("704418165710651492").createTextChannel("%s".format("channel" + event.getMember().getEffectiveName())).queue();
        }
    }

由于 queue() 是异步操作,您必须使用回调

category.createVoiceChannel(name)
        .addPermissionOverride(role, allowRole, denyRole)
        .addPermissionOverride(member, allowMember, denyMember)
        .queue(channel -> {
            System.out.println("ID for voice channel: " + channel.getId());
        });

参见javadoc and wiki

之后您可以使用这些分类方法浏览已创建频道的列表:

    /**
 * All {@link net.dv8tion.jda.api.entities.TextChannel TextChannels}
 * listed for this Category
 *
 * @return Immutable list of all child TextChannels
 */
@Nonnull
List<TextChannel> getTextChannels();

/**
 * All {@link net.dv8tion.jda.api.entities.VoiceChannel VoiceChannels}
 * listed for this Category
 *
 * @return Immutable list of all child VoiceChannels
 */
@Nonnull
List<VoiceChannel> getVoiceChannels();

https://github.com/DV8FromTheWorld/JDA/blob/master/src/main/java/net/dv8tion/jda/api/entities/Category.java

尽管如前所述,您不能保证这些通道已经创建,因为队列是一种异步方法。