@@ -6,8 +6,9 @@ Command Framework
66[ ![ License] [ License Badge ]] [ License File ]
77[ ![ Discord] [ Discord Badge ]] [ Discord Invite ]
88
9- ![ Branch Coverage Badge]
9+ ![ Unit Test Coverage Badge]
1010![ Mutant Coverage Badge]
11+ ![ Integration Test Coverage Badge]
1112
1213[ ![ Supported Java Versions] [ Supported Java Versions Badge ]] ( # )
1314
@@ -31,13 +32,15 @@ Table of Contents
3132* [ Usage] ( #usage )
3233 * [ Message Framework] ( #message-framework )
3334 * [ Javacord] ( #javacord )
35+ * [ JDA] ( #jda )
3436 * [ Creating Commands] ( #creating-commands )
3537 * [ Command Aliases] ( #command-aliases )
3638 * [ Asynchronous Command Execution] ( #asynchronous-command-execution )
3739 * [ Command Restrictions] ( #command-restrictions )
3840 * [ Command Usage] ( #command-usage )
3941 * [ Parsing Parameters] ( #parsing-parameters )
4042 * [ Customizing Command Prefix] ( #customizing-command-prefix )
43+ * [ Customizing Alias Calculation] ( #customizing-alias-calculation )
4144 * [ CDI Events] ( #cdi-events )
4245 * [ Handling Missing Commands] ( #handling-missing-commands )
4346 * [ Handling Disallowed Commands] ( #handling-disallowed-commands )
@@ -63,6 +66,7 @@ Supported Message Frameworks
6366The following message frameworks are currently supported:
6467
6568* [ Javacord] ( #javacord )
69+ * [ JDA] ( #jda )
6670
6771If you want to have support for an additional, do not hesitate to open a PR or feature request issue.
6872
7579
7680``` gradle
7781repositories { mavenCentral() }
78- dependencies { implementation 'net.kautler:command-framework:0.2 .0' }
82+ dependencies { implementation 'net.kautler:command-framework:0.3 .0' }
7983```
8084
8185### Maven
@@ -84,7 +88,7 @@ dependencies { implementation 'net.kautler:command-framework:0.2.0' }
8488<dependency >
8589 <groupId >net.kautler</groupId >
8690 <artifactId >command-framework</artifactId >
87- <version >0.2 .0</version >
91+ <version >0.3 .0</version >
8892</dependency >
8993```
9094
@@ -103,12 +107,16 @@ Usage
103107
104108For the [ Javacord] [ Javacord Website ] support, include Javacord as implementation dependency and create a CDI producer
105109that produces either one ` DiscordApi ` , or if you use sharding a ` Collection<DiscordApi> ` with all shards where you want
106- commands to be handled.
110+ commands to be handled. You should also have a disposer method that properly disconnects the produced ` DiscordApi `
111+ instances.
107112
108113_ ** Example:** _
109114``` java
110115@ApplicationScoped
111116public class JavacordProducer {
117+ @Inject
118+ private volatile Logger logger;
119+
112120 @Inject
113121 @Named
114122 private String discordToken;
@@ -119,7 +127,11 @@ public class JavacordProducer {
119127 return new DiscordApiBuilder ()
120128 .setToken(discordToken)
121129 .login()
122- .exceptionally(ExceptionLogger . get())
130+ .whenComplete((discordApi, throwable) - > {
131+ if (throwable != null ) {
132+ logger. error(" Exception while logging in to Discord" , throwable);
133+ }
134+ })
123135 .join();
124136 }
125137
@@ -129,6 +141,49 @@ public class JavacordProducer {
129141}
130142```
131143
144+ _ ** Tested versions:** _
145+ * ` 3.0.5 `
146+
147+ #### JDA
148+
149+ For the [ JDA] [ JDA Website ] support, include JDA as implementation dependency and create a CDI producer that produces
150+ either one ` JDA ` , of if you use sharding a ` Collection<JDA> ` , a ` ShardManager ` or a ` Collection<ShardManager> ` with all
151+ shards where you want commands to be handled. You should also have a disposer method that properly shuts down the
152+ produced ` JDA ` and / or ` ShardManager ` instances.
153+
154+ _ ** Example:** _
155+ ``` java
156+ @ApplicationScoped
157+ public class JdaProducer {
158+ @Inject
159+ private volatile Logger logger;
160+
161+ @Inject
162+ @Named
163+ private String discordToken;
164+
165+ @Produces
166+ @ApplicationScoped
167+ private JDA produceJda () {
168+ try {
169+ return new JDABuilder (discordToken)
170+ .build()
171+ .awaitReady();
172+ } catch (InterruptedException | LoginException e) {
173+ logger. error(" Exception while logging in to Discord" , e);
174+ return null ;
175+ }
176+ }
177+
178+ private void disposeJda (@Disposes JDA jda ) {
179+ jda. shutdown();
180+ }
181+ }
182+ ```
183+
184+ _ ** Tested versions:** _
185+ * ` 4.0.0_52 `
186+
132187### Creating Commands
133188
134189Create a CDI bean that implements the [ ` Command ` ] [ Command JavaDoc ] interface.
@@ -235,10 +290,10 @@ There are two helpers to split the `parameterString` that is provided to `Comman
235290that can then be handled separately.
236291
237292The first is the method ` Command.getParameters(...) ` which you give the parameter string and the maximum amount of
238- parameters to split into. The provided string will then be split at any arbitrary amount of consecutive non-newline
239- whitespace characters. The last element of the returned array will have all remaining text in the parameter string. If
240- you expect exactly three parameters without whitespaces, you should set the max parameters to four, so you can easily
241- test the length of the returned array whether too many parameters were given to the command.
293+ parameters to split into. The provided string will then be split at any arbitrary amount of consecutive whitespace
294+ characters. The last element of the returned array will have all remaining text in the parameter string. If you expect
295+ exactly three parameters without whitespaces, you should set the max parameters to four, so you can easily test the
296+ length of the returned array whether too many parameters were given to the command.
242297
243298The second is the [ ` ParameterParser ` ] [ ParameterParser JavaDoc ] that you can get injected into your command. For the
244299` ParameterParser ` to work, the [ usage] ( #command-usage ) of the command has to follow a defined syntax language. This
@@ -362,6 +417,50 @@ public class MentionPrefixProvider extends MentionPrefixProviderJavacord {
362417}
363418```
364419
420+ #### Customizing Alias Calculation
421+
422+ The alias calculation can be customized by providing a CDI bean that implements the
423+ [ ` AliasAndParameterStringTransformer ` ] [ AliasAndParameterStringTransformer JavaDoc ] interface. In the implementation of
424+ the ` transformAliasAndParameterString ` method you can determine from the message that caused the processing what the
425+ alias and parameter string should be. The transformer is called after the alias and parameter string are determined from
426+ the message using all registered aliases and before the command is resolved from the alias. If an alias was found from
427+ the registered aliases, the ` aliasAndParameterString ` parameter contains the found information. If no alias was found,
428+ the parameter will be ` null ` . The fields in the ` AliasAndParameterString ` object are always non-` null ` .
429+
430+ The transformer can then either accept the found alias and parameter string by returning the argument directly, or it
431+ can determine a new alias and parameter string and return these. The return value of the transformer will be used for
432+ further processing. If the alias in the returned object is not one of the registered aliases or the transformer returns
433+ ` null ` , there will not be any command found and the respective CDI event will be fired.
434+
435+ Example use-cases for this are:
436+
437+ - fuzzy-searching for mistyped aliases and their automatic correction (this could also be used for just a
438+ "did you mean X" response, but for that the command not found events are probably better suited)
439+
440+ - having a command that forwards to one command in one channel but to another command in another channel,
441+ like ` !player ` that forwards to ` !mc:player ` in an MC channel but to ` !s4:player ` in an S4 channel
442+
443+ - supporting something like ` !runas @other-user foo bar baz ` , where the transformer will transform that to alias
444+ ` foo ` and parameter string ` bar baz ` and then a custom ` Restriction ` can check whether the message author has
445+ the permissions to use ` !runas ` and then for example whether the ` other-user ` would have permissions for the
446+ ` foo ` command and only then allow it to proceed
447+
448+ - forwarding to a ` !help ` command if an unknown command was issued
449+
450+ _ ** Example:** _
451+ ``` java
452+ @ApplicationScoped
453+ public class MyAliasAndParameterStringTransformer implements AliasAndParameterStringTransformer<Message > {
454+ @Override
455+ public AliasAndParameterString transformAliasAndParameterString (
456+ Message message , AliasAndParameterString aliasAndParameterString ) {
457+ return (aliasAndParameterString == null )
458+ ? new AliasAndParameterString (" help" , " " )
459+ : aliasAndParameterString;
460+ }
461+ }
462+ ```
463+
365464### CDI Events
366465
367466#### Handling Missing Commands
@@ -463,14 +562,16 @@ limitations under the License.
463562 https://github.com/Vampire/command-framework/blob/master/LICENSE
464563[ Discord Badge] :
465564 https://shields.javacord.org/discord/534420861294346255.svg?label=Discord
466- [ Branch Coverage Badge] :
467- https://shields.javacord.org/badge/Branch %20Coverage-100%25-brightgreen.svg?style=flat
565+ [ Unit Test Coverage Badge] :
566+ https://shields.javacord.org/badge/Unit%20Test %20Coverage-100%25-brightgreen.svg?style=flat
468567[ Mutant Coverage Badge] :
469568 https://shields.javacord.org/badge/PIT%20Mutant%20Coverage-100%25-brightgreen.svg?style=flat
569+ [ Integration Test Coverage Badge] :
570+ https://shields.javacord.org/badge/Integration%20Test%20Coverage-~75%25-brightgreen.svg?style=flat
470571[ Supported Java Versions Badge] :
471572 https://shields.javacord.org/badge/Supported%20Java%20Versions-Java8+-lightgrey.svg
472573[ Supported Message Frameworks Badge] :
473- https://shields.javacord.org/badge/Supported%20Message%20Frameworks-Javacord-lightgrey.svg
574+ https://shields.javacord.org/badge/Supported%20Message%20Frameworks-Javacord%20%7C%20JDA -lightgrey.svg
474575[ Weld SE Website] :
475576 https://docs.jboss.org/weld/reference/latest/en-US/html/environments.html#weld-se
476577[ Semantic Versioning Website] :
@@ -485,6 +586,8 @@ limitations under the License.
485586
486587[ Javacord Website] :
487588 https://javacord.org
589+ [ JDA Website] :
590+ https://github.com/DV8FromTheWorld/JDA
488591
489592[ @Alias JavaDoc] :
490593 https://www.javadoc.io/page/net.kautler/command-framework/latest/net/kautler/command/api/annotation/Alias.html
@@ -508,6 +611,8 @@ limitations under the License.
508611 https://www.javadoc.io/page/net.kautler/command-framework/latest/net/kautler/command/api/ParameterParser.html
509612[ PrefixProvider JavaDoc] :
510613 https://www.javadoc.io/page/net.kautler/command-framework/latest/net/kautler/command/api/prefix/PrefixProvider.html
614+ [ AliasAndParameterStringTransformer JavaDoc] :
615+ https://www.javadoc.io/page/net.kautler/command-framework/latest/net/kautler/command/api/AliasAndParameterStringTransformer.html
511616[ @RestrictedTo JavaDoc] :
512617 https://www.javadoc.io/page/net.kautler/command-framework/latest/net/kautler/command/api/annotation/RestrictedTo.html
513618[ Restriction JavaDoc] :
0 commit comments