Skip to content

Commit bedec4a

Browse files
committed
validate live feed commands inside guild before user perms
1 parent 095825c commit bedec4a

File tree

2 files changed

+6
-18
lines changed

2 files changed

+6
-18
lines changed

src/main/java/vc/api/VcApi.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ public ApiClient apiClient(
3434
));
3535
}
3636

37-
@Bean(name = "namesApiClient")
38-
public ApiClient apiClientNames(
39-
final HttpClient.Builder httpClientBuilder,
40-
final ObjectMapper objectMapper,
41-
@Value("${API_KEY}") final String apiKey
42-
) {
43-
return apiClient(httpClientBuilder, objectMapper, apiKey)
44-
.setReadTimeout(Duration.ofSeconds(90));
45-
}
46-
4737
@Bean
4838
public ChatsApi chatsApi(final ApiClient apiClient) {
4939
return new ChatsApi(apiClient);

src/main/java/vc/commands/LiveFeedCommand.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package vc.commands;
22

3-
import discord4j.common.util.Snowflake;
43
import discord4j.core.event.domain.interaction.ChatInputInteractionEvent;
54
import discord4j.core.object.command.ApplicationCommandInteractionOption;
65
import discord4j.core.object.command.ApplicationCommandInteractionOptionValue;
@@ -28,25 +27,24 @@ public LiveFeedCommand(final LiveFeed liveFeed) {
2827

2928
@Override
3029
public Mono<Message> handle(final ChatInputInteractionEvent event) {
31-
if (!validateUserPermissions(event)) return error(event, "You must have permission: " + Permission.MANAGE_MESSAGES
32-
+ " to use this command");
30+
if (event.getInteraction().getGuildId().isEmpty()) return error(event, "This command can only be used inside a discord server");
31+
if (!validateUserPermissions(event)) return error(event, "You must have permission: " + Permission.MANAGE_MESSAGES + " to use this command");
3332
Optional<Boolean> enabledBoolean = event.getOption("enabled")
3433
.flatMap(ApplicationCommandInteractionOption::getValue)
3534
.map(ApplicationCommandInteractionOptionValue::asBoolean);
3635
Optional<Mono<Channel>> channelArg = event.getOption("channel")
3736
.flatMap(ApplicationCommandInteractionOption::getValue)
3837
.map(ApplicationCommandInteractionOptionValue::asChannel);
3938
if (enabledBoolean.isEmpty() && channelArg.isEmpty()) return error(event, "At least 1 argument is required");
40-
Optional<Snowflake> guildId = event.getInteraction().getGuildId();
41-
if (guildId.isEmpty()) return error(event, "This command can only be used in a guild");
39+
var guildId = event.getInteraction().getGuildId().get().asString();
4240
if (enabledBoolean.orElse(true) && channelArg.isEmpty()) return error(event, "Channel is required when enabling " + feedName());
4341
if (enabledBoolean.orElse(true)) {
4442
try {
4543
final Channel channel = channelArg.get().block();
46-
if (!testPermissions(guildId.get().asString(), channel)) {
44+
if (!testPermissions(guildId, channel)) {
4745
return error(event, "Bot must have permissions to send messages in: " + channel.getMention());
4846
}
49-
liveFeed.enableFeed(guildId.get().asString(), channel.getId().asString());
47+
liveFeed.enableFeed(guildId, channel.getId().asString());
5048
return event.createFollowup()
5149
.withEmbeds(EmbedCreateSpec.builder()
5250
.title(feedName() + " Enabled")
@@ -58,7 +56,7 @@ public Mono<Message> handle(final ChatInputInteractionEvent event) {
5856
}
5957
} else {
6058
try {
61-
liveFeed.disableFeed(guildId.get().asString());
59+
liveFeed.disableFeed(guildId);
6260
return event.createFollowup()
6361
.withEmbeds(EmbedCreateSpec.builder()
6462
.title(feedName() + " Disabled")

0 commit comments

Comments
 (0)