Skip to content
This repository was archived by the owner on Oct 21, 2022. It is now read-only.

Commit a15d7db

Browse files
author
Lucas Malandrino
authored
Merge pull request #2 from JesusCrie/dev
Command module available
2 parents 12eb86d + df66e19 commit a15d7db

File tree

17 files changed

+357
-139
lines changed

17 files changed

+357
-139
lines changed

ModularBot-Command/src/main/java/com/jesus_crie/modularbot_command/AccessLevel.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public class AccessLevel {
5757
* @param requiredPermissions The permissions required if in a guild.
5858
* @param onlyGuild Flag that indicate that this can only occur in a guild.
5959
* @param onlyPrivate Flag that indicate that this can only occur in a private channel.
60-
* @param user Flag that indicate that only this user can use this, <bold>overwrite every other flag</bold>.
60+
* @param user Flag that indicate that only this user can use this, /!\ overwrite every other flag /!\.
6161
*/
6262
public AccessLevel(@Nonnull EnumSet<Permission> requiredPermissions, boolean onlyGuild, boolean onlyPrivate, long user) {
6363
this.requiredPermissions = requiredPermissions;

ModularBot-Command/src/main/java/com/jesus_crie/modularbot_command/Command.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,13 @@ private void registerCommandPatterns() {
135135

136136
} else if (params[1].isAssignableFrom(Options.class)) {
137137
// CommandEvent, Options
138-
patterns.add(new CommandPattern((event, args, options) -> invokeMethod(method, options)));
138+
patterns.add(new CommandPattern((event, args, options) -> invokeMethod(method, event, options)));
139139

140140
} else {
141141
// CommandEvent, Arg1
142142
patterns.add(new CommandPattern(
143143
translateArguments(method, 1),
144-
(event, args, options) -> invokeMethod(method, args.get(0))
144+
(event, args, options) -> invokeMethod(method, event, args.get(0))
145145
));
146146
}
147147

ModularBot-Command/src/main/java/com/jesus_crie/modularbot_command/CommandEvent.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,31 @@
88

99
public class CommandEvent extends MessageReceivedEvent {
1010

11-
private Command command;
11+
private final CommandModule module;
12+
private final Command command;
1213

13-
public CommandEvent(@Nonnull MessageReceivedEvent baseEvent, Command command) {
14-
this(baseEvent.getJDA(), baseEvent.getResponseNumber(), baseEvent.getMessage(), command);
14+
public CommandEvent(@Nonnull final MessageReceivedEvent baseEvent, @Nonnull final CommandModule module,
15+
@Nonnull final Command command) {
16+
this(baseEvent.getJDA(), baseEvent.getResponseNumber(), baseEvent.getMessage(), module, command);
1517
}
1618

17-
public CommandEvent(JDA api, long responseNumber, Message message, Command command) {
19+
public CommandEvent(@Nonnull final JDA api, final long responseNumber, @Nonnull final Message message,
20+
@Nonnull final CommandModule module, @Nonnull final Command command) {
1821
super(api, responseNumber, message);
22+
this.module = module;
1923
this.command = command;
2024
}
2125

26+
public void fastReply(@Nonnull final String message) {
27+
channel.sendMessage(message).complete();
28+
}
29+
30+
@Nonnull
31+
public CommandModule getModule() {
32+
return module;
33+
}
34+
35+
@Nonnull
2236
public Command getCommand() {
2337
return command;
2438
}

ModularBot-Command/src/main/java/com/jesus_crie/modularbot_command/CommandModule.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import com.jesus_crie.modularbot.ModularBotBuilder;
44
import com.jesus_crie.modularbot.module.BaseModule;
55
import com.jesus_crie.modularbot_command.listener.CommandListener;
6+
import com.jesus_crie.modularbot_command.listener.DiscordCommandListener;
7+
import com.jesus_crie.modularbot_command.listener.NopCommandListener;
68
import com.jesus_crie.modularbot_command.processing.CommandProcessor;
79

810
import javax.annotation.Nonnull;
@@ -24,13 +26,15 @@ public class CommandModule extends BaseModule {
2426
// Command processor
2527
private CommandProcessor processor = new CommandProcessor();
2628

29+
private CommandListener listener = new NopCommandListener();
30+
2731
public CommandModule() {
2832
super(INFO);
2933
}
3034

3135
@Override
3236
public void onLoad(@Nonnull ModularBotBuilder builder) {
33-
builder.addListeners(new CommandListener(this));
37+
builder.addListeners(new DiscordCommandListener(this));
3438
}
3539

3640
public void registerCommands(@Nonnull Command... commands) {
@@ -61,4 +65,13 @@ public Command getCommand(@Nonnull String name) {
6165
.findAny()
6266
.orElse(null);
6367
}
68+
69+
public void setListener(@Nonnull final CommandListener listener) {
70+
this.listener = listener;
71+
}
72+
73+
@Nonnull
74+
public CommandListener getListener() {
75+
return listener;
76+
}
6477
}

ModularBot-Command/src/main/java/com/jesus_crie/modularbot_command/annotations/RegisterPattern.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Use on a method inside a class that extends {@link Command Command} to associate
1212
* a pattern with it.
1313
* The method should be protected or higher and should take 3 arguments in that order:
14-
* {@link CommandEvent CommandEvent}, {@link java.util.List List<Object>}, {@link Options Options}.
14+
* {@link CommandEvent CommandEvent}, {@link java.util.List List}, {@link Options Options}.
1515
* The return type doesn't matter.
1616
*/
1717
@Target(ElementType.METHOD)
Lines changed: 70 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,90 @@
11
package com.jesus_crie.modularbot_command.listener;
22

3-
import com.jesus_crie.modularbot_command.Command;
43
import com.jesus_crie.modularbot_command.CommandEvent;
5-
import com.jesus_crie.modularbot_command.CommandModule;
64
import com.jesus_crie.modularbot_command.exception.CommandProcessingException;
5+
import com.jesus_crie.modularbot_command.exception.UnknownOptionException;
76
import com.jesus_crie.modularbot_command.processing.Options;
8-
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
9-
import net.dv8tion.jda.core.hooks.ListenerAdapter;
7+
import net.dv8tion.jda.core.entities.Message;
108
import net.dv8tion.jda.core.utils.tuple.Pair;
119

10+
import javax.annotation.Nonnull;
1211
import java.util.List;
1312
import java.util.Map;
1413

15-
public class CommandListener extends ListenerAdapter {
14+
public interface CommandListener {
1615

17-
private final CommandModule module;
16+
/**
17+
* Triggered every time a command is typed (= any message that starts with the prefix
18+
*/
19+
void onCommandReceived();
1820

19-
public CommandListener(CommandModule module) {
20-
this.module = module;
21-
}
21+
/**
22+
* Triggered when a command was typed and it was a real command.
23+
* But nothing has been executed yet and no checks have been performed.
24+
*
25+
* @param command The actual event.
26+
*/
27+
void onCommandFound(@Nonnull final CommandEvent command);
2228

23-
@Override
24-
public void onMessageReceived(MessageReceivedEvent event) {
25-
if (event.getAuthor().getIdLong() == event.getJDA().getSelfUser().getIdLong())
26-
return;
29+
/**
30+
* Triggered when a message starts with the prefix but it was a false alert, the command doesn't exist.
31+
* An error 404 in a manner.
32+
*
33+
* @param name The name of the command that was typed.
34+
* @param message The message containing the wrong command.
35+
*/
36+
void onCommandNotFound(@Nonnull final String name, @Nonnull final Message message);
2737

28-
final String prefix = module.getPrefixForGuild(event.getGuild().getIdLong());
29-
if (!event.getMessage().getContentRaw().startsWith(prefix))
30-
return;
38+
/**
39+
* Triggered when a command is found but the user doesn't satisfy the {@link com.jesus_crie.modularbot_command.AccessLevel AccessLevel}
40+
* associated with this command.
41+
*
42+
* @param event The event that was fired.
43+
*/
44+
void onTooLowAccessLevel(@Nonnull final CommandEvent event);
3145

32-
final String[] parts = event.getMessage().getContentRaw().split(" ", 2);
33-
final String name = parts[0].substring(prefix.length());
34-
final Command command = module.getCommand(name);
46+
/**
47+
* Triggered when a command is found, the user have the correct privileges and what he just typed has been
48+
* successfully parsed.
49+
*
50+
* @param event The event that was fired.
51+
* @param processedContent The output of the command processor.
52+
* @see com.jesus_crie.modularbot_command.processing.CommandProcessor
53+
* @see com.jesus_crie.modularbot_command.processing.CommandProcessor#process(String)
54+
*/
55+
void onCommandSuccessfullyProcessed(@Nonnull final CommandEvent event, @Nonnull final Pair<List<String>, Map<String, String>> processedContent);
3556

36-
if (command == null) {
37-
// TODO 10/06/2018 notify not found
38-
return;
39-
}
57+
/**
58+
* Triggered when the processing of the command has failed, usually a syntax error.
59+
*
60+
* @param event The event that was fired.
61+
* @param error The error from the command processor.
62+
* @see com.jesus_crie.modularbot_command.processing.CommandProcessor
63+
*/
64+
void onCommandFailedProcessing(@Nonnull final CommandEvent event, @Nonnull final CommandProcessingException error);
4065

41-
final CommandEvent cmdEvent = new CommandEvent(event, command);
66+
/**
67+
* Triggered when the command was successfully processed but one of the option is not registered in the command.
68+
*
69+
* @param event The event that was fired.
70+
* @param error The error from the {@link com.jesus_crie.modularbot_command.processing.Options}.
71+
* @see com.jesus_crie.modularbot_command.processing.Options
72+
*/
73+
void onCommandFailedUnknownOption(@Nonnull final CommandEvent event, @Nonnull final UnknownOptionException error);
4274

43-
if (!command.getAccessLevel().check(cmdEvent)) {
44-
// TODO 10/06/2018 notify too low access
45-
return;
46-
}
75+
/**
76+
* Triggered when the command failed to execute because the provided arguments doesn't match any registered pattern.
77+
*
78+
* @param event The actual event.
79+
* @param options The provided options.
80+
* @param arguments The provided arguments.
81+
*/
82+
void onCommandFailedNoPatternMatch(@Nonnull final CommandEvent event, @Nonnull final Options options, @Nonnull final List<String> arguments);
4783

48-
try {
49-
final Pair<List<String>, Map<String, String>> processedContent = module.getCommandProcessor().process(event.getMessage().getContentRaw());
50-
51-
// TODO 13/06/2018 notify command processed successfully
52-
53-
final Options options = new Options(module, command, processedContent.getRight());
54-
if (!command.execute(module, cmdEvent, options, processedContent.getLeft())) {
55-
// TODO 13/06/2018 notify not pattern found
56-
}
57-
58-
} catch (CommandProcessingException e) {
59-
// TODO 11/06/2018 notify error processing command
60-
}
61-
}
84+
/**
85+
* Triggered when the command is successfully executed.
86+
*
87+
* @param event The event that triggered the command in the first place.
88+
*/
89+
void onCommandSuccess(@Nonnull final CommandEvent event);
6290
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.jesus_crie.modularbot_command.listener;
2+
3+
import com.jesus_crie.modularbot_command.Command;
4+
import com.jesus_crie.modularbot_command.CommandEvent;
5+
import com.jesus_crie.modularbot_command.CommandModule;
6+
import com.jesus_crie.modularbot_command.exception.CommandProcessingException;
7+
import com.jesus_crie.modularbot_command.exception.UnknownOptionException;
8+
import com.jesus_crie.modularbot_command.processing.Options;
9+
import net.dv8tion.jda.core.events.message.MessageReceivedEvent;
10+
import net.dv8tion.jda.core.hooks.ListenerAdapter;
11+
import net.dv8tion.jda.core.utils.tuple.Pair;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import java.util.List;
16+
import java.util.Map;
17+
18+
public class DiscordCommandListener extends ListenerAdapter {
19+
20+
private static final Logger LOG = LoggerFactory.getLogger("DiscordCommandListener");
21+
22+
private final CommandModule module;
23+
24+
public DiscordCommandListener(CommandModule module) {
25+
this.module = module;
26+
}
27+
28+
@Override
29+
public void onMessageReceived(MessageReceivedEvent event) {
30+
if (event.getAuthor().getIdLong() == event.getJDA().getSelfUser().getIdLong())
31+
return;
32+
33+
final String prefix = module.getPrefixForGuild(event.getGuild().getIdLong());
34+
if (!event.getMessage().getContentRaw().startsWith(prefix))
35+
return;
36+
37+
final String[] parts = event.getMessage().getContentRaw().split(" ", 2);
38+
final String name = parts[0].substring(prefix.length());
39+
final Command command = module.getCommand(name);
40+
41+
if (command == null) {
42+
module.getListener().onCommandNotFound(name, event.getMessage());
43+
return;
44+
}
45+
46+
final CommandEvent cmdEvent = new CommandEvent(event, module, command);
47+
48+
if (!command.getAccessLevel().check(cmdEvent)) {
49+
module.getListener().onTooLowAccessLevel(cmdEvent);
50+
return;
51+
}
52+
53+
try {
54+
final String fullArgs;
55+
if (parts.length == 2) fullArgs = parts[1];
56+
else fullArgs = "";
57+
58+
final Pair<List<String>, Map<String, String>> processedContent = module.getCommandProcessor().process(fullArgs);
59+
60+
module.getListener().onCommandSuccessfullyProcessed(cmdEvent, processedContent);
61+
62+
final Options options = new Options(module, command, processedContent.getRight());
63+
if (!command.execute(module, cmdEvent, options, processedContent.getLeft()))
64+
module.getListener().onCommandFailedNoPatternMatch(cmdEvent, options, processedContent.getLeft());
65+
66+
} catch (CommandProcessingException e) {
67+
module.getListener().onCommandFailedProcessing(cmdEvent, e);
68+
} catch (UnknownOptionException e) {
69+
module.getListener().onCommandFailedUnknownOption(cmdEvent, e);
70+
}
71+
}
72+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.jesus_crie.modularbot_command.listener;
2+
3+
import com.jesus_crie.modularbot_command.CommandEvent;
4+
import com.jesus_crie.modularbot_command.exception.CommandProcessingException;
5+
import com.jesus_crie.modularbot_command.exception.UnknownOptionException;
6+
import com.jesus_crie.modularbot_command.processing.Options;
7+
import javafx.util.Pair;
8+
import net.dv8tion.jda.core.entities.Message;
9+
10+
import javax.annotation.Nonnull;
11+
import java.util.List;
12+
import java.util.Map;
13+
14+
public class NopCommandListener implements CommandListener {
15+
16+
@Override
17+
public void onCommandReceived() {
18+
/* no-op */
19+
}
20+
21+
@Override
22+
public void onCommandFound(@Nonnull CommandEvent command) {
23+
/* no-op */
24+
}
25+
26+
@Override
27+
public void onCommandNotFound(@Nonnull String name, @Nonnull Message message) {
28+
/* no-op */
29+
}
30+
31+
@Override
32+
public void onTooLowAccessLevel(@Nonnull CommandEvent event) {
33+
/* no-op */
34+
}
35+
36+
@Override
37+
public void onCommandSuccessfullyProcessed(@Nonnull CommandEvent event, @Nonnull Pair<List<String>, Map<String, String>> processedContent) {
38+
/* no-op */
39+
}
40+
41+
@Override
42+
public void onCommandFailedProcessing(@Nonnull CommandEvent event, @Nonnull CommandProcessingException error) {
43+
/* no-op */
44+
}
45+
46+
@Override
47+
public void onCommandFailedUnknownOption(@Nonnull CommandEvent event, @Nonnull UnknownOptionException error) {
48+
/* no-op */
49+
}
50+
51+
@Override
52+
public void onCommandFailedNoPatternMatch(@Nonnull CommandEvent event, @Nonnull Options options, @Nonnull List<String> arguments) {
53+
/* no-op */
54+
}
55+
56+
@Override
57+
public void onCommandSuccess(@Nonnull CommandEvent event) {
58+
/* no-op */
59+
}
60+
}

ModularBot-Command/src/main/java/com/jesus_crie/modularbot_command/processing/CommandPattern.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ private Argument getLastArgument() {
6868
public boolean hasArguments() {
6969
return !arguments.isEmpty();
7070
}
71+
72+
@Override
73+
public String toString() {
74+
return "CommandPattern{ " + arguments + " }";
75+
}
7176
}

0 commit comments

Comments
 (0)