如何打印出这个 RestAction / List

Posted

技术标签:

【中文标题】如何打印出这个 RestAction / List【英文标题】:How to print out this RestAction / List 【发布时间】:2021-02-16 17:22:52 【问题描述】:

我正在为我的服务器创建 Discord-Bot。通常我可以很容易地打印出 Lists,但是这个 RestActionList 会烧我的大脑:D。

谁能告诉我如何打印一个 RestAction?

public class ShowBannsCommand implements ServerCommand 
    @Override
    public void performCommand(Member m, TextChannel channel, Message message, MessageReceivedEvent event, EventWaiter waiter) 
        Guild guild = event.getGuild();

        if(m.hasPermission(Permission.BAN_MEMBERS)) 
            message.delete().queue();

            RestAction<List<Guild.Ban>> restAction = guild.retrieveBanList();

            EmbedBuilder builder = EmbedHelper.getDefault("Bannlist of " + guild.getName(), m.getAsMention() + ", here is the bannlist you requested.")
                    .setAuthor("Request by " + m.getUser().getName(), EmbedHelper.link, m.getUser().getEffectiveAvatarUrl())
                    .addField("Server:", guild.getName(), true)
                    .addField("Total banns:", "", true)
                    .addField("Banns:", "", false);
            channel.sendMessage(builder.build()).queue();
        
    

我使用的是 JDA 4.2.0_214

【问题讨论】:

【参考方案1】:

使用队列回调:

guild.retrievBanList().queue(list -> 
    // Note this list should have a hardcoded limit to prevent hitting the character limit
    String banList = list.stream().map(User::getName).collect(Collectors.joining(", "));
    EmbedBuilder builder = EmbedHelper.getDefault("Bannlist of " + guild.getName(), m.getAsMention() + ", here is the bannlist you requested.")
                    .setAuthor("Request by " + m.getUser().getName(), EmbedHelper.link, m.getUser().getEffectiveAvatarUrl())
                    .addField("Server:", guild.getName(), true)
                    .addField("Total banns:", String.valueOf(list.size()), true)
                    .addField("Bans:", banList, false);
    channel.sendMessage(builder.build()).queue();
);

【讨论】:

以上是关于如何打印出这个 RestAction / List的主要内容,如果未能解决你的问题,请参考以下文章

Java 如何打印出List中得值

如何在Java中打印出List的所有元素?

给定一段Java 代码如下:要打印出list中存储的内容,以下语句正确的是()。(选择二项)

python基础--list转换range()的打印结果

java中如何打印下面代码list里面的值?解决了加分!

为什么我的动态数组打印出垃圾值?