Skip to content

Commit c93e1e2

Browse files
Added support for Context Commands
1 parent cf26d4f commit c93e1e2

83 files changed

Lines changed: 485 additions & 325 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ General utility bot for the [JavaDiscord Community](https://join.javadiscord.net
77
To start up, run the bot once, and it will generate a `config` directory. Stop the bot, and set the `jdaBotToken` (in `systems.json`) to contain your token. Additionally, you should set the `mongoDatabaseUrl` to a URL to your own instance of MongoDB.
88

99
# Commands
10-
Commands are defined in this bot using a `commands.yaml` configuration file. The data in this file is transformed at startup time into an array of `net.javadiscord.javabot.command.data.CommandConfig` objects using JSON deserialization.
10+
Commands are defined in this bot using a `commands.yaml` configuration file. The data in this file is transformed at startup time into an array of `net.javadiscord.javabot.command.data.slash_commands.SlashCommandConfig` objects using JSON deserialization.
1111

12-
These commands are then used by `net.javadiscord.javabot.command.SlashCommands#registerSlashCommands(Guild)` to register the defined commands as Discord slash commands which become available to users in guilds and private messages with the bot.
12+
These commands are then used by `net.javadiscord.javabot.command.InteractionHandler#registerSlashCommands(Guild)` to register the defined commands as Discord slash commands which become available to users in guilds and private messages with the bot.
1313

1414
**Each command MUST define a `handler` property, whose name is the fully-qualified class name of a `SlashCommandHandler`.** When registering commands, the bot will look for such a class, and attempt to create a new instance of it using a no-args constructor. Therefore, make sure that your handler class has a no-args constructor.
1515

‎src/main/java/net/javadiscord/javabot/Bot.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import net.dv8tion.jda.api.utils.ChunkingFilter;
1010
import net.dv8tion.jda.api.utils.MemberCachePolicy;
1111
import net.dv8tion.jda.api.utils.cache.CacheFlag;
12-
import net.javadiscord.javabot.command.SlashCommands;
12+
import net.javadiscord.javabot.command.InteractionHandler;
1313
import net.javadiscord.javabot.data.config.BotConfig;
1414
import net.javadiscord.javabot.data.h2db.DbHelper;
1515
import net.javadiscord.javabot.events.*;
@@ -42,10 +42,10 @@ public class Bot {
4242
/**
4343
* A reference to the slash command listener that's the main point of
4444
* interaction for users with this bot. It's marked as a publicly accessible
45-
* reference so that {@link SlashCommands#registerSlashCommands} can
45+
* reference so that {@link InteractionHandler#registerCommands} can
4646
* be called wherever it's needed.
4747
*/
48-
public static SlashCommands slashCommands;
48+
public static InteractionHandler interactionHandler;
4949
/**
5050
* An instance of {@link AutoMod}.
5151
* */
@@ -74,7 +74,7 @@ private Bot() {
7474
* <ol>
7575
* <li>Setting the time zone to UTC, to keep our sanity when working with times.</li>
7676
* <li>Loading the configuration JSON file.</li>
77-
* <li>Initializing the {@link SlashCommands} listener (which reads command data from a YAML file).</li>
77+
* <li>Initializing the {@link InteractionHandler} listener (which reads command data from a YAML file).</li>
7878
* <li>Creating and configuring the {@link JDA} instance that enables the bot's Discord connectivity.</li>
7979
* <li>Adding event listeners to the bot.</li>
8080
* </ol>
@@ -86,7 +86,7 @@ public static void main(String[] args) throws Exception {
8686
TimeZone.setDefault(TimeZone.getTimeZone(ZoneOffset.UTC));
8787
config = new BotConfig(Path.of("config"));
8888
dataSource = DbHelper.initDataSource(config);
89-
slashCommands = new SlashCommands();
89+
interactionHandler = new InteractionHandler();
9090
autoMod = new AutoMod();
9191
imageCache = new ImageCacheUtils();
9292
asyncPool = Executors.newScheduledThreadPool(config.getSystems().getAsyncPoolSize());
@@ -96,7 +96,7 @@ public static void main(String[] args) throws Exception {
9696
.setMemberCachePolicy(MemberCachePolicy.ALL)
9797
.enableCache(CacheFlag.ACTIVITY)
9898
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES)
99-
.addEventListeners(slashCommands)
99+
.addEventListeners(interactionHandler)
100100
.build();
101101
addEventListeners(jda);
102102
try {
@@ -110,7 +110,7 @@ public static void main(String[] args) throws Exception {
110110

111111
/**
112112
* Adds all the bot's event listeners to the JDA instance, except for the
113-
* main {@link SlashCommands} listener and {@link AutoMod}.
113+
* main {@link InteractionHandler} listener and {@link AutoMod}.
114114
*
115115
* @param jda The JDA bot instance to add listeners to.
116116
*/

‎src/main/java/net/javadiscord/javabot/command/DelegatingCommandHandler.java‎

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
44
import net.dv8tion.jda.api.requests.restaction.interactions.ReplyCallbackAction;
5+
import net.javadiscord.javabot.command.interfaces.ISlashCommand;
56

67
import java.util.Collections;
78
import java.util.HashMap;
@@ -13,23 +14,23 @@
1314
* that this parent handler can do the logic of finding the right subcommand to
1415
* invoke depending on the event received.
1516
*/
16-
public class DelegatingCommandHandler implements SlashCommandHandler {
17-
private final Map<String, SlashCommandHandler> subcommandHandlers;
18-
private final Map<String, SlashCommandHandler> subcommandGroupHandlers;
17+
public class DelegatingCommandHandler implements ISlashCommand {
18+
private final Map<String, ISlashCommand> subcommandHandlers;
19+
private final Map<String, ISlashCommand> subcommandGroupHandlers;
1920

2021
/**
2122
* Constructs the handler with an already-initialized map of subcommands.
2223
*
2324
* @param subcommandHandlers The map of subcommands to use.
2425
*/
25-
public DelegatingCommandHandler(Map<String, SlashCommandHandler> subcommandHandlers) {
26+
public DelegatingCommandHandler(Map<String, ISlashCommand> subcommandHandlers) {
2627
this.subcommandHandlers = subcommandHandlers;
2728
this.subcommandGroupHandlers = new HashMap<>();
2829
}
2930

3031
/**
3132
* Constructs the handler with an empty map, which subcommands can be added
32-
* to via {@link DelegatingCommandHandler#addSubcommand(String, SlashCommandHandler)}.
33+
* to via {@link DelegatingCommandHandler#addSubcommand(String, ISlashCommand)}.
3334
*/
3435
public DelegatingCommandHandler() {
3536
this.subcommandHandlers = new HashMap<>();
@@ -42,7 +43,7 @@ public DelegatingCommandHandler() {
4243
*
4344
* @return An unmodifiable map containing all registered subcommands.
4445
*/
45-
public Map<String, SlashCommandHandler> getSubcommandHandlers() {
46+
public Map<String, ISlashCommand> getSubcommandHandlers() {
4647
return Collections.unmodifiableMap(this.subcommandHandlers);
4748
}
4849

@@ -52,7 +53,7 @@ public Map<String, SlashCommandHandler> getSubcommandHandlers() {
5253
*
5354
* @return An unmodifiable map containing all registered group handlers.
5455
*/
55-
public Map<String, SlashCommandHandler> getSubcommandGroupHandlers() {
56+
public Map<String, ISlashCommand> getSubcommandGroupHandlers() {
5657
return Collections.unmodifiableMap(this.subcommandGroupHandlers);
5758
}
5859

@@ -65,7 +66,7 @@ public Map<String, SlashCommandHandler> getSubcommandGroupHandlers() {
6566
* @throws UnsupportedOperationException If this handler was initialized
6667
* with an unmodifiable map of subcommand handlers.
6768
*/
68-
protected void addSubcommand(String name, SlashCommandHandler handler) {
69+
protected void addSubcommand(String name, ISlashCommand handler) {
6970
this.subcommandHandlers.put(name, handler);
7071
}
7172

@@ -78,7 +79,7 @@ protected void addSubcommand(String name, SlashCommandHandler handler) {
7879
* @throws UnsupportedOperationException If this handler was initialized
7980
* with an unmodifiable map of subcommand group handlers.
8081
*/
81-
protected void addSubcommandGroup(String name, SlashCommandHandler handler) {
82+
protected void addSubcommandGroup(String name, ISlashCommand handler) {
8283
this.subcommandGroupHandlers.put(name, handler);
8384
}
8485

@@ -90,21 +91,21 @@ protected void addSubcommandGroup(String name, SlashCommandHandler handler) {
9091
* @return The reply action that is sent to the user.
9192
*/
9293
@Override
93-
public ReplyCallbackAction handle(SlashCommandInteractionEvent event) throws ResponseException {
94+
public ReplyCallbackAction handleSlashCommandInteraction(SlashCommandInteractionEvent event) throws ResponseException {
9495
// First we check if the event has specified a subcommand group, and if we have a group handler for it.
9596
if (event.getSubcommandGroup() != null) {
96-
SlashCommandHandler groupHandler = this.getSubcommandGroupHandlers().get(event.getSubcommandGroup());
97+
ISlashCommand groupHandler = this.getSubcommandGroupHandlers().get(event.getSubcommandGroup());
9798
if (groupHandler != null) {
98-
return groupHandler.handle(event);
99+
return groupHandler.handleSlashCommandInteraction(event);
99100
}
100101
}
101102
// If the event doesn't have a subcommand group, or no handler was found for the group, we just move on to the subcommand.
102103
if (event.getSubcommandName() == null) {
103104
return this.handleNonSubcommand(event);
104105
} else {
105-
SlashCommandHandler handler = this.getSubcommandHandlers().get(event.getSubcommandName());
106+
ISlashCommand handler = this.getSubcommandHandlers().get(event.getSubcommandName());
106107
if (handler != null) {
107-
return handler.handle(event);
108+
return handler.handleSlashCommandInteraction(event);
108109
} else {
109110
return Responses.warning(event, "Unknown Subcommand", "The subcommand you entered could not be found.");
110111
}

0 commit comments

Comments
 (0)