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

Add basic command system

This commit is contained in:
Arnold Alejo Nunag 2020-09-01 04:27:48 +08:00
parent 52210ff999
commit f1d77e11db
Signed by: sciwhiz12
GPG Key ID: 622CF446534317E1
8 changed files with 162 additions and 11 deletions

5
.gitignore vendored
View File

@ -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

View File

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

View File

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

View File

@ -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<String> token = parser
.accepts("token", "The Discord token for the bot user").withRequiredArg().required();
ArgumentAcceptingOptionSpec<String> 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);
}
}

View File

@ -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<String, ICommand> 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);
}
}
}

View File

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

View File

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

View File

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