Skip to content

Commit 347fcaf

Browse files
committed
Add commandPreProcessFunction to CommandClient
1 parent 609c343 commit 347fcaf

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

command/src/main/java/com/jagrosh/jdautilities/command/CommandClientBuilder.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class CommandClientBuilder
4646
private String altprefix;
4747
private String[] prefixes;
4848
private Function<MessageReceivedEvent, String> prefixFunction;
49+
private Function<MessageReceivedEvent, Boolean> commandPreProcessFunction;
4950
private String serverInvite;
5051
private String success;
5152
private String warning;
@@ -73,7 +74,7 @@ public class CommandClientBuilder
7374
*/
7475
public CommandClient build()
7576
{
76-
CommandClient client = new CommandClientImpl(ownerId, coOwnerIds, prefix, altprefix, prefixes, prefixFunction, activity, status, serverInvite,
77+
CommandClient client = new CommandClientImpl(ownerId, coOwnerIds, prefix, altprefix, prefixes, prefixFunction, commandPreProcessFunction, activity, status, serverInvite,
7778
success, warning, error, carbonKey, botsKey, new ArrayList<>(commands), useHelp,
7879
shutdownAutomatically, helpConsumer, helpWord, executor, linkedCacheSize, compiler, manager);
7980
if(listener!=null)
@@ -171,7 +172,23 @@ public CommandClientBuilder setPrefixFunction(Function<MessageReceivedEvent, Str
171172
this.prefixFunction = prefixFunction;
172173
return this;
173174
}
174-
175+
176+
/**
177+
* Sets the pre-process function. This code is executed before every command.<br>
178+
* Returning "true" will allow processing to proceed.<br>
179+
* Returning "false" or "null" will prevent the Command from executing.
180+
*
181+
* @param commandPreProcessFunction
182+
* The function to execute
183+
*
184+
* @return This builder
185+
*/
186+
public CommandClientBuilder setCommandPreProcessFunction(Function<MessageReceivedEvent, Boolean> commandPreProcessFunction)
187+
{
188+
this.commandPreProcessFunction = commandPreProcessFunction;
189+
return this;
190+
}
191+
175192
/**
176193
* Sets whether the {@link com.jagrosh.jdautilities.command.CommandClient CommandClient} will use
177194
* the builder to automatically create a help command or not.

command/src/main/java/com/jagrosh/jdautilities/command/impl/CommandClientImpl.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ public class CommandClientImpl implements CommandClient, EventListener
7575
private final String altprefix;
7676
private final String[] prefixes;
7777
private final Function<MessageReceivedEvent, String> prefixFunction;
78+
private final Function<MessageReceivedEvent, Boolean> commandPreProcessFunction;
7879
private final String serverInvite;
7980
private final HashMap<String, Integer> commandIndex;
8081
private final ArrayList<Command> commands;
@@ -97,7 +98,7 @@ public class CommandClientImpl implements CommandClient, EventListener
9798
private CommandListener listener = null;
9899
private int totalGuilds;
99100

100-
public CommandClientImpl(String ownerId, String[] coOwnerIds, String prefix, String altprefix, String[] prefixes, Function<MessageReceivedEvent, String> prefixFunction, Activity activity, OnlineStatus status, String serverInvite,
101+
public CommandClientImpl(String ownerId, String[] coOwnerIds, String prefix, String altprefix, String[] prefixes, Function<MessageReceivedEvent, String> prefixFunction, Function<MessageReceivedEvent, Boolean> commandPreProcessFunction, Activity activity, OnlineStatus status, String serverInvite,
101102
String success, String warning, String error, String carbonKey, String botsKey, ArrayList<Command> commands,
102103
boolean useHelp, boolean shutdownAutomatically, Consumer<CommandEvent> helpConsumer, String helpWord, ScheduledExecutorService executor,
103104
int linkedCacheSize, AnnotatedModuleCompiler compiler, GuildSettingsManager manager)
@@ -124,6 +125,7 @@ public CommandClientImpl(String ownerId, String[] coOwnerIds, String prefix, Str
124125
this.altprefix = altprefix==null || altprefix.isEmpty() ? null : altprefix;
125126
this.prefixes = prefixes==null || prefixes.length == 0 ? null : prefixes;
126127
this.prefixFunction = prefixFunction;
128+
this.commandPreProcessFunction = commandPreProcessFunction==null ? event -> true : commandPreProcessFunction;
127129
this.textPrefix = prefix;
128130
this.activity = activity;
129131
this.status = status;
@@ -592,7 +594,10 @@ else if(event.isFromType(ChannelType.PRIVATE) || event.getTextChannel().canTalk(
592594
if(listener != null)
593595
listener.onCommand(cevent, command);
594596
uses.put(command.getName(), uses.getOrDefault(command.getName(), 0) + 1);
595-
command.run(cevent);
597+
if(commandPreProcessFunction.apply(event))
598+
{
599+
command.run(cevent);
600+
}
596601
return; // Command is done
597602
}
598603
}

0 commit comments

Comments
 (0)