Skip to content
This repository was archived by the owner on Jun 18, 2020. It is now read-only.

Commit a279b89

Browse files
committed
Added docs
1 parent 290a58c commit a279b89

File tree

11 files changed

+176
-16
lines changed

11 files changed

+176
-16
lines changed

src/main/java/com/darichey/discord/Command.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66
import java.util.Set;
77
import java.util.function.Consumer;
88

9+
/**
10+
* A command to be executed in Discord. This holds a set of {@link Limiter}s that determine if the command can be executed in a given context as well
11+
* as a function to be called should all of the limiters pass.
12+
*/
913
@SuppressWarnings("WeakerAccess")
1014
public class Command {
1115

@@ -17,38 +21,72 @@ private Command(Consumer<CommandContext> onCalled, Set<Limiter> limiters) {
1721
this.limiters = limiters;
1822
}
1923

24+
/**
25+
* Gets the function that is executed when the command is called.
26+
* @return The function that is executed when the command is called.
27+
*/
2028
public Consumer<CommandContext> getOnCalled() {
2129
return onCalled;
2230
}
2331

32+
/**
33+
* Gets the limiters which determine if the command can be executed in a given context.
34+
* @return The limiters which determine if the command can be executed in a given context.
35+
*/
2436
public Set<Limiter> getLimiters() {
2537
return limiters;
2638
}
2739

40+
/**
41+
* Gets a builder that can be used to build a new Command.
42+
* @return A new command builder.
43+
*/
2844
public static Builder builder() {
2945
return new Builder();
3046
}
3147

48+
/**
49+
* Used to build Command instances.
50+
*/
3251
public static class Builder {
3352

3453
private Consumer<CommandContext> onCalled;
3554
private Set<Limiter> limiters = new HashSet<>();
3655

56+
/**
57+
* Sets the function that is executed when the command is called.
58+
* @param onCalled The function that is executed when the command is called.
59+
* @return The builder instance.
60+
*/
3761
public Builder onCalled(Consumer<CommandContext> onCalled) {
3862
this.onCalled = onCalled;
3963
return this;
4064
}
4165

66+
/**
67+
* Adds a limiter to the set of limiters for the command.
68+
* @param limiter The limiter to add.
69+
* @return The builder instance.
70+
*/
4271
public Builder limiter(Limiter limiter) {
4372
this.limiters.add(limiter);
4473
return this;
4574
}
4675

76+
/**
77+
* Sets all of the limiters that determine if the command can be executed in a given context.
78+
* @param limiters The limiters to set.
79+
* @return The builder instance.
80+
*/
4781
public Builder limiters(Set<Limiter> limiters) {
4882
this.limiters = limiters;
4983
return this;
5084
}
5185

86+
/**
87+
* Builds the command instance.
88+
* @return The command instance.
89+
*/
5290
public Command build() {
5391
return new Command(onCalled, limiters);
5492
}

src/main/java/com/darichey/discord/CommandContext.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@
1010
import java.util.Arrays;
1111
import java.util.List;
1212

13+
/**
14+
* Provides information about the context in which a command was executed.
15+
*/
1316
public class CommandContext {
17+
1418
private final IDiscordClient client;
1519
private final String name;
1620
private final List<String> args;
@@ -19,7 +23,7 @@ public class CommandContext {
1923
private final IChannel channel;
2024
private final IUser author;
2125

22-
public CommandContext(MessageReceivedEvent event, String prefix, String name, String args) {
26+
CommandContext(MessageReceivedEvent event, String name, String args) {
2327
this.client = event.getClient();
2428
this.name = name;
2529
this.args = Arrays.asList(args.split("\\s+"));
@@ -29,30 +33,58 @@ public CommandContext(MessageReceivedEvent event, String prefix, String name, St
2933
this.author = event.getAuthor();
3034
}
3135

36+
/**
37+
* Gets the client that received the message this context was built from.
38+
* @return The client that received the message this context was built from.
39+
*/
3240
public IDiscordClient getClient() {
3341
return client;
3442
}
3543

44+
/**
45+
* Gets the name of the command that was executed.
46+
* @return The name of the command that was executed.
47+
*/
3648
public String getName() {
3749
return name;
3850
}
3951

52+
/**
53+
* Gets the arguments of the command that was executed.
54+
* @return The arguments of the command that was executed.
55+
*/
4056
public List<String> getArgs() {
4157
return args;
4258
}
4359

60+
/**
61+
* Gets the message that this context was built from.
62+
* @return The message that this context was built from.
63+
*/
4464
public IMessage getMessage() {
4565
return message;
4666
}
4767

68+
/**
69+
* Gets the guild in which the message that this context was built from was received.
70+
* @return The guild in which the message that this context was built from was received.
71+
*/
4872
public IGuild getGuild() {
4973
return guild;
5074
}
5175

76+
/**
77+
* Gets the channel in which the message that this context was built from was received.
78+
* @return The channel in which the message that this context was built from was received.
79+
*/
5280
public IChannel getChannel() {
5381
return channel;
5482
}
5583

84+
/**
85+
* Gets the author of the message that this context was built from.
86+
* @return The author of the message that this context was built from.
87+
*/
5688
public IUser getAuthor() {
5789
return author;
5890
}

src/main/java/com/darichey/discord/CommandListener.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import sx.blah.discord.api.events.IListener;
44
import sx.blah.discord.handle.impl.events.guild.channel.message.MessageReceivedEvent;
55

6+
/**
7+
* Listens for messages which start with the prefix specified by a {@link CommandRegistry} and attempts to parse and call the correct command.
8+
*/
9+
@SuppressWarnings("WeakerAccess")
610
public class CommandListener implements IListener<MessageReceivedEvent> {
711

812
private final CommandRegistry registry;
@@ -26,7 +30,7 @@ public void handle(MessageReceivedEvent event) {
2630

2731
String args = name.length() == prefixRemoved.length() ? "" : prefixRemoved.substring(name.length() + 1);
2832

29-
registry.call(name, new CommandContext(event, prefix, name, args));
33+
registry.call(name, new CommandContext(event, name, args));
3034
}
3135
}
3236
}

src/main/java/com/darichey/discord/CommandRegistry.java

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77
import java.util.*;
88

9+
/**
10+
* The point of access for calling commands. This can hold default limiters which are automatically applied to all commands as well as guild-specific
11+
* prefixes.
12+
*/
913
@SuppressWarnings("WeakerAccess")
1014
public class CommandRegistry {
1115

@@ -29,62 +33,109 @@ public CommandRegistry(String defaultPrefix, Limiter... defaultLimiters) {
2933
Collections.addAll(this.defaultLimiters, defaultLimiters);
3034
}
3135

36+
/**
37+
* Calls a command with the given name, if it exists.
38+
* @param name The name of the command to call.
39+
* @param ctx The context to give to the command function.
40+
*/
3241
public void call(String name, CommandContext ctx) {
33-
getCommand(name).ifPresent(cmd -> {
34-
for (Limiter limiter : defaultLimiters) {
35-
if (!limiter.check(ctx)) {
36-
limiter.onFail(ctx);
37-
return;
38-
}
42+
getCommand(name).ifPresent(cmd -> call(cmd, ctx));
43+
}
44+
45+
private void call(Command cmd, CommandContext ctx) {
46+
for (Limiter limiter : defaultLimiters) {
47+
if (!limiter.check(ctx)) {
48+
limiter.onFail(ctx);
49+
return;
3950
}
51+
}
4052

41-
for (Limiter limiter : cmd.getLimiters()) {
42-
if (!limiter.check(ctx)) {
43-
limiter.onFail(ctx);
44-
return;
45-
}
53+
for (Limiter limiter : cmd.getLimiters()) {
54+
if (!limiter.check(ctx)) {
55+
limiter.onFail(ctx);
56+
return;
4657
}
58+
}
4759

48-
cmd.getOnCalled().accept(ctx);
49-
});
60+
cmd.getOnCalled().accept(ctx);
5061
}
5162

63+
/**
64+
* Associates a command to a name and optional aliases.
65+
* @param command The command to register.
66+
* @param name The name to associate with the command.
67+
* @param aliases The aliases to associate with the command. (Or none)
68+
*/
5269
public void register(Command command, String name, String... aliases) {
5370
register(command, name);
5471
for (String alias : aliases) {
5572
register(command, alias);
5673
}
5774
}
58-
5975
private void register(Command command, String name) {
6076
if (commands.containsKey(name)) throw new IllegalArgumentException("Attempt to register two commands with the same name: " + name);
6177
commands.put(name, command);
6278
}
6379

80+
/**
81+
* Optionally returns the command associated with the given name;
82+
* @param name The name to look up.
83+
* @return The command associated with the given name.
84+
*/
6485
public Optional<Command> getCommand(String name) {
6586
return Optional.ofNullable(commands.get(name));
6687
}
6788

89+
/**
90+
* Sets a guild-specific prefix.
91+
* @param guild The guild to set the prefix in.
92+
* @param prefix The prefix to set.
93+
*/
6894
public void overwritePrefix(IGuild guild, String prefix) {
6995
overwritePrefix(guild.getLongID(), prefix);
7096
}
7197

98+
/**
99+
* Sets a guild-specific prefix.
100+
* @param id The id of the guild to set the prefix in.
101+
* @param prefix The prefix to set.
102+
*/
72103
public void overwritePrefix(long id, String prefix) {
73104
guildPrefixes.put(id, prefix);
74105
}
75106

107+
/**
108+
* Optionally returns the guild-specific prefix for the given guild.
109+
* @param guild The guild to look up.
110+
* @return The guild-specific prefix for the given guild.
111+
*/
76112
public Optional<String> getPrefixForGuild(IGuild guild) {
77113
return getPrefixForGuild(guild.getLongID());
78114
}
79115

116+
/**
117+
* Optionally returns the guild-specific prefix for the given guild.
118+
* @param id The id of the guild to look up.
119+
* @return The guild-specific prefix for the given guild.
120+
*/
80121
public Optional<String> getPrefixForGuild(long id) {
81122
return Optional.ofNullable(guildPrefixes.get(id));
82123
}
83124

125+
/**
126+
* Gets the prefix for the given guild. This is either the guild-specific prefix or the default prefix if one is not set.
127+
* @param guild The guild to look up.
128+
* @return The guild-specific prefix for this guild or
129+
*/
84130
public String getEffectivePrefix(IGuild guild) {
85131
return getEffectivePrefix(guild.getLongID());
86132
}
87133

134+
/**
135+
* Gets the prefix for the given guild. This is either the guild-specific prefix or the default prefix if one is not set.
136+
* @param id The id of the guild to look up.
137+
* @return The guild-specific prefix for this guild or
138+
*/
88139
public String getEffectivePrefix(long id) {
89140
return getPrefixForGuild(id).orElse(defaultPrefix);
90141
}

src/main/java/com/darichey/discord/limiter/ChannelLimiter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.darichey.discord.CommandContext;
44
import sx.blah.discord.handle.obj.IChannel;
55

6+
/**
7+
* Limits execution of a command to a set of channels.
8+
*/
69
public class ChannelLimiter extends IDLimiter {
710

811
public ChannelLimiter(IChannel... channels) {

src/main/java/com/darichey/discord/limiter/GuildLimiter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.darichey.discord.CommandContext;
44
import sx.blah.discord.handle.obj.IGuild;
55

6+
/**
7+
* Limits execution of a command to a set of guilds.
8+
*/
69
public class GuildLimiter extends IDLimiter {
710

811
public GuildLimiter(IGuild... guilds) {

src/main/java/com/darichey/discord/limiter/IDLimiter.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66

77
import java.util.Arrays;
88

9+
/**
10+
* A limiter that limits execution to a set of IDs.
11+
*/
912
public abstract class IDLimiter implements Limiter {
1013

1114
private final long[] ids;
@@ -23,5 +26,10 @@ public boolean check(CommandContext ctx) {
2326
return ArrayUtils.contains(ids, getID(ctx));
2427
}
2528

29+
/**
30+
* Gets the ID to check for this limiter. If the ID returned by this method is not in {@link #ids}, the check will not pass.
31+
* @param ctx The context to get the ID from.
32+
* @return The ID to check for this limiter.
33+
*/
2634
public abstract long getID(CommandContext ctx);
2735
}

src/main/java/com/darichey/discord/limiter/Limiter.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,23 @@
22

33
import com.darichey.discord.CommandContext;
44

5+
/**
6+
* Used to limit the execution of a command to a certain context.
7+
*/
58
@FunctionalInterface
69
public interface Limiter {
710

11+
/**
12+
* Determines if the command can be executed in the given context.
13+
* @param ctx The context in which the command was executed.
14+
* @return True if the command can be executed.
15+
*/
816
boolean check(CommandContext ctx);
917

18+
/**
19+
* Executed by the {@link com.darichey.discord.CommandRegistry} if a command is called and {@link #check(CommandContext)} returns false.
20+
* @param ctx The context in which the limiter failed.
21+
*/
1022
default void onFail(CommandContext ctx) {
1123
// NO-OP
1224
}

src/main/java/com/darichey/discord/limiter/RoleLimiter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import java.util.Arrays;
99

10+
/**
11+
* Limits execution of a command to a user with <b>ALL</b> of the given roles.
12+
*/
1013
public class RoleLimiter implements Limiter {
1114

1215
private final long[] ids;

0 commit comments

Comments
 (0)