From 188eafa2eebe08e9e22e2857f890fbd815c190f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beatrice=20Dellac=C3=A0?= Date: Thu, 25 Aug 2022 22:37:32 +0200 Subject: [PATCH] Add a delayed log method --- .../java/wtf/beatrice/hidekobot/HidekoBot.java | 14 +++++++++----- .../wtf/beatrice/hidekobot/utils/Logger.java | 17 +++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java index eb51cd1..e36a80c 100644 --- a/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java +++ b/src/main/java/wtf/beatrice/hidekobot/HidekoBot.java @@ -31,20 +31,24 @@ public class HidekoBot try { + // try to create the bot object and authenticate it with discord. jdaBuilder = JDABuilder.createDefault(botToken); jdaBuilder.setActivity(Activity.playing("the piano")); jda = jdaBuilder.build(); } catch (LoginException e) { - logger.log(e.getMessage()); - return; + logger.log(e.getMessage()); // print the error message, omit the stack trace. + return; // if we failed connecting and authenticating, then quit. } - + + // find the bot's user id and generate an invite-link. botUserId = jda.getSelfUser().getId(); standardInviteLink = standardInviteLink.replace("%userid%", botUserId); - logger.log("Bot User ID: " + botUserId); - logger.log("Invite Link: " + standardInviteLink); + // log the invite-link to console so noob users can just click on it. + logger.log("Bot User ID: " + botUserId, 10); + logger.log("Invite Link: " + standardInviteLink, 10); + } diff --git a/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java b/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java index 6872806..18db314 100644 --- a/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java +++ b/src/main/java/wtf/beatrice/hidekobot/utils/Logger.java @@ -2,6 +2,9 @@ package wtf.beatrice.hidekobot.utils; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; public class Logger { @@ -28,4 +31,18 @@ public class Logger .replace("%class%", className) .replace("%message%", message)); } + + // log a message to console after delaying it (in seconds). + public void log(String message, int delay) + { + Executors.newSingleThreadScheduledExecutor().schedule(new Runnable() + { + @Override + public void run() + { + log(message); + } + }, delay, TimeUnit.SECONDS); + + } }