From f1d77e11db78418de5982df7fcd79794a2e510a2 Mon Sep 17 00:00:00 2001 From: Arnold Alejo Nunag Date: Tue, 1 Sep 2020 04:27:48 +0800 Subject: [PATCH] Add basic command system --- .gitignore | 5 +- build.gradle | 20 ++++--- .../sciwhiz12/janitor/CommandListener.java | 14 +++++ .../java/sciwhiz12/janitor/JanitorBot.java | 60 ++++++++++++++++++- .../janitor/commands/CommandRegistry.java | 44 ++++++++++++++ .../sciwhiz12/janitor/commands/ICommand.java | 8 +++ .../sciwhiz12/janitor/commands/OKCommand.java | 11 ++++ .../janitor/commands/PingCommand.java | 11 ++++ 8 files changed, 162 insertions(+), 11 deletions(-) create mode 100644 src/main/java/sciwhiz12/janitor/CommandListener.java create mode 100644 src/main/java/sciwhiz12/janitor/commands/CommandRegistry.java create mode 100644 src/main/java/sciwhiz12/janitor/commands/ICommand.java create mode 100644 src/main/java/sciwhiz12/janitor/commands/OKCommand.java create mode 100644 src/main/java/sciwhiz12/janitor/commands/PingCommand.java diff --git a/.gitignore b/.gitignore index 369640a..e9d2177 100644 --- a/.gitignore +++ b/.gitignore @@ -1,11 +1,14 @@ # From https://github.com/github/gitignore/blob/master/Gradle.gitignore .gradle -**/build/ +build/ !src/**/build/ gradle-app.setting !gradle-wrapper.jar .gradletasknamecache +.idea/ +out/ + # # Work around https://youtrack.jetbrains.com/issue/IDEA-116898 # gradle/wrapper/gradle-wrapper.properties diff --git a/build.gradle b/build.gradle index 0d15f2c..cf015ba 100644 --- a/build.gradle +++ b/build.gradle @@ -26,15 +26,19 @@ archivesBaseName = 'janitor_bot' version = getVersion() println("Version: ${version}") +shadowJar { + configurations = [project.configurations.implementation] +} + dependencies { - shadow "net.dv8tion:JDA:${jda_version}" - shadow "com.electronwill.night-config:toml:${nightconfig_version}" - shadow "net.sf.jopt-simple:jopt-simple:${jopt_version}" - shadow "com.google.guava:guava:${guava_version}" - shadow "com.google.code.gson:gson:${gson_version}" - shadow "org.apache.logging.log4j:log4j-api:${log4j_version}" - shadow "org.apache.logging.log4j:log4j-core:${log4j_version}" - shadow "org.apache.logging.log4j:log4j-slf4j-impl:${log4j_version}" + implementation "net.dv8tion:JDA:${jda_version}" + implementation "com.electronwill.night-config:toml:${nightconfig_version}" + implementation "net.sf.jopt-simple:jopt-simple:${jopt_version}" + implementation "com.google.guava:guava:${guava_version}" + implementation "com.google.code.gson:gson:${gson_version}" + implementation "org.apache.logging.log4j:log4j-api:${log4j_version}" + implementation "org.apache.logging.log4j:log4j-core:${log4j_version}" + implementation "org.apache.logging.log4j:log4j-slf4j-impl:${log4j_version}" testImplementation "junit:junit:${junit_version}" } diff --git a/src/main/java/sciwhiz12/janitor/CommandListener.java b/src/main/java/sciwhiz12/janitor/CommandListener.java new file mode 100644 index 0000000..b7e24dc --- /dev/null +++ b/src/main/java/sciwhiz12/janitor/CommandListener.java @@ -0,0 +1,14 @@ +package sciwhiz12.janitor; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import net.dv8tion.jda.api.hooks.ListenerAdapter; +import org.jetbrains.annotations.NotNull; + +public class CommandListener extends ListenerAdapter { + public static final CommandListener INSTANCE = new CommandListener(); + + @Override + public void onMessageReceived(@NotNull MessageReceivedEvent event) { + JanitorBot.INSTANCE.getCommandRegistry().parseMessage(event); + } +} diff --git a/src/main/java/sciwhiz12/janitor/JanitorBot.java b/src/main/java/sciwhiz12/janitor/JanitorBot.java index 8850da1..271a9f5 100644 --- a/src/main/java/sciwhiz12/janitor/JanitorBot.java +++ b/src/main/java/sciwhiz12/janitor/JanitorBot.java @@ -1,5 +1,61 @@ package sciwhiz12.janitor; -public class JanitorBot -{ +import joptsimple.ArgumentAcceptingOptionSpec; +import joptsimple.OptionParser; +import joptsimple.OptionSet; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.requests.GatewayIntent; +import sciwhiz12.janitor.commands.CommandRegistry; +import sciwhiz12.janitor.commands.OKCommand; +import sciwhiz12.janitor.commands.PingCommand; + +import javax.security.auth.login.LoginException; + +public class JanitorBot { + public static JanitorBot INSTANCE; + + private final JDA jda; + private final CommandRegistry cmdRegistry; + + public JanitorBot(JDA jda, String prefix) { + this.jda = jda; + this.cmdRegistry = new CommandRegistry(this, prefix); + } + + public JDA getDiscord() { + return this.jda; + } + + public CommandRegistry getCommandRegistry() { + return this.cmdRegistry; + } + + public static void main(String[] args) throws LoginException { + System.out.println("Starting..."); + + OptionParser parser = new OptionParser(); + ArgumentAcceptingOptionSpec token = parser + .accepts("token", "The Discord token for the bot user").withRequiredArg().required(); + ArgumentAcceptingOptionSpec prefix = parser + .accepts("prefix", "The prefix for commands").withRequiredArg().defaultsTo("!"); + + OptionSet options = parser.parse(args); + + System.out.println("Configuring and connecting..."); + + JDABuilder builder = JDABuilder.createDefault(token.value(options)); + builder.addEventListeners(CommandListener.INSTANCE); + builder.enableIntents(GatewayIntent.GUILD_MESSAGES, GatewayIntent.GUILD_MESSAGE_REACTIONS); + JDA jda = builder.build(); + INSTANCE = new JanitorBot(jda, prefix.value(options)); + + String inviteURL = jda.getInviteUrl(Permission.ADMINISTRATOR); + + INSTANCE.getCommandRegistry().addCommand("ping", new PingCommand()); + INSTANCE.getCommandRegistry().addCommand("ok", new OKCommand()); + + System.out.println("Ready! Invite URL: " + inviteURL); + } } diff --git a/src/main/java/sciwhiz12/janitor/commands/CommandRegistry.java b/src/main/java/sciwhiz12/janitor/commands/CommandRegistry.java new file mode 100644 index 0000000..dec324f --- /dev/null +++ b/src/main/java/sciwhiz12/janitor/commands/CommandRegistry.java @@ -0,0 +1,44 @@ +package sciwhiz12.janitor.commands; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import sciwhiz12.janitor.JanitorBot; + +import java.util.HashMap; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class CommandRegistry { + private final JanitorBot bot; + private final String prefix; + private final Pattern pattern; + private final Map registry = new HashMap<>(); + + public CommandRegistry(JanitorBot bot, String prefix) { + this.bot = bot; + this.prefix = prefix; + this.pattern = Pattern.compile("^" + prefix + "([A-Za-z0-9]+).*$"); + } + + public void addCommand(String cmd, ICommand instance) { + registry.put(cmd, instance); + } + + public void parseMessage(MessageReceivedEvent event) { + try { + String msg = event.getMessage().getContentStripped(); + Matcher matcher = pattern.matcher(msg); + if (!matcher.matches()) { return; } + String cmd = matcher.group(1); + if (registry.containsKey(cmd)) { + System.out.printf("Received command: %s ; full message: %s%n", cmd, msg); + registry.get(cmd).onCommand(bot, event); + } + } + catch (Exception e) { + System.err + .printf("Error while parsing message: %s ; %s%n", event.getMessage().getContentStripped(), + e); + } + } +} diff --git a/src/main/java/sciwhiz12/janitor/commands/ICommand.java b/src/main/java/sciwhiz12/janitor/commands/ICommand.java new file mode 100644 index 0000000..e866a7e --- /dev/null +++ b/src/main/java/sciwhiz12/janitor/commands/ICommand.java @@ -0,0 +1,8 @@ +package sciwhiz12.janitor.commands; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import sciwhiz12.janitor.JanitorBot; + +public interface ICommand { + void onCommand(JanitorBot bot, MessageReceivedEvent event); +} diff --git a/src/main/java/sciwhiz12/janitor/commands/OKCommand.java b/src/main/java/sciwhiz12/janitor/commands/OKCommand.java new file mode 100644 index 0000000..a3a78ec --- /dev/null +++ b/src/main/java/sciwhiz12/janitor/commands/OKCommand.java @@ -0,0 +1,11 @@ +package sciwhiz12.janitor.commands; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import sciwhiz12.janitor.JanitorBot; + +public class OKCommand implements ICommand { + @Override + public void onCommand(JanitorBot bot, MessageReceivedEvent event) { + event.getMessage().addReaction("\uD83D\uDC4C").queue(); + } +} diff --git a/src/main/java/sciwhiz12/janitor/commands/PingCommand.java b/src/main/java/sciwhiz12/janitor/commands/PingCommand.java new file mode 100644 index 0000000..a737e32 --- /dev/null +++ b/src/main/java/sciwhiz12/janitor/commands/PingCommand.java @@ -0,0 +1,11 @@ +package sciwhiz12.janitor.commands; + +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; +import sciwhiz12.janitor.JanitorBot; + +public class PingCommand implements ICommand { + @Override + public void onCommand(JanitorBot bot, MessageReceivedEvent event) { + event.getMessage().getChannel().sendMessage("Pong!").queue(); + } +}