Skip to content

Commit 4866526

Browse files
committed
Added help command for being able to see command usages.
1 parent be1df3d commit 4866526

8 files changed

Lines changed: 86 additions & 10 deletions

File tree

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ dependencies {
7777
bootstrap("org.mangorage:mangobotbootstrap:1.0.84")
7878
launchtarget("org.mangorage:mangobotlaunchtarget:0.1.8")
7979

80-
plugin('org.mangorage:mangobot:12.0.98')
80+
plugin('org.mangorage:mangobot:12.0.101')
8181

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

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,14 @@ public void onMessageReceived(MessageReceivedEvent event) {
7979
event.getMessage().reply(result.getMessage()).queue();
8080

8181
if (isSilent)
82-
TaskScheduler.getExecutor().schedule(() -> {
83-
message.delete().queue();
84-
}, 250, TimeUnit.MILLISECONDS);
82+
TaskScheduler.getExecutor().schedule(
83+
() -> {
84+
message.delete().queue();
85+
},
86+
250,
87+
TimeUnit.MILLISECONDS
88+
);
8589
} else {
86-
8790
String[] command_pre = rawMessage.split(" ");
8891
Arguments arguments = Arguments.of(Arrays.copyOfRange(command_pre, 1, command_pre.length));
8992

@@ -95,7 +98,12 @@ public void onMessageReceived(MessageReceivedEvent event) {
9598
if (msg != null)
9699
message.reply(msg).queue();
97100
}
101+
}
98102

103+
if (!cmdParseResult.getMessages().isEmpty()) {
104+
message.reply(
105+
String.join("\n", cmdParseResult.getMessages())
106+
).queue();
99107
}
100108
}
101109
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.mangorage.mangobotplugin.commands.misc;
2+
3+
import net.dv8tion.jda.api.entities.Message;
4+
import org.mangorage.mangobotcore.api.command.v1.CommandParseResult;
5+
import org.mangorage.mangobotcore.api.command.v1.ICommandDispatcher;
6+
import org.mangorage.mangobotcore.api.command.v1.argument.RequiredArg;
7+
import org.mangorage.mangobotcore.api.command.v1.argument.types.StringArgumentType;
8+
import org.mangorage.mangobotcore.api.jda.command.v2.AbstractJDACommand;
9+
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandResult;
10+
11+
public class HelpCommand extends AbstractJDACommand {
12+
private final ICommandDispatcher<Message, JDACommandResult> commandDispatcher;
13+
private final RequiredArg<String> commandArg;
14+
15+
public HelpCommand(String name, ICommandDispatcher<Message, JDACommandResult> commandDispatcher) {
16+
super(name);
17+
this.commandDispatcher = commandDispatcher;
18+
this.commandArg = registerRequiredArgument(
19+
"command",
20+
"The command to get help for",
21+
StringArgumentType.single()
22+
);
23+
}
24+
25+
@Override
26+
public JDACommandResult run(Message message, String[] arguments, CommandParseResult commandParseResult) throws Throwable {
27+
final String commandName = commandArg.get(arguments, commandParseResult);
28+
var command = commandDispatcher.getCommand(commandName);
29+
if (command == null) {
30+
message.reply("Command `" + commandName + "` not found.").queue();
31+
} else {
32+
message.reply(
33+
String.join("\n", command.buildUsage())
34+
).queue();
35+
}
36+
return JDACommandResult.PASS;
37+
}
38+
}

src/main/java/org/mangorage/mangobotplugin/commands/PingCommand.java renamed to src/main/java/org/mangorage/mangobotplugin/commands/misc/PingCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.mangorage.mangobotplugin.commands;
1+
package org.mangorage.mangobotplugin.commands.misc;
22

33
import net.dv8tion.jda.api.entities.Message;
44
import org.mangorage.mangobotcore.api.command.v1.CommandParseResult;

src/main/java/org/mangorage/mangobotplugin/commands/PingsCommand.java renamed to src/main/java/org/mangorage/mangobotplugin/commands/misc/PingsCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.mangorage.mangobotplugin.commands;
1+
package org.mangorage.mangobotplugin.commands.misc;
22

33
import net.dv8tion.jda.api.EmbedBuilder;
44
import net.dv8tion.jda.api.entities.Message;

