[英]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.