我正在尝试让我的不和谐机器人在特定时间发送消息。我正在使用 Discord JDA 的 ListenerAdapter 中的 onGuildAvailable(GuildAvailableEvent event) 方法。我也试过 onGuildReady(GuildReadyEvent event),但这似乎也不起作用。任何帮助表示赞赏。到目前为止,这是我的代码:
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();
}
}
}
}
答案 0 :(得分:0)
您可以使用 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)还是多个服务器(在这里你可以初始化一个公会列表或只是获取机器人所在的每个公会)。