【问题标题】:How to send message at specific time on Discord using Discord JDA in Java如何使用 Java 中的 Discord JDA 在 Discord 上的特定时间发送消息
【发布时间】:2021-05-05 02:57:30
【问题描述】:

我正在尝试让我的不和谐机器人在特定时间发送消息。我在 Discord JDA 的 ListenerAdapter 中使用 onGuildAvailable(GuildAvailableEvent event) 方法。我也尝试过 onGuildReady(GuildReadyEvent 事件),但这似乎也不起作用。任何帮助表示赞赏。到目前为止,这是我的代码:

private static GuildAvailableEvent e;
private static final Timer timer = new Timer(1000, new Listener());

public void onGuildAvailable(GuildAvailableEvent event) {
    e = event;
    timer.setRepeats(true);
    timer.start();
    timer.restart();
}

private static class Listener implements ActionListener {
    public void actionPerformed(ActionEvent event) {
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("e HH:mm");
        String time = dtf.format(LocalDateTime.now());
        if(time.charAt(0) == '0' || time.charAt(0) == '2' || time.charAt(0) == '3' || time.charAt(0) == '4' || time.charAt(0) == '5') {
            String message = "Class is starting! Get to class!";
            if(time.substring(2, time.length() - 1).equalsIgnoreCase("08:05")) {
                Objects.requireNonNull(e.getGuild().getDefaultChannel()).sendMessage(message).queue();
            } else if(time.substring(2, time.length() - 1).equalsIgnoreCase("09:25")) {
                Objects.requireNonNull(e.getGuild().getDefaultChannel()).sendMessage(message).queue();
            } else if(time.substring(2, time.length() - 1).equalsIgnoreCase("11:55")) {
                Objects.requireNonNull(e.getGuild().getDefaultChannel()).sendMessage(message).queue();
            } else if(time.substring(2, time.length() - 1).equalsIgnoreCase("13:30")) {
                Objects.requireNonNull(e.getGuild().getDefaultChannel()).sendMessage(message).queue();
            } else if(time.substring(2, time.length() - 1).equalsIgnoreCase("15:39")) { // test time
                Objects.requireNonNull(e.getGuild().getDefaultChannel()).sendMessage(message).queue();
            }
        }
    }
}

【问题讨论】:

    标签: java discord-jda


    【解决方案1】:

    您可以使用ReadyEvent,但我建议使用ScheduledExecutorService 发送这些消息。

    首先,您必须将当前时间与您想要安排消息的时间进行比较。

    public void onReady(@NotNull ReadyEvent event) {
    
        // get the current ZonedDateTime of your TimeZone
        ZonedDateTime now = ZonedDateTime.now(ZoneId.of("Europe/Berlin"));
    
        // set the ZonedDateTime of the first lesson at 8:05
        ZonedDateTime nextFirstLesson = now.withHour(8).withMinute(05).withSecond(0);
    
        // if it's already past the time (in this case 8:05) the first lesson will be scheduled for the next day
        if (now.compareTo(nextFirstLesson) > 0) {
            nextFirstLesson = nextFirstLesson.plusDays(1);
        }
    
        // duration between now and the beginning of the next first lesson
        Duration durationUntilFirstLesson = Duration.between(now, nextFirstLesson);
        // in seconds
        long initialDelayFirstLesson = durationUntilFirstLesson.getSeconds();
    
        // schedules the reminder at a fixed rate of one day
        ScheduledExecutorService schedulerFirstLesson = Executors.newScheduledThreadPool(1);
        schedulerFirstLesson.scheduleAtFixedRate(() -> {
            // send a message
            /*
            String message = "Class is starting! Get to class!";
            JDA jda = event.getJDA();
            for (Guild guild : jda.getGuilds()) {
                guild.getDefaultChannel().sendMessage(message).queue();
            }
            */
    
                    },
                initialDelayFirstLesson,
                TimeUnit.DAYS.toSeconds(1),
                TimeUnit.SECONDS);
    }
    

    这只是第一课的一个基本概念。其余部分由您决定。

    例如,您可能想检查现在是哪一天以便不在周末发送消息,或者只为所有课程使用一个调度程序。

    我不知道您是只想将这些消息发送到一个特定的服务器(在这种情况下,您可能只想硬编码公会 ID)还是发送到多个服务器(在这里您可以初始化公会列表或只是获取机器人所在的每个公会)。

    【讨论】:

      猜你喜欢
      • 2019-03-01
      • 2021-06-17
      • 2019-06-26
      • 2021-07-06
      • 2018-01-06
      • 2020-07-06
      • 2017-10-28
      • 2021-09-30
      • 2021-09-10
      相关资源
      最近更新 更多