mirror of
https://github.com/sciwhiz12/Janitor.git
synced 2024-11-10 04:31:26 +00:00
Add basic command system
This commit is contained in:
parent
52210ff999
commit
f1d77e11db
5
.gitignore
vendored
5
.gitignore
vendored
|
@ -1,11 +1,14 @@
|
||||||
# From https://github.com/github/gitignore/blob/master/Gradle.gitignore
|
# From https://github.com/github/gitignore/blob/master/Gradle.gitignore
|
||||||
|
|
||||||
.gradle
|
.gradle
|
||||||
**/build/
|
build/
|
||||||
!src/**/build/
|
!src/**/build/
|
||||||
gradle-app.setting
|
gradle-app.setting
|
||||||
!gradle-wrapper.jar
|
!gradle-wrapper.jar
|
||||||
.gradletasknamecache
|
.gradletasknamecache
|
||||||
|
|
||||||
|
.idea/
|
||||||
|
out/
|
||||||
|
|
||||||
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
|
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
|
||||||
# gradle/wrapper/gradle-wrapper.properties
|
# gradle/wrapper/gradle-wrapper.properties
|
||||||
|
|
20
build.gradle
20
build.gradle
|
@ -26,15 +26,19 @@ archivesBaseName = 'janitor_bot'
|
||||||
version = getVersion()
|
version = getVersion()
|
||||||
println("Version: ${version}")
|
println("Version: ${version}")
|
||||||
|
|
||||||
|
shadowJar {
|
||||||
|
configurations = [project.configurations.implementation]
|
||||||
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
shadow "net.dv8tion:JDA:${jda_version}"
|
implementation "net.dv8tion:JDA:${jda_version}"
|
||||||
shadow "com.electronwill.night-config:toml:${nightconfig_version}"
|
implementation "com.electronwill.night-config:toml:${nightconfig_version}"
|
||||||
shadow "net.sf.jopt-simple:jopt-simple:${jopt_version}"
|
implementation "net.sf.jopt-simple:jopt-simple:${jopt_version}"
|
||||||
shadow "com.google.guava:guava:${guava_version}"
|
implementation "com.google.guava:guava:${guava_version}"
|
||||||
shadow "com.google.code.gson:gson:${gson_version}"
|
implementation "com.google.code.gson:gson:${gson_version}"
|
||||||
shadow "org.apache.logging.log4j:log4j-api:${log4j_version}"
|
implementation "org.apache.logging.log4j:log4j-api:${log4j_version}"
|
||||||
shadow "org.apache.logging.log4j:log4j-core:${log4j_version}"
|
implementation "org.apache.logging.log4j:log4j-core:${log4j_version}"
|
||||||
shadow "org.apache.logging.log4j:log4j-slf4j-impl:${log4j_version}"
|
implementation "org.apache.logging.log4j:log4j-slf4j-impl:${log4j_version}"
|
||||||
|
|
||||||
testImplementation "junit:junit:${junit_version}"
|
testImplementation "junit:junit:${junit_version}"
|
||||||
}
|
}
|
||||||
|
|
14
src/main/java/sciwhiz12/janitor/CommandListener.java
Normal file
14
src/main/java/sciwhiz12/janitor/CommandListener.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,5 +1,61 @@
|
||||||
package sciwhiz12.janitor;
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
8
src/main/java/sciwhiz12/janitor/commands/ICommand.java
Normal file
8
src/main/java/sciwhiz12/janitor/commands/ICommand.java
Normal 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);
|
||||||
|
}
|
11
src/main/java/sciwhiz12/janitor/commands/OKCommand.java
Normal file
11
src/main/java/sciwhiz12/janitor/commands/OKCommand.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
11
src/main/java/sciwhiz12/janitor/commands/PingCommand.java
Normal file
11
src/main/java/sciwhiz12/janitor/commands/PingCommand.java
Normal 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user