mirror of
https://github.com/sciwhiz12/Janitor.git
synced 2024-09-20 00:34:02 +00:00
Add command line console
This commit is contained in:
parent
c633802786
commit
e8c09c1a85
81
src/main/java/sciwhiz12/janitor/BotConsole.java
Normal file
81
src/main/java/sciwhiz12/janitor/BotConsole.java
Normal 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");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,6 +7,7 @@ import net.dv8tion.jda.api.entities.Activity;
|
||||||
import sciwhiz12.janitor.commands.CommandRegistry;
|
import sciwhiz12.janitor.commands.CommandRegistry;
|
||||||
import sciwhiz12.janitor.config.BotConfig;
|
import sciwhiz12.janitor.config.BotConfig;
|
||||||
import sciwhiz12.janitor.listeners.StatusListener;
|
import sciwhiz12.janitor.listeners.StatusListener;
|
||||||
|
import sciwhiz12.janitor.utils.Util;
|
||||||
|
|
||||||
import javax.security.auth.login.LoginException;
|
import javax.security.auth.login.LoginException;
|
||||||
|
|
||||||
|
@ -16,10 +17,12 @@ import static sciwhiz12.janitor.Logging.STATUS;
|
||||||
public class JanitorBot {
|
public class JanitorBot {
|
||||||
private final JDA jda;
|
private final JDA jda;
|
||||||
private final BotConfig config;
|
private final BotConfig config;
|
||||||
|
private final BotConsole console;
|
||||||
private final CommandRegistry cmdRegistry;
|
private final CommandRegistry cmdRegistry;
|
||||||
|
|
||||||
public JanitorBot(JDABuilder jdaBuilder, BotConfig config) throws LoginException {
|
public JanitorBot(JDABuilder jdaBuilder, BotConfig config) throws LoginException {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
|
this.console = new BotConsole(this, System.in);
|
||||||
this.cmdRegistry = new CommandRegistry(this, config.getCommandPrefix());
|
this.cmdRegistry = new CommandRegistry(this, config.getCommandPrefix());
|
||||||
jdaBuilder
|
jdaBuilder
|
||||||
.setActivity(Activity.playing("the Readying game..."))
|
.setActivity(Activity.playing("the Readying game..."))
|
||||||
|
@ -31,6 +34,7 @@ public class JanitorBot {
|
||||||
);
|
);
|
||||||
this.jda = jdaBuilder.build();
|
this.jda = jdaBuilder.build();
|
||||||
JANITOR.info(STATUS, "Bot is built");
|
JANITOR.info(STATUS, "Bot is built");
|
||||||
|
// console.start();
|
||||||
}
|
}
|
||||||
|
|
||||||
public JDA getJDA() {
|
public JDA getJDA() {
|
||||||
|
@ -47,6 +51,22 @@ public class JanitorBot {
|
||||||
|
|
||||||
public void shutdown() {
|
public void shutdown() {
|
||||||
JANITOR.info(STATUS, "Shutting down!");
|
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();
|
getJDA().shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,5 +10,6 @@ public class Logging {
|
||||||
public static final Marker COMMANDS = MarkerFactory.getMarker("COMMANDS");
|
public static final Marker COMMANDS = MarkerFactory.getMarker("COMMANDS");
|
||||||
|
|
||||||
public static final Logger JANITOR = LoggerFactory.getLogger("janitor");
|
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");
|
public static final Logger CONFIG = LoggerFactory.getLogger("janitor.config");
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +1,12 @@
|
||||||
package sciwhiz12.janitor.commands;
|
package sciwhiz12.janitor.commands;
|
||||||
|
|
||||||
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
|
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 {
|
public class ShutdownCommand extends BaseCommand {
|
||||||
private final long ownerID;
|
private final long ownerID;
|
||||||
|
|
||||||
public ShutdownCommand(CommandRegistry registry, long ownerID) {
|
public ShutdownCommand(CommandRegistry registry, long ownerID) {
|
||||||
super(registry);
|
super(registry);
|
||||||
System.out.println(ownerID);
|
|
||||||
this.ownerID = ownerID;
|
this.ownerID = ownerID;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -20,22 +15,8 @@ public class ShutdownCommand extends BaseCommand {
|
||||||
if (event.getAuthor().getIdLong() == ownerID) {
|
if (event.getAuthor().getIdLong() == ownerID) {
|
||||||
event.getMessage().getChannel()
|
event.getMessage().getChannel()
|
||||||
.sendMessage("Shutting down, in accordance with the owner's command. Goodbye all!")
|
.sendMessage("Shutting down, in accordance with the owner's command. Goodbye all!")
|
||||||
.queue();
|
.complete();
|
||||||
registry.getBot().getConfig().getOwnerID()
|
registry.getBot().shutdown();
|
||||||
.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());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user