Skip to content

Commit

Permalink
Implement command registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Aizistral committed Sep 7, 2024
1 parent 93ef9cc commit 18e6978
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/aizistral/infmachine/commands/Command.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.aizistral.infmachine.commands;

import java.util.List;

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;

public interface Command {

public String getName();

public SlashCommandData getData();

public void onEvent(SlashCommandInteractionEvent event);

}
11 changes: 11 additions & 0 deletions src/main/java/com/aizistral/infmachine/commands/CommandOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.aizistral.infmachine.commands;

import net.dv8tion.jda.api.interactions.commands.OptionType;

public record CommandOption(OptionType type, String name, String description, boolean required) {

public CommandOption(OptionType type, String name, String description) {
this(type, name, description, true);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.aizistral.infmachine.commands;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.aizistral.infmachine.Bootstrap;
import com.aizistral.infmachine.config.Localization;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.hooks.EventListener;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;

public class CommandRegistry implements EventListener {
@Getter
private static CommandRegistry instance;
private final Map<String, Command> commands = new HashMap<>();

private CommandRegistry() {
// NO-OP
}

private void populate() {
this.register(new PingCommand());
}

private void register(Command command) {
this.commands.put(command.getData().getName(), command);
}

private void sendUpdate() {
Bootstrap.JDA.updateCommands().addCommands(this.commands.values().stream().map(Command::getData).toList()).queue();
}

@Override
public void onEvent(GenericEvent event) {
if (event instanceof SlashCommandInteractionEvent slashEvent) {
Command command = this.commands.get(slashEvent.getName());

if (command != null) {
command.onEvent(slashEvent);
} else {
slashEvent.reply(Localization.get("msg.commandNotFound")).queue();
}
}
}

public static void bootstrap() {
Preconditions.checkArgument(instance == null, "CommandRegistry already bootstrapped!");

instance = new CommandRegistry();
instance.populate();
instance.sendUpdate();
Bootstrap.JDA.addEventListener(instance);
}

}
28 changes: 28 additions & 0 deletions src/main/java/com/aizistral/infmachine/commands/PingCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.aizistral.infmachine.commands;

import com.aizistral.infmachine.config.Localization;

import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import net.dv8tion.jda.api.interactions.commands.build.Commands;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;

public class PingCommand implements Command {

@Override
public String getName() {
return "ping";
}

@Override
public SlashCommandData getData() {
return Commands.slash("ping", Localization.get("cmd.ping.desc"));
}

@Override
public void onEvent(SlashCommandInteractionEvent event) {
long time = System.currentTimeMillis();
event.reply("Pong!").setEphemeral(false).flatMap(v -> event.getHook().editOriginalFormat(
"Pong: %d ms", System.currentTimeMillis() - time)).queue();
}

}
1 change: 1 addition & 0 deletions src/main/resources/lang/local.lang
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,4 @@ msg.openVotingSuccess=Succesfully opened voting for user <@%s>!
msg.openVotingFail=Failed to open a new voting for user <@%s>. Is there one already?
msg.ratingOwn=## Your Rating Stats:\n\nLeaderboard position (by rating): **#%s**\nTotal rating acquired: **%s points**\n\nLeaderboard position (by messages): **#%s**\nTotal messages sent: **%s messages**
msg.rating=## <@%s>'s Rating Stats:\n\nLeaderboard position (by rating): **#%s**\nTotal rating acquired: **%s points**\n\nLeaderboard position (by messages): **#%s**\nTotal messages sent: **%s messages**
msg.commandNotFound=I know no such command...

0 comments on commit 18e6978

Please # to comment.