【问题标题】:JDA retrieve messages on a specific dateJDA 在特定日期检索消息
【发布时间】:2021-12-29 12:36:32
【问题描述】:

所以实际上我想订购 2 个参数,例如 !stat 11/18/21

此命令将允许不和谐机器人计算在该特定日期发布在频道中的消息数量。有点像 CTRL + F "期间: ... ... ...

问题是,经过大量研究,我没有发现任何具体的东西。

所以如果有人能帮助我,给我一些伪代码作为例子,或者给我一个地方来学习如何在 JDA 上处理时间

我希望我足够清楚:( (因为我是法国人,英语完全不好,我不得不使用谷歌繁体)

【问题讨论】:

    标签: java discord bots


    【解决方案1】:

    这是我尝试使用java.timechannel.getIterableHistory() 来有效地迭代和计算消息:

    public CompletableFuture<Integer> getCountDuring(String dateString, MessageChannel channel) {
      // Parse the date from the input
      DateTimeFormatter dateFormat = DateTimeFormatter.ofPattern("MM/dd/yy");
      LocalDateTime date = LocalDate.parse(dateString, dateFormat).atStartOfDay();
      // Get the boundary of the time as instant
      Instant startOfDayDT = Instant.ofEpochSecond(date.toEpochSecond(ZoneOffset.UTC));
      Instant endOfDayDT = Instant.ofEpochSecond(date.plusDays(1).toEpochSecond(ZoneOffset.UTC));
      // Create pseudo-snowflake for the message to start from
      long endOfDay = TimeUtil.getDiscordTimestamp(endOfDayDT.toEpochMilli());
    
      // Use atomic counter
      AtomicInteger count = new AtomicInteger(0);
    
      CompletableFuture<Integer> future = new CompletableFuture<>();
      channel.getIterableHistory()
        .skipTo(endOfDay) // start iterating backwards from the end of the day
        .forEachAsync(message -> {
          // Check message creation time is in range of day
          Instant time = message.getTimeCreated().toInstant();
          if (time.isBefore(endOfDayDT) && time.isAfter(startOfDayDT)) {
            count.incrementAndGet();
          }
          // Stop iterating when this is false
          return time.isAfter(startOfDayDT);
        })
        .thenRun(() -> future.complete(count.get())) // yield the counter at the end
        .exceptionally(error -> { // catch errors
          future.completeExceptionally(error);
          return null;
        });
      return future;
    }
    
    // Example usage
    getCountDuring("11/18/21", channel).thenAccept(count -> {
      System.out.println("Counted " + count + " messages on 11/18/21"); 
    });
    

    我还没有真正测试过这是否有效,如果你有任何问题,我可以看看。

    【讨论】:

      【解决方案2】:

      呃,我认为这会更容易我承认我不是初学者但也不是专家,我并不真正理解你发给我的代码。

      我想我必须学习如何在 java 上更好地处理时间,然后我会再次阅读您的答案。我可能会更舒服。

      【讨论】:

        猜你喜欢
        • 2020-07-02
        • 1970-01-01
        • 1970-01-01
        • 2019-04-28
        • 2021-09-20
        • 1970-01-01
        • 1970-01-01
        • 2012-10-21
        • 1970-01-01
        相关资源
        最近更新 更多