Skip to content

Commit 9730677

Browse files
committed
Support slash commands for Javacord
1 parent a816a86 commit 9730677

File tree

56 files changed

+8352
-25
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+8352
-25
lines changed

config/codenarc/codenarc.groovy

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,7 @@ ruleset {
302302
ExplicitCallToCompareToMethod {
303303
doNotApplyToClassNames = [
304304
'net.kautler.command.api.restriction.javacord.RoleJavacordTest',
305+
'net.kautler.command.api.restriction.javacord.slash.RoleJavacordSlashTest',
305306
'net.kautler.command.api.restriction.jda.RoleJdaTest'
306307
].join(', ')
307308
}
@@ -415,7 +416,9 @@ ruleset {
415416
ObjectOverrideMisspelledMethodName
416417
//PackageName
417418
PackageNameMatchesFilePath
418-
ParameterName
419+
ParameterName {
420+
ignoreParameterNames = '_'
421+
}
419422
PropertyName
420423
VariableName
421424

@@ -507,7 +510,11 @@ ruleset {
507510
// rulesets/unused.xml
508511
UnusedArray
509512
UnusedMethodParameter {
510-
doNotApplyToClassNames = 'net.kautler.command.integ.test.VersionIntegTest$VersionHolder'
513+
doNotApplyToClassNames = [
514+
'net.kautler.command.integ.test.VersionIntegTest$VersionHolder',
515+
'net.kautler.command.integ.test.javacord.PingSlashIntegTest$SlashCommandRegisterer',
516+
'net.kautler.command.integ.test.javacord.event.CommandNotFoundEventJavacordSlashIntegTest$SlashCommandRegisterer'
517+
].join(', ')
511518
}
512519
UnusedObject
513520
UnusedPrivateField

config/pmd/pmd.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,9 @@
270270
\QFound 'DD'-anomaly for variable 'distance' (lines '\E.* |
271271
\QFound 'DU'-anomaly for variable 'channelId' (lines '\E.* |
272272
\QFound 'DU'-anomaly for variable 'roleId' (lines '\E.* |
273-
\QFound 'DU'-anomaly for variable 'userId' (lines '\E.*
273+
\QFound 'DU'-anomaly for variable 'userId' (lines '\E.* |
274+
\QFound 'DU'-anomaly for variable 'alias' (lines '\E.* |
275+
\QFound 'DU'-anomaly for variable 'subcommandOrGroup' (lines '\E.*
274276
</value>
275277
</property>
276278
</properties>

config/spotbugs/spotbugs-exclude.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@
5151
<Class name="net.kautler.command.api.restriction.javacord.RoleJavacord"/>
5252
<Class name="net.kautler.command.api.restriction.javacord.ServerJavacord"/>
5353
<Class name="net.kautler.command.api.restriction.javacord.UserJavacord"/>
54+
<Class name="net.kautler.command.api.restriction.javacord.slash.ChannelJavacordSlash"/>
55+
<Class name="net.kautler.command.api.restriction.javacord.slash.RoleJavacordSlash"/>
56+
<Class name="net.kautler.command.api.restriction.javacord.slash.ServerJavacordSlash"/>
57+
<Class name="net.kautler.command.api.restriction.javacord.slash.UserJavacordSlash"/>
5458
<Class name="net.kautler.command.api.restriction.jda.ChannelJda"/>
5559
<Class name="net.kautler.command.api.restriction.jda.GuildJda"/>
5660
<Class name="net.kautler.command.api.restriction.jda.RoleJda"/>
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* Copyright 2019-2022 Björn Kautler
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.kautler.command.example.ping;
18+
19+
import java.util.List;
20+
import java.util.concurrent.CompletableFuture;
21+
import java.util.function.Function;
22+
23+
import javax.enterprise.context.ApplicationScoped;
24+
import javax.inject.Inject;
25+
26+
import net.kautler.command.api.Command;
27+
import net.kautler.command.api.CommandContext;
28+
import net.kautler.command.api.annotation.Alias;
29+
import net.kautler.command.api.annotation.Description;
30+
import net.kautler.command.api.slash.javacord.SlashCommandJavacord;
31+
import org.apache.logging.log4j.Logger;
32+
import org.javacord.api.entity.message.Message;
33+
import org.javacord.api.interaction.SlashCommandInteraction;
34+
import org.javacord.api.interaction.SlashCommandOption;
35+
36+
import static java.util.Collections.singletonList;
37+
38+
class CombinedPingCommand {
39+
@Inject
40+
Logger logger;
41+
42+
protected void doExecute(String nonce, Function<String, CompletableFuture<?>> responder) {
43+
responder
44+
.apply(((nonce == null) || nonce.isEmpty()) ? "pong" : "pong: " + nonce)
45+
.whenComplete((sentMessage, throwable) -> {
46+
if (throwable != null) {
47+
logger.error("Exception while executing ping command", throwable);
48+
}
49+
});
50+
}
51+
52+
@Alias("combined-ping")
53+
@Description("Ping back an optional nonce")
54+
@ApplicationScoped
55+
static class PingSlashCommand extends CombinedPingCommand implements SlashCommandJavacord {
56+
@Override
57+
public void execute(CommandContext<? extends SlashCommandInteraction> commandContext) {
58+
SlashCommandInteraction slashCommandInteraction = commandContext.getMessage();
59+
doExecute(
60+
slashCommandInteraction.getOptionStringValueByName("nonce").orElse(null),
61+
reply -> slashCommandInteraction
62+
.createImmediateResponder()
63+
.setContent(reply)
64+
.respond());
65+
}
66+
67+
@Override
68+
public List<SlashCommandOption> getOptions() {
69+
return singletonList(SlashCommandOption.createStringOption(
70+
"nonce", "The nonce to echo back with the pong", false));
71+
}
72+
}
73+
74+
@Alias("combined-ping")
75+
@ApplicationScoped
76+
static class PingTextCommand extends CombinedPingCommand implements Command<Message> {
77+
@Override
78+
public void execute(CommandContext<? extends Message> commandContext) {
79+
doExecute(
80+
commandContext.getParameterString().orElse(null),
81+
reply -> commandContext
82+
.getMessage()
83+
.getChannel()
84+
.sendMessage(reply));
85+
}
86+
}
87+
}

examples/simplePingBotJavacord/src/main/java/net/kautler/command/example/ping/PingCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public void execute(CommandContext<? extends Message> commandContext) {
3636
.getChannel()
3737
.sendMessage(commandContext
3838
.getParameterString()
39-
.filter(parameterString -> !parameterString.isEmpty())
40-
.map(parameterString -> "pong: " + parameterString)
39+
.filter(nonce -> !nonce.isEmpty())
40+
.map(nonce -> "pong: " + nonce)
4141
.orElse("pong"))
4242
.whenComplete((sentMessage, throwable) -> {
4343
if (throwable != null) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2019-2022 Björn Kautler
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.kautler.command.example.ping;
18+
19+
import java.util.List;
20+
21+
import javax.enterprise.context.ApplicationScoped;
22+
import javax.inject.Inject;
23+
24+
import net.kautler.command.api.CommandContext;
25+
import net.kautler.command.api.annotation.Alias;
26+
import net.kautler.command.api.annotation.Description;
27+
import net.kautler.command.api.slash.javacord.SlashCommandJavacord;
28+
import org.apache.logging.log4j.Logger;
29+
import org.javacord.api.interaction.SlashCommandInteraction;
30+
import org.javacord.api.interaction.SlashCommandOption;
31+
32+
import static java.util.Collections.singletonList;
33+
34+
@Alias("ping")
35+
@Description("Ping back an optional nonce")
36+
@ApplicationScoped
37+
class PingSlashCommand implements SlashCommandJavacord {
38+
@Inject
39+
Logger logger;
40+
41+
@Override
42+
public void execute(CommandContext<? extends SlashCommandInteraction> commandContext) {
43+
SlashCommandInteraction slashCommandInteraction = commandContext.getMessage();
44+
45+
slashCommandInteraction
46+
.createImmediateResponder()
47+
.setContent(slashCommandInteraction
48+
.getOptionStringValueByName("nonce")
49+
.map(nonce -> "pong: " + nonce)
50+
.orElse("pong"))
51+
.respond()
52+
.whenComplete((sentMessage, throwable) -> {
53+
if (throwable != null) {
54+
logger.error("Exception while executing ping command", throwable);
55+
}
56+
});
57+
}
58+
59+
@Override
60+
public List<SlashCommandOption> getOptions() {
61+
return singletonList(SlashCommandOption.createStringOption(
62+
"nonce", "The nonce to echo back with the pong", false));
63+
}
64+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2022 Björn Kautler
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package net.kautler.command.example.ping;
18+
19+
import java.util.List;
20+
21+
import javax.enterprise.context.ApplicationScoped;
22+
import javax.enterprise.context.Initialized;
23+
import javax.enterprise.event.Observes;
24+
import javax.inject.Inject;
25+
26+
import org.apache.logging.log4j.Logger;
27+
import org.javacord.api.DiscordApi;
28+
import org.javacord.api.interaction.SlashCommandBuilder;
29+
30+
@ApplicationScoped
31+
public class SlashCommandRegisterer {
32+
@Inject
33+
Logger logger;
34+
35+
@Inject
36+
DiscordApi discordApi;
37+
38+
@Inject
39+
List<SlashCommandBuilder> slashCommandBuilders;
40+
41+
void registerSlashCommands(@Observes @Initialized(ApplicationScoped.class) Object __) {
42+
discordApi
43+
.bulkOverwriteGlobalApplicationCommands(slashCommandBuilders)
44+
.whenComplete((slashCommands, throwable) -> {
45+
if (throwable != null) {
46+
logger.error("Exception while registering slash commands", throwable);
47+
}
48+
});
49+
}
50+
}

examples/simplePingBotJda/src/main/java/net/kautler/command/example/ping/PingCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public void execute(CommandContext<? extends Message> commandContext) {
3636
.getChannel()
3737
.sendMessage(commandContext
3838
.getParameterString()
39-
.filter(parameterString -> !parameterString.isEmpty())
40-
.map(parameterString -> "pong: " + parameterString)
39+
.filter(nonce -> !nonce.isEmpty())
40+
.map(nonce -> "pong: " + nonce)
4141
.orElse("pong"))
4242
.queue(null, throwable -> logger.error("Exception while executing ping command", throwable));
4343
}

0 commit comments

Comments
 (0)