-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/main/java/com/aizistral/infmachine/commands/Command.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
11
src/main/java/com/aizistral/infmachine/commands/CommandOption.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
65 changes: 65 additions & 0 deletions
65
src/main/java/com/aizistral/infmachine/commands/CommandRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
28
src/main/java/com/aizistral/infmachine/commands/PingCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters