From e8c09c1a85cf074e064875d33376f701b06a56ab Mon Sep 17 00:00:00 2001 From: Arnold Alejo Nunag Date: Wed, 2 Sep 2020 19:08:22 +0800 Subject: [PATCH] Add command line console --- .../java/sciwhiz12/janitor/BotConsole.java | 81 +++++++++++++++++++ .../java/sciwhiz12/janitor/JanitorBot.java | 20 +++++ src/main/java/sciwhiz12/janitor/Logging.java | 1 + .../janitor/commands/ShutdownCommand.java | 23 +----- 4 files changed, 104 insertions(+), 21 deletions(-) create mode 100644 src/main/java/sciwhiz12/janitor/BotConsole.java diff --git a/src/main/java/sciwhiz12/janitor/BotConsole.java b/src/main/java/sciwhiz12/janitor/BotConsole.java new file mode 100644 index 0000000..58719ca --- /dev/null +++ b/src/main/java/sciwhiz12/janitor/BotConsole.java @@ -0,0 +1,81 @@ +package sciwhiz12.janitor; + +import com.google.common.base.Strings; + +import java.io.InputStream; +import java.util.Scanner; + +import static sciwhiz12.janitor.Logging.CONSOLE; + +public class BotConsole { + private final JanitorBot bot; + private final Thread thread; + private volatile boolean running = true; + + public BotConsole(JanitorBot bot, InputStream input) { + this.bot = bot; + this.thread = new Thread(this.new ConsoleThread(input)); + this.thread.setName("janitor_console"); + this.thread.setDaemon(true); + } + + public Thread getConsoleThread() { + return this.thread; + } + + public void start() { + this.thread.start(); + } + + public void stop() { + running = false; + this.thread.interrupt(); + } + + public void parseCommand(String input) { + String[] parts = input.split(" "); + switch (parts[0]) { + case "shutdown": { + bot.shutdown(); + break; + } + default: + CONSOLE.warn("Unknown command: " + input); + } + } + + public class ConsoleThread implements Runnable { + private final Scanner scanner; + + public ConsoleThread(InputStream consoleInput) { + scanner = new Scanner(consoleInput); + } + + @Override + public void run() { + CONSOLE.info("Console thread is now running"); + outer: + while (BotConsole.this.running) { + while (!scanner.hasNextLine()) { + try { + Thread.sleep(150); + } catch (InterruptedException e) { + CONSOLE.warn("Console thread is interrupted"); + continue outer; + } + } + try { + String input = scanner.nextLine(); + if (Strings.isNullOrEmpty(input)) { + continue; + } + CONSOLE.debug("Received command: {}", input); + BotConsole.this.parseCommand(scanner.nextLine()); + } catch (Exception e) { + CONSOLE.error("Error while running console thread", e); + } + } + CONSOLE.info("Console thread is now closed"); + } + } +} diff --git a/src/main/java/sciwhiz12/janitor/JanitorBot.java b/src/main/java/sciwhiz12/janitor/JanitorBot.java index 14db4fb..36aba6a 100644 --- a/src/main/java/sciwhiz12/janitor/JanitorBot.java +++ b/src/main/java/sciwhiz12/janitor/JanitorBot.java @@ -7,6 +7,7 @@ import net.dv8tion.jda.api.entities.Activity; import sciwhiz12.janitor.commands.CommandRegistry; import sciwhiz12.janitor.config.BotConfig; import sciwhiz12.janitor.listeners.StatusListener; +import sciwhiz12.janitor.utils.Util; import javax.security.auth.login.LoginException; @@ -16,10 +17,12 @@ import static sciwhiz12.janitor.Logging.STATUS; public class JanitorBot { private final JDA jda; private final BotConfig config; + private final BotConsole console; private final CommandRegistry cmdRegistry; public JanitorBot(JDABuilder jdaBuilder, BotConfig config) throws LoginException { this.config = config; + this.console = new BotConsole(this, System.in); this.cmdRegistry = new CommandRegistry(this, config.getCommandPrefix()); jdaBuilder .setActivity(Activity.playing("the Readying game...")) @@ -31,6 +34,7 @@ public class JanitorBot { ); this.jda = jdaBuilder.build(); JANITOR.info(STATUS, "Bot is built"); +// console.start(); } public JDA getJDA() { @@ -47,6 +51,22 @@ public class JanitorBot { public void shutdown() { JANITOR.info(STATUS, "Shutting down!"); + console.stop(); + getJDA().getRegisteredListeners().forEach(listener -> getJDA().removeEventListener(listener)); + getConfig().getOwnerID() + .map(id -> getJDA().getUserById(id)) + .ifPresent(owner -> owner.openPrivateChannel().submit() + .thenCompose(channel -> channel.sendMessage( + "Shutting down, in accordance with your orders. Goodbye!") + .submit()) + .whenComplete(Util.handle( + msg -> JANITOR + .debug(STATUS, "Sent shutdown message to owner: {}", + Util.toString(owner)), + err -> JANITOR + .error(STATUS, "Error while sending shutdown message to owner", err) + )) + .join()); getJDA().shutdown(); } } diff --git a/src/main/java/sciwhiz12/janitor/Logging.java b/src/main/java/sciwhiz12/janitor/Logging.java index 4f16289..a9c0c13 100644 --- a/src/main/java/sciwhiz12/janitor/Logging.java +++ b/src/main/java/sciwhiz12/janitor/Logging.java @@ -10,5 +10,6 @@ public class Logging { public static final Marker COMMANDS = MarkerFactory.getMarker("COMMANDS"); public static final Logger JANITOR = LoggerFactory.getLogger("janitor"); + public static final Logger CONSOLE = LoggerFactory.getLogger("janitor.console"); public static final Logger CONFIG = LoggerFactory.getLogger("janitor.config"); } diff --git a/src/main/java/sciwhiz12/janitor/commands/ShutdownCommand.java b/src/main/java/sciwhiz12/janitor/commands/ShutdownCommand.java index 78e1f79..9637217 100644 --- a/src/main/java/sciwhiz12/janitor/commands/ShutdownCommand.java +++ b/src/main/java/sciwhiz12/janitor/commands/ShutdownCommand.java @@ -1,17 +1,12 @@ package sciwhiz12.janitor.commands; import net.dv8tion.jda.api.events.message.MessageReceivedEvent; -import sciwhiz12.janitor.utils.Util; - -import static sciwhiz12.janitor.Logging.JANITOR; -import static sciwhiz12.janitor.Logging.STATUS; public class ShutdownCommand extends BaseCommand { private final long ownerID; public ShutdownCommand(CommandRegistry registry, long ownerID) { super(registry); - System.out.println(ownerID); this.ownerID = ownerID; } @@ -20,22 +15,8 @@ public class ShutdownCommand extends BaseCommand { if (event.getAuthor().getIdLong() == ownerID) { event.getMessage().getChannel() .sendMessage("Shutting down, in accordance with the owner's command. Goodbye all!") - .queue(); - registry.getBot().getConfig().getOwnerID() - .map(id -> event.getJDA().getUserById(id)) - .ifPresent(owner -> owner.openPrivateChannel().submit() - .thenCompose(channel -> channel.sendMessage( - "Shutting down, in accordance with your orders. Goodbye!") - .submit()) - .whenComplete(Util.handle( - msg -> JANITOR - .debug(STATUS, "Sent shutdown message to owner: {}", - Util.toString(owner)), - err -> JANITOR - .error(STATUS, "Error while sending shutdown message to owner", err) - )) - .thenAccept(v -> registry.getBot().shutdown()) - .join()); + .complete(); + registry.getBot().shutdown(); } } }