1
0
mirror of https://github.com/sciwhiz12/Janitor.git synced 2024-09-19 21:04:02 +00:00

Add command line console

This commit is contained in:
Arnold Alejo Nunag 2020-09-02 19:08:22 +08:00
parent c633802786
commit e8c09c1a85
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
4 changed files with 104 additions and 21 deletions

View File

@ -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");
}
}
}

View File

@ -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();
}
}

View File

@ -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");
}

View File

@ -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();
}
}
}