[英]How to send message at specific time on Discord using Discord JDA in Java
我试图让我的 discord 机器人在特定时间发送消息。 我正在使用 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();
}
}
}
}
您可以使用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);
}
这只是第一课的一个基本概念。 实施 rest 取决于您。
例如,您可能想要检查是哪一天以便不在周末发送消息,或者只为所有课程使用一个调度程序。
我不知道您是只想将这些消息发送到一个特定的服务器(在这种情况下,您可能只想硬编码公会 ID)还是发送到多个服务器(在这里您可以初始化公会列表或只是让每个公会机器人在)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.