src/main/java/org/mangorage/mangobotplugin/commands/trick/TrickManager.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ public final class TrickManager {
1414
.build(Trick.class);
1515

1616
private final Map<TrickKey, Trick> loadedTricks = new HashMap<>();
17+
private final MangoBot plugin;
1718

1819
public TrickManager(MangoBot plugin) {
20+
this.plugin = plugin;
21+
1922
// Load tricks from data handler
2023
TRICKS_DATA_HANDLER.load(plugin.getPluginDirectory()).forEach(data -> {
2124
loadedTricks.put(new TrickKey(data.getTrickID(), data.getGuildID()), data);
@@ -39,4 +42,14 @@ public Trick getTrickForGuildByName(long guildId, String name) {
3942
return loadedTricks.get(new TrickKey(name, guildId));
4043
}
4144

45+
public boolean removeTrick(String trickID, long guildID) {
46+
TrickKey key = new TrickKey(trickID, guildID);
47+
Trick removed = loadedTricks.remove(key);
48+
if (removed != null) {
49+
TRICKS_DATA_HANDLER.delete(plugin.getPluginDirectory(), removed);
50+
return true;
51+
}
52+
return false;
53+
}
54+
4255
}

src/main/java/org/mangorage/mangobotplugin/commands/trick/impl/TrickRemoveSubCommand.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,36 @@
22

33
import net.dv8tion.jda.api.entities.Message;
44
import org.mangorage.mangobotcore.api.command.v1.CommandParseResult;
5+
import org.mangorage.mangobotcore.api.command.v1.argument.RequiredArg;
6+
import org.mangorage.mangobotcore.api.command.v1.argument.types.StringArgumentType;
57
import org.mangorage.mangobotcore.api.jda.command.v2.AbstractJDACommand;
68
import org.mangorage.mangobotcore.api.jda.command.v2.JDACommandResult;
79
import org.mangorage.mangobotplugin.commands.trick.TrickManager;
810

911
public final class TrickRemoveSubCommand extends AbstractJDACommand {
12+
13+
1014
private final TrickManager trickManager;
15+
private final RequiredArg<String> trickArg;
1116

1217
public TrickRemoveSubCommand(String name, TrickManager trickManager) {
1318
super(name);
1419
this.trickManager = trickManager;
20+
this.trickArg = registerRequiredArgument(
21+
"trick_id",
22+
"The Trick ID",
23+
StringArgumentType.single()
24+
);
1525
}
1626

1727
@Override
1828
public JDACommandResult run(Message context, String[] arguments, CommandParseResult commandParseResult) throws Throwable {
19-
context.reply("Removing tricks is under maintenance!").queue();
29+
final var trickName = trickArg.get(arguments, commandParseResult);
30+
if (trickManager.removeTrick(trickName, context.getGuildIdLong())) {
31+
context.reply("Successfully removed trick: " + trickName).queue();
32+
} else {
33+
context.reply("No trick found with ID: " + trickName).queue();
34+
}
2035
return JDACommandResult.PASS;
2136
}
2237
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@
2121
import org.mangorage.mangobotcore.api.util.jda.slash.command.Command;
2222
import org.mangorage.mangobotplugin.BotEventListener;
2323
import org.mangorage.mangobotplugin.commands.internal.homedepot.HomeDepotCommand;
24+
import org.mangorage.mangobotplugin.commands.misc.HelpCommand;
2425
import org.mangorage.mangobotplugin.commands.trick.TrickManager;
2526
import org.mangorage.mangobotplugin.commands.trick.impl.TrickCommand;
2627
import org.mangorage.mangobotplugin.pagedlist.PagedListManager;
2728
import org.mangorage.mangobotplugin.actions.TrashButtonAction;
28-
import org.mangorage.mangobotplugin.commands.PingCommand;
29-
import org.mangorage.mangobotplugin.commands.PingsCommand;
29+
import org.mangorage.mangobotplugin.commands.misc.PingCommand;
30+
import org.mangorage.mangobotplugin.commands.misc.PingsCommand;
3031

3132
import java.nio.file.Path;
3233
import java.util.EnumSet;
@@ -81,6 +82,7 @@ public final class MangoBot implements Plugin {
8182
public MangoBot() {
8283
ACTION_REGISTRY.register(new TrashButtonAction());
8384

85+
commandDispatcher.register(new HelpCommand("help", getCommandDispatcher()));
8486
commandDispatcher.register(new PingCommand("ping"));
8587
commandDispatcher.register(new PingsCommand("pings"));;
8688
commandDispatcher.register(new HomeDepotCommand("homedepot"));

0 commit comments

Comments
 (0)