From 1f2668458ade0edb3b4426fa488d5b301eacb791 Mon Sep 17 00:00:00 2001 From: Arnold Alejo Nunag Date: Tue, 1 Sep 2020 16:57:22 +0800 Subject: [PATCH] Add shutdown command --- .../java/sciwhiz12/janitor/JanitorBot.java | 7 +++++++ .../janitor/commands/ShutdownCommand.java | 20 +++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 src/main/java/sciwhiz12/janitor/commands/ShutdownCommand.java diff --git a/src/main/java/sciwhiz12/janitor/JanitorBot.java b/src/main/java/sciwhiz12/janitor/JanitorBot.java index 271a9f5..0d9a314 100644 --- a/src/main/java/sciwhiz12/janitor/JanitorBot.java +++ b/src/main/java/sciwhiz12/janitor/JanitorBot.java @@ -10,6 +10,7 @@ import net.dv8tion.jda.api.requests.GatewayIntent; import sciwhiz12.janitor.commands.CommandRegistry; import sciwhiz12.janitor.commands.OKCommand; import sciwhiz12.janitor.commands.PingCommand; +import sciwhiz12.janitor.commands.ShutdownCommand; import javax.security.auth.login.LoginException; @@ -40,6 +41,9 @@ public class JanitorBot { .accepts("token", "The Discord token for the bot user").withRequiredArg().required(); ArgumentAcceptingOptionSpec prefix = parser .accepts("prefix", "The prefix for commands").withRequiredArg().defaultsTo("!"); + ArgumentAcceptingOptionSpec owner = parser.accepts("owner", + "The snowflake ID of the bot owner; Used for shutdowns and other bot management commands") + .withRequiredArg().ofType(Long.class); OptionSet options = parser.parse(args); @@ -55,6 +59,9 @@ public class JanitorBot { INSTANCE.getCommandRegistry().addCommand("ping", new PingCommand()); INSTANCE.getCommandRegistry().addCommand("ok", new OKCommand()); + if (options.has(owner)) { + INSTANCE.getCommandRegistry().addCommand("shutdown", new ShutdownCommand(owner.value(options))); + } System.out.println("Ready! Invite URL: " + inviteURL); } diff --git a/src/main/java/sciwhiz12/janitor/commands/ShutdownCommand.java b/src/main/java/sciwhiz12/janitor/commands/ShutdownCommand.java new file mode 100644 index 0000000..469c5f3 --- /dev/null +++ b/src/main/java/sciwhiz12/janitor/commands/ShutdownCommand.java @@ -0,0 +1,20 @@ +package sciwhiz12.janitor.commands; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import sciwhiz12.janitor.JanitorBot; + +public class ShutdownCommand implements ICommand { + private final long ownerID; + + public ShutdownCommand(long ownerID) { + this.ownerID = ownerID; + } + + @Override + public void onCommand(JanitorBot bot, MessageReceivedEvent event) { + if (event.getAuthor().getIdLong() == ownerID) { + event.getMessage().getChannel().sendMessage("Shutting down. Goodbye!").queue(); + event.getJDA().shutdown(); + } + } +}