Skip to content

Commit 8a0d5e7

Browse files
committed
Added concept for combined command
1 parent 8a686bd commit 8a0d5e7

File tree

5 files changed

+126
-39
lines changed

5 files changed

+126
-39
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
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;
@@ -36,7 +37,7 @@ public static void main(String[] args) throws Exception {
3637
.setMemberCachePolicy(MemberCachePolicy.ALL)
3738
.enableCache(CacheFlag.ACTIVITY)
3839
.enableIntents(GatewayIntent.GUILD_MEMBERS, GatewayIntent.GUILD_PRESENCES)
39-
.addEventListeners(client)
40+
.addEventListeners(client, new SlashCommands(client))
4041
.build();
4142

4243
//EVENTS
@@ -50,7 +51,6 @@ public static void main(String[] args) throws Exception {
5051
jda.addEventListener(new CstmCmdListener());
5152
jda.addEventListener(new AutoMod());
5253
jda.addEventListener(new SubmissionListener());
53-
jda.addEventListener(new SlashCommands());
5454
//jda.addEventListener(new StarboardListener());
5555
}
5656

@@ -89,6 +89,7 @@ private static Command[] discoverCommands() {
8989
return reflections.getSubTypesOf(Command.class).stream()
9090
.map(type -> {
9191
try {
92+
if (Modifier.isAbstract(type.getModifiers())) return null;
9293
return (Command) type.getDeclaredConstructor().newInstance();
9394
} catch (Exception e) {
9495
e.printStackTrace();

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.google.gson.JsonObject;
44
import com.google.gson.JsonParser;
5+
import com.jagrosh.jdautilities.command.CommandClient;
56
import com.javadiscord.javabot.commands.other.qotw.Correct;
67
import com.javadiscord.javabot.commands.user_commands.*;
8+
import com.javadiscord.javabot.other.SlashEnabledCommand;
79
import com.mongodb.BasicDBObject;
810
import com.mongodb.client.MongoCollection;
911
import com.mongodb.client.MongoDatabase;
@@ -21,16 +23,31 @@
2123
import net.dv8tion.jda.api.requests.restaction.CommandUpdateAction;
2224
import org.bson.Document;
2325

26+
import java.util.HashMap;
27+
import java.util.Map;
28+
2429
import static com.javadiscord.javabot.events.Startup.mongoClient;
2530
import static net.dv8tion.jda.api.interactions.commands.OptionType.STRING;
2631
import static net.dv8tion.jda.api.interactions.commands.OptionType.USER;
2732

2833
public class SlashCommands extends ListenerAdapter {
34+
private final Map<String, SlashEnabledCommand> commandsIndex;
35+
36+
public SlashCommands(CommandClient commandClient) {
37+
this.commandsIndex = new HashMap<>();
38+
this.registerSlashCommands(commandClient);
39+
}
2940

3041
@Override
3142
public void onSlashCommand(SlashCommandEvent event) {
3243
if (event.getGuild() == null) return;
3344

45+
var command = this.commandsIndex.get(event.getName());
46+
if (command != null) {
47+
command.execute(event);
48+
return;
49+
}
50+
3451
switch (event.getName()) {
3552

3653
case "avatar":
@@ -73,7 +90,7 @@ public void onSlashCommand(SlashCommandEvent event) {
7390
break;
7491

7592
case "ping":
76-
Ping.exCommand(event);
93+
new Ping().execute(event);
7794
break;
7895

7996
case "profile":
@@ -100,7 +117,17 @@ public void onSlashCommand(SlashCommandEvent event) {
100117
}
101118
}
102119

103-
120+
private void registerSlashCommands(CommandClient commandClient) {
121+
for (var cmd : commandClient.getCommands()) {
122+
if (cmd instanceof SlashEnabledCommand) {
123+
var slashCommand = (SlashEnabledCommand) cmd;
124+
this.commandsIndex.put(slashCommand.getName(), slashCommand);
125+
for (var alias : slashCommand.getAliases()) {
126+
this.commandsIndex.put(alias, slashCommand);
127+
}
128+
}
129+
}
130+
}
104131

105132
@Override
106133
public void onReady(ReadyEvent event){
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)