如何在 Bot 启动时发送消息
How to Send a Message on Bot Startup
目前我正在制作一个机器人,它会在每个星期四 1:00PM 提醒我。计时器部分已经完成,但我需要它在机器人启动时进行初始化。我正在使用 GuildMessageReceivedEvent 并通过执行命令来激活它。我试过 ReadyEvent 但我无法使用该事件发送消息。感谢任何帮助。
代码:
public class Reminder extends ListenerAdapter {
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
String[] args = e.getMessage().getContentRaw().split("\s+");
if (args[0].equalsIgnoreCase("!start")) {
if (e.getGuild().getId().equalsIgnoreCase("679875946597056683")) {
TextChannel defaultChannel = e.getGuild().getDefaultChannel();
if (defaultChannel != null) {
e.getChannel().sendTyping().queue();
e.getChannel().sendMessage("Reminder has been activated.").queue();
LocalDateTime now = LocalDateTime.now(ZoneId.of("America/Los_Angeles"));
LocalDateTime then = LocalDateTime.now(ZoneId.of("America/Los_Angeles"));
then = then.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY)).withHour(13).withMinute(0).withSecond(0);
Duration duration = Duration.between(now, then);
long initialDelay = duration.getSeconds();
if (initialDelay < 0) {
initialDelay = Duration.between(now, then.with(TemporalAdjusters.next(DayOfWeek.THURSDAY)).withHour(13).withMinute(0)).getSeconds();
}
ScheduledExecutorService scheduledActivity = Executors.newScheduledThreadPool(1);
scheduledActivity.scheduleAtFixedRate(() -> {
List<Message> messages = e.getChannel().getHistory().retrievePast(1).complete();
for (Message msg : messages) {
if (!msg.getContentRaw().equalsIgnoreCase("Reminder")) {
defaultChannel.sendTyping().queue();
defaultChannel.sendMessage("Reminder").queue();
}
}
},
initialDelay,
TimeUnit.DAYS.toSeconds(7),
TimeUnit.SECONDS);
}
}
}
}
}
您可以使用 ReadyEvent
将消息发送到服务器中的特定频道。
例如,在您的 ReadyEvent
:
event.getJDA().getGuildById("Your Server ID").getTextChannelById("Your Discord Channel ID").sendMessage("Bot is ready!").queue();
或者您可以通过以下方式将其发送到您服务器的系统频道:
event.getJDA().getGuildById("Your Server ID").getSystemChannel().sendMessage("Bot is ready!").queue();
目前我正在制作一个机器人,它会在每个星期四 1:00PM 提醒我。计时器部分已经完成,但我需要它在机器人启动时进行初始化。我正在使用 GuildMessageReceivedEvent 并通过执行命令来激活它。我试过 ReadyEvent 但我无法使用该事件发送消息。感谢任何帮助。
代码:
public class Reminder extends ListenerAdapter {
@Override
public void onGuildMessageReceived(GuildMessageReceivedEvent e) {
String[] args = e.getMessage().getContentRaw().split("\s+");
if (args[0].equalsIgnoreCase("!start")) {
if (e.getGuild().getId().equalsIgnoreCase("679875946597056683")) {
TextChannel defaultChannel = e.getGuild().getDefaultChannel();
if (defaultChannel != null) {
e.getChannel().sendTyping().queue();
e.getChannel().sendMessage("Reminder has been activated.").queue();
LocalDateTime now = LocalDateTime.now(ZoneId.of("America/Los_Angeles"));
LocalDateTime then = LocalDateTime.now(ZoneId.of("America/Los_Angeles"));
then = then.with(TemporalAdjusters.nextOrSame(DayOfWeek.THURSDAY)).withHour(13).withMinute(0).withSecond(0);
Duration duration = Duration.between(now, then);
long initialDelay = duration.getSeconds();
if (initialDelay < 0) {
initialDelay = Duration.between(now, then.with(TemporalAdjusters.next(DayOfWeek.THURSDAY)).withHour(13).withMinute(0)).getSeconds();
}
ScheduledExecutorService scheduledActivity = Executors.newScheduledThreadPool(1);
scheduledActivity.scheduleAtFixedRate(() -> {
List<Message> messages = e.getChannel().getHistory().retrievePast(1).complete();
for (Message msg : messages) {
if (!msg.getContentRaw().equalsIgnoreCase("Reminder")) {
defaultChannel.sendTyping().queue();
defaultChannel.sendMessage("Reminder").queue();
}
}
},
initialDelay,
TimeUnit.DAYS.toSeconds(7),
TimeUnit.SECONDS);
}
}
}
}
}
您可以使用 ReadyEvent
将消息发送到服务器中的特定频道。
例如,在您的 ReadyEvent
:
event.getJDA().getGuildById("Your Server ID").getTextChannelById("Your Discord Channel ID").sendMessage("Bot is ready!").queue();
或者您可以通过以下方式将其发送到您服务器的系统频道:
event.getJDA().getGuildById("Your Server ID").getSystemChannel().sendMessage("Bot is ready!").queue();