Skip to content

Commit 12009a5

Browse files
author
Dynxsty
authored
Merge pull request #8 from Java-Discord/andrew/command_organization
Proposal for Combined Slash and Normal Commands
2 parents cfaeb78 + 00df7e2 commit 12009a5

File tree

6 files changed

+143
-61
lines changed

6 files changed

+143
-61
lines changed

src/main/java/com/javadiscord/javabot/Bot.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.javadiscord.javabot;
22

33
import com.jagrosh.jdautilities.command.Command;
4+
import com.jagrosh.jdautilities.command.CommandClient;
45
import com.jagrosh.jdautilities.command.CommandClientBuilder;
5-
import com.jagrosh.jdautilities.commons.waiter.EventWaiter;
66
import com.javadiscord.javabot.events.*;
77
import com.javadiscord.javabot.properties.MultiProperties;
88
import net.dv8tion.jda.api.JDA;
@@ -13,39 +13,33 @@
1313
import net.dv8tion.jda.api.utils.cache.CacheFlag;
1414
import org.reflections.Reflections;
1515

16+
import java.lang.reflect.Modifier;
1617
import java.nio.file.Path;
1718
import java.util.Objects;
1819
import java.util.Properties;
1920

2021

2122
public class Bot {
22-
23-
public static JDA jda;
24-
public static EventWaiter waiter;
25-
2623
private static final Properties properties = new MultiProperties(Path.of("bot.props"));
2724

2825
public static void main(String[] args) throws Exception {
29-
waiter = new EventWaiter();
30-
31-
CommandClientBuilder client = new CommandClientBuilder()
26+
CommandClient client = new CommandClientBuilder()
3227
.setOwnerId("374328434677121036")
3328
.setCoOwnerIds("299555811804315648", "620615131256061972")
3429
.setPrefix("!")
3530
.setEmojis("✅", "⚠️", "❌")
3631
.useHelpBuilder(false)
37-
.addCommands(discoverCommands());
38-
32+
.addCommands(discoverCommands())
33+
.build();
3934

40-
jda = JDABuilder.createDefault(properties.getProperty("token", "null"))
35+
JDA jda = JDABuilder.createDefault(properties.getProperty("token", "null"))
4136
.setStatus(OnlineStatus.DO_NOT_DISTURB)
4237
.setMemberCachePolicy(MemberCachePolicy.ALL)
4338
.enableCache(CacheFlag.ACTIVITY)
4439
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES)
40+
.addEventListeners(client, new SlashCommands(client))
4541
.build();
4642

47-
jda.addEventListener(waiter, client.build());
48-
4943
//EVENTS
5044
jda.addEventListener(new GuildJoin());
5145
jda.addEventListener(new UserJoin());
@@ -57,7 +51,6 @@ public static void main(String[] args) throws Exception {
5751
jda.addEventListener(new CstmCmdListener());
5852
jda.addEventListener(new AutoMod());
5953
jda.addEventListener(new SubmissionListener());
60-
jda.addEventListener(new SlashCommands());
6154
//jda.addEventListener(new StarboardListener());
6255
}
6356

@@ -96,6 +89,7 @@ private static Command[] discoverCommands() {
9689
return reflections.getSubTypesOf(Command.class).stream()
9790
.map(type -> {
9891
try {
92+
if (Modifier.isAbstract(type.getModifiers())) return null;
9993
return (Command) type.getDeclaredConstructor().newInstance();
10094
} catch (Exception e) {
10195
e.printStackTrace();

src/main/java/com/javadiscord/javabot/SlashCommands.java

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.javadiscord.javabot;
22

3-
import com.javadiscord.javabot.commands.other.qotw.Correct;
4-
import com.javadiscord.javabot.commands.user_commands.*;
5-
import com.javadiscord.javabot.other.Constants;
63
import com.google.gson.JsonObject;
74
import com.google.gson.JsonParser;
5+
import com.jagrosh.jdautilities.command.CommandClient;
6+
import com.javadiscord.javabot.commands.other.qotw.Correct;
7+
import com.javadiscord.javabot.commands.user_commands.*;
8+
import com.javadiscord.javabot.other.SlashEnabledCommand;
89
import com.mongodb.BasicDBObject;
910
import com.mongodb.client.MongoCollection;
1011
import com.mongodb.client.MongoDatabase;
@@ -22,16 +23,35 @@
2223
import net.dv8tion.jda.api.requests.restaction.CommandUpdateAction;
2324
import org.bson.Document;
2425

26+
import java.util.HashMap;
27+
import java.util.Map;
28+
2529
import static com.javadiscord.javabot.events.Startup.mongoClient;
2630
import static net.dv8tion.jda.api.interactions.commands.OptionType.STRING;
2731
import static net.dv8tion.jda.api.interactions.commands.OptionType.USER;
2832

2933
public class SlashCommands extends ListenerAdapter {
34+
/**
35+
* Maps every command name and alias to an instance of the command, for
36+
* constant-time lookup.
37+
*/
38+
private final Map<String, SlashEnabledCommand> commandsIndex;
39+
40+
public SlashCommands(CommandClient commandClient) {
41+
this.commandsIndex = new HashMap<>();
42+
this.registerSlashCommands(commandClient);
43+
}
3044

3145
@Override
3246
public void onSlashCommand(SlashCommandEvent event) {
3347
if (event.getGuild() == null) return;
3448

49+
var command = this.commandsIndex.get(event.getName());
50+
if (command != null) {
51+
command.execute(event);
52+
return;
53+
}
54+
3555
switch (event.getName()) {
3656

3757
case "avatar":
@@ -73,10 +93,6 @@ public void onSlashCommand(SlashCommandEvent event) {
7393
Lmgtfy.exCommand(event, event.getOption("text").getAsString());
7494
break;
7595

76-
case "ping":
77-
Ping.exCommand(event);
78-
break;
79-
8096
case "profile":
8197

8298
OptionMapping profileOption = event.getOption("user");
@@ -101,12 +117,25 @@ public void onSlashCommand(SlashCommandEvent event) {
101117
}
102118
}
103119

104-
120+
/**
121+
* Registers all slash commands in the command index.
122+
* @param commandClient The command client to register commands from.
123+
*/
124+
private void registerSlashCommands(CommandClient commandClient) {
125+
for (var cmd : commandClient.getCommands()) {
126+
if (cmd instanceof SlashEnabledCommand) {
127+
var slashCommand = (SlashEnabledCommand) cmd;
128+
this.commandsIndex.put(slashCommand.getName(), slashCommand);
129+
for (var alias : slashCommand.getAliases()) {
130+
this.commandsIndex.put(alias, slashCommand);
131+
}
132+
}
133+
}
134+
}
105135

106136
@Override
107137
public void onReady(ReadyEvent event){
108-
109-
CommandUpdateAction commands = Bot.jda.getGuilds().get(0).updateCommands();
138+
CommandUpdateAction commands = event.getJDA().getGuilds().get(0).updateCommands();
110139

111140
// Simple reply Commands
112141
commands.addCommands(

src/main/java/com/javadiscord/javabot/StatusUpdate.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ public void onReady(ReadyEvent event) {
2424
Activity.listening(event.getJDA().getGuilds().get(0).getMemberCount() + " members" + " | " + new Version().getVersion()),
2525
Activity.watching("!help" + " | " + new Version().getVersion())};
2626

27-
Bot.jda.getPresence().setStatus(OnlineStatus.DO_NOT_DISTURB);
28-
Bot.jda.getPresence().setActivity(activities[currentIndex]);
27+
event.getJDA().getPresence().setStatus(OnlineStatus.DO_NOT_DISTURB);
28+
event.getJDA().getPresence().setActivity(activities[currentIndex]);
2929
currentIndex = (currentIndex + 1) % activities.length;
3030
}, 0, 35, TimeUnit.SECONDS);
3131
}
Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,26 @@
11
package com.javadiscord.javabot.commands.user_commands;
22

3-
import com.javadiscord.javabot.events.Startup;
4-
import com.jagrosh.jdautilities.command.Command;
5-
import com.jagrosh.jdautilities.command.CommandEvent;
3+
import com.javadiscord.javabot.other.Constants;
4+
import com.javadiscord.javabot.other.SlashEnabledCommand;
5+
import com.javadiscord.javabot.other.SlashEnabledCommandEvent;
66
import net.dv8tion.jda.api.EmbedBuilder;
7-
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
8-
9-
import java.awt.*;
10-
11-
public class Ping extends Command {
12-
13-
public static void exCommand (CommandEvent event) {
14-
15-
long gatewayPing = event.getJDA().getGatewayPing();
16-
String botImage = Startup.bot.getAvatarUrl();
17-
18-
EmbedBuilder eb = new EmbedBuilder()
19-
.setAuthor(gatewayPing + "ms", null, botImage)
20-
.setColor(new Color(0x2F3136));
21-
event.reply(eb.build());
22-
}
23-
24-
public static void exCommand (SlashCommandEvent event) {
25-
26-
long gatewayPing = event.getJDA().getGatewayPing();
27-
String botImage = Startup.bot.getAvatarUrl();
28-
29-
EmbedBuilder eb = new EmbedBuilder()
30-
.setAuthor(gatewayPing + "ms", null, botImage)
31-
.setColor(new Color(0x2F3136));
32-
33-
event.replyEmbeds(eb.build()).queue();
34-
}
357

8+
public class Ping extends SlashEnabledCommand {
369
public Ping() {
37-
3810
this.name = "ping";
3911
this.aliases = new String[]{"pong"};
4012
this.category = new Category("USER COMMANDS");
4113
this.help = "Checks Java's gateway ping";
4214
}
4315

44-
protected void execute(CommandEvent event) {
45-
46-
exCommand(event);
16+
@Override
17+
protected void execute(SlashEnabledCommandEvent event) {
18+
long gatewayPing = event.getJDA().getGatewayPing();
19+
String botImage = event.getJDA().getSelfUser().getAvatarUrl();
20+
var e = new EmbedBuilder()
21+
.setAuthor(gatewayPing + "ms", null, botImage)
22+
.setColor(Constants.GRAY)
23+
.build();
24+
event.reply(e);
4725
}
4826
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.javadiscord.javabot.other;
2+
3+
import com.jagrosh.jdautilities.command.Command;
4+
import com.jagrosh.jdautilities.command.CommandEvent;
5+
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
6+
7+
public abstract class SlashEnabledCommand extends Command {
8+
@Override
9+
protected void execute(CommandEvent event) {
10+
this.execute(new SlashEnabledCommandEvent(event));
11+
}
12+
13+
public void execute(SlashCommandEvent event) {
14+
this.execute(new SlashEnabledCommandEvent(event));
15+
}
16+
17+
protected abstract void execute(SlashEnabledCommandEvent event);
18+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.javadiscord.javabot.other;
2+
3+
import com.jagrosh.jdautilities.command.CommandEvent;
4+
import net.dv8tion.jda.api.JDA;
5+
import net.dv8tion.jda.api.entities.MessageChannel;
6+
import net.dv8tion.jda.api.entities.MessageEmbed;
7+
import net.dv8tion.jda.api.entities.User;
8+
import net.dv8tion.jda.api.events.interaction.SlashCommandEvent;
9+
10+
import java.util.Optional;
11+
12+
public class SlashEnabledCommandEvent {
13+
private final CommandEvent messageCommandEvent;
14+
private final SlashCommandEvent slashCommandEvent;
15+
16+
private final JDA jda;
17+
private final MessageChannel channel;
18+
private final User user;
19+
20+
public SlashEnabledCommandEvent(CommandEvent messageCommandEvent) {
21+
this.messageCommandEvent = messageCommandEvent;
22+
this.slashCommandEvent = null;
23+
this.jda = messageCommandEvent.getJDA();
24+
this.channel = messageCommandEvent.getChannel();
25+
this.user = messageCommandEvent.getAuthor();
26+
}
27+
28+
public SlashEnabledCommandEvent(SlashCommandEvent slashCommandEvent) {
29+
this.slashCommandEvent = slashCommandEvent;
30+
this.messageCommandEvent = null;
31+
this.jda = slashCommandEvent.getJDA();
32+
this.channel = slashCommandEvent.getChannel();
33+
this.user = slashCommandEvent.getUser();
34+
}
35+
36+
public Optional<CommandEvent> getMessageCommandEvent() {
37+
return Optional.ofNullable(messageCommandEvent);
38+
}
39+
40+
public Optional<SlashCommandEvent> getSlashCommandEvent() {
41+
return Optional.ofNullable(slashCommandEvent);
42+
}
43+
44+
public JDA getJDA() {
45+
return jda;
46+
}
47+
48+
public MessageChannel getChannel() {
49+
return channel;
50+
}
51+
52+
public User getUser() {
53+
return user;
54+
}
55+
56+
public void reply(MessageEmbed embed) {
57+
if (this.messageCommandEvent != null) {
58+
this.messageCommandEvent.reply(embed);
59+
} else {
60+
this.slashCommandEvent.replyEmbeds(embed).queue();
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)