Skip to content

Commit fa39829

Browse files
committed
Converted some commands to new command system.
1 parent 1adc682 commit fa39829

File tree

5 files changed

+131
-49
lines changed

5 files changed

+131
-49
lines changed

build.gradle

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ buildscript {
1717

1818
dependencies {
1919
classpath 'gradle.plugin.com.github.johnrengelman:shadow:7.1.2'
20-
classpath 'org.mangorage:MangoBotGradle:6.0.17'
20+
classpath 'org.mangorage:MangoBotGradle:6.0.22'
2121
}
2222
}
2323

@@ -74,9 +74,10 @@ dependencies {
7474
testImplementation 'org.junit.jupiter:junit-jupiter'
7575

7676
installer('org.mangorage:installer:4.0.26')
77-
bootstrap("org.mangorage:mangobotbootstrap:1.0.72")
77+
bootstrap("org.mangorage:mangobotbootstrap:1.0.84")
78+
launchtarget("org.mangorage:mangobotlaunchtarget:0.1.8")
7879

79-
plugin('org.mangorage:mangobot:12.0.81')
80+
plugin('org.mangorage:mangobot:12.0.89')
8081

8182
library('org.slf4j:slf4j-simple:2.0.13') // Use a recent version)
8283
library('org.luaj:luaj-jme:3.0.1')

src/main/java/org/mangorage/mangobotplugin/BotEventListener.java

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,45 @@
11
package org.mangorage.mangobotplugin;
22

3+
import net.dv8tion.jda.api.entities.Message;
34
import net.dv8tion.jda.api.events.interaction.ModalInteractionEvent;
45
import net.dv8tion.jda.api.events.interaction.command.CommandAutoCompleteInteractionEvent;
56
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
67
import net.dv8tion.jda.api.events.interaction.component.ButtonInteractionEvent;
78
import net.dv8tion.jda.api.events.message.MessageReceivedEvent;
89
import net.dv8tion.jda.api.events.message.react.MessageReactionAddEvent;
910
import net.dv8tion.jda.api.hooks.SubscribeEvent;
11+
import org.mangorage.mangobotcore.api.command.v1.ICommandContext;
12+
import org.mangorage.mangobotcore.api.jda.command.v1.CommandResult;
13+
import org.mangorage.mangobotcore.api.jda.event.v1.CommandEvent;
1014
import org.mangorage.mangobotcore.api.jda.event.v1.DiscordButtonInteractEvent;
1115
import org.mangorage.mangobotcore.api.jda.event.v1.DiscordMessageReactionAddEvent;
1216
import org.mangorage.mangobotcore.api.jda.event.v1.DiscordMessageReceivedEvent;
1317
import org.mangorage.mangobotcore.api.jda.event.v1.DiscordModalInteractionEvent;
18+
import org.mangorage.mangobotcore.api.plugin.MangoBotCore;
1419
import org.mangorage.mangobotcore.api.util.jda.slash.command.watcher.WatcherManager;
20+
import org.mangorage.mangobotcore.api.util.misc.Arguments;
21+
import org.mangorage.mangobotcore.api.util.misc.TaskScheduler;
1522
import org.mangorage.mangobotplugin.entrypoint.MangoBot;
1623

24+
import java.util.Arrays;
25+
import java.util.concurrent.TimeUnit;
26+
1727
public final class BotEventListener {
28+
private record JDAMessageContext(Message message) implements ICommandContext {
29+
30+
@Override
31+
public <T> T get(Class<T> tClass) {
32+
if (tClass == Message.class) return (T) message;
33+
return null;
34+
}
35+
36+
@Override
37+
public boolean hasType(Class<?> tClass) {
38+
return tClass == Message.class;
39+
}
40+
}
41+
42+
1843
private final MangoBot mangoBot;
1944

2045
public BotEventListener(MangoBot mangoBot) {
@@ -45,7 +70,52 @@ public void onModalInteract(ButtonInteractionEvent event) {
4570
@SubscribeEvent
4671
public void onMessageReceived(MessageReceivedEvent event) {
4772
DiscordMessageReceivedEvent.BUS.post(new DiscordMessageReceivedEvent(event));
48-
mangoBot.getCommandManager().handle(event.getMessage());
73+
74+
// Command Logic
75+
final var message = event.getMessage();
76+
final var rawMessage = message.getContentRaw();
77+
final var cmdPrefix = MangoBotCore.isDevMode() ? "dev!" : "!";
78+
final var silentPrefix = "s" + cmdPrefix;
79+
final var isSilent = rawMessage.startsWith(silentPrefix);
80+
81+
82+
if (isSilent || rawMessage.startsWith(cmdPrefix)) {
83+
final var dispatcher = mangoBot.getCommandDispatcher();
84+
final var result = dispatcher.execute(
85+
isSilent ?
86+
rawMessage.replaceFirst(silentPrefix, "") : rawMessage.replaceFirst(cmdPrefix, ""),
87+
new JDAMessageContext(message)
88+
);
89+
90+
if (result != CommandResult.INVALID_COMMAND) {
91+
if (result != null) {
92+
if (result instanceof CommandResult finalResult)
93+
finalResult.accept(message);
94+
}
95+
96+
if (isSilent)
97+
TaskScheduler.getExecutor().schedule(() -> {
98+
message.delete().queue();
99+
}, 250, TimeUnit.MILLISECONDS);
100+
} else {
101+
mangoBot.getCommandManager().handle(event.getMessage());
102+
103+
// TODO: Undo When we remove the old cmd manager!
104+
// String[] command_pre = rawMessage.split(" ");
105+
// Arguments arguments = Arguments.of(Arrays.copyOfRange(command_pre, 1, command_pre.length));
106+
//
107+
// var cmd = rawMessage.replaceFirst(cmdPrefix, "").split(" ");
108+
//
109+
// final var cmdEvent = CommandEvent.BUS.fire(new CommandEvent(message, cmd[0], arguments));
110+
// if (cmdEvent.isHandled())
111+
// cmdEvent.getResult().accept(message);
112+
}
113+
114+
// TODO: Move isSilent check to here, to delete the cmd that was "attempted"
115+
}
116+
117+
118+
49119
}
50120

51121
@SubscribeEvent
Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,26 @@
11
package org.mangorage.mangobotplugin.commands;
22

33
import net.dv8tion.jda.api.entities.Message;
4+
import org.mangorage.mangobotcore.api.command.v1.ICommandDispatcher;
5+
import org.mangorage.mangobotcore.api.command.v1.ICommandNode;
46
import org.mangorage.mangobotcore.api.jda.command.v1.CommandResult;
57
import org.mangorage.mangobotcore.api.jda.command.v1.ICommand;
68
import org.mangorage.mangobotcore.api.util.misc.Arguments;
79

810
import java.util.List;
911

10-
public class PingCommand implements ICommand {
11-
@Override
12-
public String id() {
13-
return "ping";
14-
}
15-
16-
@Override
17-
public List<String> commands() {
18-
return List.of("ping");
19-
}
20-
21-
@Override
22-
public String usage() {
23-
return "";
24-
}
12+
public final class PingCommand {
2513

26-
@Override
27-
public CommandResult execute(Message message, Arguments arguments) {
28-
message.reply("Pong!").queue();;
29-
return CommandResult.PASS;
14+
public static void register(String id, ICommandDispatcher dispatcher) {
15+
dispatcher.register(
16+
ICommandNode.create(id)
17+
.requires(ctx -> ctx.hasType(Message.class))
18+
.usage("Checks if the discord bot is running!")
19+
.executes((ctx, args) -> {
20+
ctx.get(Message.class).reply("Pong!").queue();
21+
return CommandResult.PASS;
22+
})
23+
.build()
24+
);
3025
}
3126
}

src/main/java/org/mangorage/mangobotplugin/commands/PingsCommand.java

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.entities.Message;
55
import net.dv8tion.jda.api.entities.MessageEmbed;
6+
import org.mangorage.mangobotcore.api.command.v1.ICommandDispatcher;
7+
import org.mangorage.mangobotcore.api.command.v1.ICommandNode;
68
import org.mangorage.mangobotcore.api.jda.command.v1.CommandResult;
79
import org.mangorage.mangobotcore.api.jda.command.v1.ICommand;
810
import org.mangorage.mangobotcore.api.util.misc.Arguments;
911

1012
import java.awt.*;
1113
import java.util.List;
1214

13-
public final class PingsCommand implements ICommand {
15+
public final class PingsCommand {
1416
public static final MessageEmbed EMBED =
1517
new EmbedBuilder()
1618
.setTitle("Please disable pings when replying to others")
@@ -23,30 +25,21 @@ public final class PingsCommand implements ICommand {
2325
"""
2426
).build();
2527

26-
27-
@Override
28-
public String id() {
29-
return "pings";
30-
}
31-
32-
@Override
33-
public List<String> commands() {
34-
return List.of("pings");
35-
}
36-
37-
@Override
38-
public String usage() {
39-
return "Pings Usage: N/A";
40-
}
41-
42-
@Override
43-
public CommandResult execute(Message message, Arguments arguments) {
44-
var referenced = message.getReferencedMessage();
45-
if (referenced == null) {
46-
message.getChannel().sendMessageEmbeds(EMBED).queue();
47-
} else {
48-
referenced.replyEmbeds(EMBED).queue();
49-
}
50-
return CommandResult.PASS;
28+
public static void register(String id, ICommandDispatcher dispatcher) {
29+
dispatcher.register(
30+
ICommandNode.create(id)
31+
.requires(ctx -> ctx.hasType(Message.class))
32+
.executes((ctx, args) -> {
33+
final var message = ctx.get(Message.class);
34+
var referenced = message.getReferencedMessage();
35+
if (referenced == null) {
36+
message.getChannel().sendMessageEmbeds(EMBED).queue();
37+
} else {
38+
referenced.replyEmbeds(EMBED).queue();
39+
}
40+
return CommandResult.PASS;
41+
})
42+
.build()
43+
);
5144
}
5245
}

src/main/java/org/mangorage/mangobotplugin/entrypoint/MangoBot.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
import net.dv8tion.jda.api.JDABuilder;
55
import net.dv8tion.jda.api.OnlineStatus;
66
import net.dv8tion.jda.api.entities.Activity;
7+
import net.dv8tion.jda.api.entities.Message;
78
import net.dv8tion.jda.api.hooks.AnnotatedEventManager;
89
import net.dv8tion.jda.api.requests.GatewayIntent;
910
import net.dv8tion.jda.api.utils.MemberCachePolicy;
1011
import net.dv8tion.jda.api.utils.cache.CacheFlag;
12+
import org.mangorage.mangobotcore.api.command.v1.ICommandDispatcher;
13+
import org.mangorage.mangobotcore.api.command.v1.ICommandNode;
14+
import org.mangorage.mangobotcore.api.command.v1.ICommandNodeBuilder;
1115
import org.mangorage.mangobotcore.api.config.v1.ConfigTypes;
1216
import org.mangorage.mangobotcore.api.config.v1.IConfig;
1317
import org.mangorage.mangobotcore.api.config.v1.IConfigSetting;
1418
import org.mangorage.mangobotcore.api.jda.command.v1.CommandManager;
19+
import org.mangorage.mangobotcore.api.jda.command.v1.CommandResult;
1520
import org.mangorage.mangobotcore.api.plugin.v1.MangoBotPlugin;
1621
import org.mangorage.mangobotcore.api.plugin.v1.Plugin;
1722
import org.mangorage.mangobotcore.api.util.jda.ButtonActionRegistry;
@@ -77,6 +82,7 @@ public final class MangoBot implements Plugin {
7782
CacheFlag.FORUM_TAGS
7883
);
7984

85+
private final ICommandDispatcher commandDispatcher = ICommandDispatcher.create(CommandResult.INVALID_COMMAND);
8086
private final CommandManager commandManager = CommandManager.create();
8187
private final PagedListManager pagedListManager = new PagedListManager();
8288

@@ -85,6 +91,15 @@ public final class MangoBot implements Plugin {
8591
public MangoBot() {
8692
ACTION_REGISTRY.register(new TrashButtonAction());
8793

94+
commandDispatcher.register(
95+
ICommandNode.create("new-cmd")
96+
.executes((ctx, args) -> {
97+
ctx.get(Message.class).reply("Whelp it works now!").queue();
98+
return CommandResult.PASS;
99+
})
100+
.build()
101+
);
102+
88103
commandManager.register(new EmojiCommand());
89104
commandManager.register(new HomeDepotAlertCommand());
90105

@@ -137,10 +152,18 @@ public JDA getJDA() {
137152
return jda;
138153
}
139154

155+
/**
156+
* Use {@link #getCommandDispatcher()}
157+
*/
158+
@Deprecated(forRemoval = true)
140159
public CommandManager getCommandManager() {
141160
return commandManager;
142161
}
143162

163+
public ICommandDispatcher getCommandDispatcher() {
164+
return commandDispatcher;
165+
}
166+
144167
public PagedListManager getPagedListManager() {
145168
return pagedListManager;
146169
}

0 commit comments

Comments
 (0)