Skip to content

Commit

Permalink
[AB-xxx] making echo work with user installable apps
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheldan committed Apr 12, 2024
1 parent 250df57 commit 03eef96
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 19 deletions.
2 changes: 1 addition & 1 deletion abstracto-application/core/core-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
</dependency>

<dependency>
<groupId>net.dv8tion</groupId>
<groupId>io.github.freya022</groupId>
<artifactId>JDA</artifactId>
<exclusions>
<exclusion>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ public CommandResult execute(CommandContext commandContext) {

@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {

String message = slashCommandParameterService.getCommandOption(INPUT_PARAMETER, event, String.class);
GuildMessageChannel messageChannel;
boolean redirect = false;
Expand Down Expand Up @@ -127,6 +126,7 @@ public CommandConfiguration getConfiguration() {
.templated(true)
.supportsEmbedException(true)
.causesReaction(false)
.userInstallable(true)
.slashCommandConfig(slashCommandConfig)
.parameters(parameters)
.help(helpInfo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.JDA;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.commands.build.CommandData;
import net.dv8tion.jda.api.interactions.commands.build.SlashCommandData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.util.Pair;
Expand All @@ -28,6 +29,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Slf4j
Expand Down Expand Up @@ -92,7 +94,7 @@ public void execute() {
return;
}
log.info("Updating slash command {} in guild {}.", command.getConfiguration().getName(), guild.getId());
slashCommandService.convertCommandConfigToCommandData(command.getConfiguration(), slashCommandsToUpdate, guild.getIdLong());
slashCommandService.convertCommandConfigToCommandData(command.getConfiguration(), slashCommandsToUpdate, guild.getIdLong(), false);
});

log.info("Updating context commands for guild {}.", guild.getIdLong());
Expand Down Expand Up @@ -126,7 +128,15 @@ public void execute() {
return null;
});
});

List<Pair<List<CommandConfiguration>, SlashCommandData>> userCommandsToUpdate = new ArrayList<>();
incomingSlashCommands.forEach(command -> {
slashCommandService.convertCommandConfigToCommandData(command.getConfiguration(), userCommandsToUpdate, jda.getGuilds().get(0).getIdLong(), true);
});
List<CommandData> userCommands = userCommandsToUpdate
.stream()
.map(Pair::getSecond)
.collect(Collectors.toList());
jda.updateCommands().addCommands(userCommands).queue();
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import dev.sheldan.abstracto.core.templating.model.AttachedFile;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.core.utils.FileService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageEmbed;
Expand Down Expand Up @@ -235,7 +234,6 @@ public CompletableFuture<Message> editOriginal(MessageToSend messageToSend, Inte
}

public CompletableFuture<InteractionHook> replyMessageToSend(MessageToSend messageToSend, IReplyCallback callback) {
Long serverId = callback.getGuild().getIdLong();
ReplyCallbackAction action = null;
if(messageToSend.getMessages() != null && !messageToSend.getMessages().isEmpty()) {
metricService.incrementCounter(MESSAGE_SEND_METRIC);
Expand Down Expand Up @@ -272,6 +270,7 @@ public CompletableFuture<InteractionHook> replyMessageToSend(MessageToSend messa
action = callback.reply(".");
}
action = action.setComponents(actionRows);
Long serverId = callback.getGuild().getIdLong();
AServer server = serverManagementService.loadServer(serverId);
actionRows.forEach(components -> components.forEach(component -> {
if(component instanceof ActionComponent) {
Expand All @@ -293,9 +292,12 @@ public CompletableFuture<InteractionHook> replyMessageToSend(MessageToSend messa
action = action.setEphemeral(messageToSend.getEphemeral());
}
}
Set<Message.MentionType> allowedMentions = allowedMentionService.getAllowedMentionsFor(callback.getMessageChannel(), messageToSend);
if(action != null) {
action.setAllowedMentions(allowedMentions);
if(callback.getHook().getInteraction().hasGuild()) {
Set<Message.MentionType> allowedMentions = allowedMentionService.getAllowedMentionsFor(callback.getMessageChannel(), messageToSend);
if(action != null) {
action.setAllowedMentions(allowedMentions);
}

}

if(action == null) {
Expand All @@ -306,7 +308,10 @@ public CompletableFuture<InteractionHook> replyMessageToSend(MessageToSend messa

@Override
public CompletableFuture<InteractionHook> replyMessage(String templateKey, Object model, IReplyCallback callback) {
Long serverId = callback.getGuild().getIdLong();
Long serverId = null;
if(callback.getHook().getInteraction().hasGuild()) {
serverId = callback.getGuild().getIdLong();
}
MessageToSend messageToSend = templateService.renderTemplateToMessageToSend(templateKey, model, serverId);
return replyMessageToSend(messageToSend, callback);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ public List<Command> getSlashCommands() {
public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
try {
if(commands == null || commands.isEmpty()) return;
log.debug("Executing slash command in guild {} from user {}.", event.getGuild().getIdLong(), event.getMember().getIdLong());
if(event.hasGuild()) {
log.debug("Executing slash command in guild {} from user {}.", event.getGuild().getIdLong(), event.getMember().getIdLong());
}
CompletableFuture.runAsync(() -> self.executeListenerLogic(event), slashCommandExecutor).exceptionally(throwable -> {
log.error("Failed to execute listener logic in async slash command event.", throwable);
return null;
Expand All @@ -90,6 +92,7 @@ public void onSlashCommandInteraction(SlashCommandInteractionEvent event) {
}
}


@Transactional
public void executeListenerLogic(SlashCommandInteractionEvent event) {
Optional<Command> potentialCommand = findCommand(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.core.utils.CompletableFutureList;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.interactions.IntegrationType;
import net.dv8tion.jda.api.interactions.commands.Command;
import net.dv8tion.jda.api.interactions.commands.OptionType;
import net.dv8tion.jda.api.interactions.commands.build.*;
Expand Down Expand Up @@ -53,7 +54,10 @@ public class SlashCommandServiceBean implements SlashCommandService {
private FeatureFlagService featureFlagService;

@Override
public void convertCommandConfigToCommandData(CommandConfiguration commandConfiguration, List<Pair<List<CommandConfiguration>, SlashCommandData>> existingCommands, Long serverId) {
public void convertCommandConfigToCommandData(CommandConfiguration commandConfiguration, List<Pair<List<CommandConfiguration>, SlashCommandData>> existingCommands, Long serverId, boolean userCommandsOnly) {
if(userCommandsOnly && !commandConfiguration.isUserInstallable()) {
return;
}
boolean isTemplated = commandConfiguration.isTemplated();
SlashCommandConfig slashConfig = commandConfiguration.getSlashCommandConfig();
String description;
Expand All @@ -72,6 +76,9 @@ public void convertCommandConfigToCommandData(CommandConfiguration commandConfig
.map(Pair::getSecond)
.findAny();
SlashCommandData rootCommand = existingRootCommand.orElseGet(() -> Commands.slash(rootName, description));
if(commandConfiguration.isUserInstallable() && userCommandsOnly) {
rootCommand.setIntegrationTypes(IntegrationType.USER_INSTALL);
}
if(commandName != null) {
SubcommandData slashCommand = new SubcommandData(commandName, description);
if(groupName == null) {
Expand All @@ -94,7 +101,7 @@ public void convertCommandConfigToCommandData(CommandConfiguration commandConfig
List<OptionData> requiredParameters = getParameters(commandConfiguration, isTemplated, internalCommandName, serverId);
rootCommand.addOptions(requiredParameters);
}
if(!existingRootCommand.isPresent()) {
if(existingRootCommand.isEmpty()) {
Optional<Pair<List<CommandConfiguration>, SlashCommandData>> existingCommand = existingCommands
.stream()
.filter(listSlashCommandDataPair -> listSlashCommandDataPair.getSecond().equals(rootCommand))
Expand All @@ -109,7 +116,7 @@ public void convertCommandConfigToCommandData(CommandConfiguration commandConfig

@Override
public void convertCommandConfigToCommandData(CommandConfiguration commandConfiguration, List<Pair<List<CommandConfiguration>, SlashCommandData>> existingCommands) {
convertCommandConfigToCommandData(commandConfiguration, existingCommands, null);
convertCommandConfigToCommandData(commandConfiguration, existingCommands, null, false);
}

private List<OptionData> getParameters(CommandConfiguration commandConfiguration, boolean isTemplated, String internalCommandName, Long serverId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public DefaultListenerResult execute(FeatureActivationListenerModel model) {
return;
}
log.info("Updating slash command {} in guild {}.", command.getConfiguration().getName(), guild.getId());
slashCommandService.convertCommandConfigToCommandData(command.getConfiguration(), commandsToUpDate, model.getServerId());
slashCommandService.convertCommandConfigToCommandData(command.getConfiguration(), commandsToUpDate, model.getServerId(), false);
});
slashCommandService.addGuildSlashCommands(guild, commandsToUpDate)
.thenAccept(commands1 -> log.info("Updating {} slash commands in guild {}.", commandsToUpDate.size(), guild.getIdLong()));
Expand Down
2 changes: 1 addition & 1 deletion abstracto-application/core/core-int/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>net.dv8tion</groupId>
<groupId>io.github.freya022</groupId>
<artifactId>JDA</artifactId>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public ConditionResult shouldExecute(CommandContext context, Command command) {

@Override
public ConditionResult shouldExecute(SlashCommandInteractionEvent slashCommandInteractionEvent, Command command) {
if(!slashCommandInteractionEvent.hasGuild()) {
return ConditionResult.SUCCESS;
}
boolean adminModeActive = service.adminModeActive(slashCommandInteractionEvent.getGuild());
if(adminModeActive){
if(slashCommandInteractionEvent.getMember().hasPermission(Permission.ADMINISTRATOR)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public ConditionResult shouldExecute(CommandContext commandContext, Command comm

@Override
public ConditionResult shouldExecute(SlashCommandInteractionEvent slashCommandInteractionEvent, Command command) {
if(!slashCommandInteractionEvent.hasGuild()) {
return ConditionResult.SUCCESS;
}
commandCoolDownService.takeLock();
try {
CoolDownCheckResult result = commandCoolDownService.allowedToExecuteCommand(command, slashCommandInteractionEvent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ public ConditionResult shouldExecute(CommandContext context, Command command) {

@Override
public ConditionResult shouldExecute(SlashCommandInteractionEvent slashCommandInteractionEvent, Command command) {
if(!slashCommandInteractionEvent.hasGuild()) {
return ConditionResult.SUCCESS;
}
Long serverId = slashCommandInteractionEvent.getGuild().getIdLong();
return evaluateFeatureCondition(command, serverId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ private ConditionResult checkFeatureModeCondition(Command command, Long serverId

@Override
public ConditionResult shouldExecute(SlashCommandInteractionEvent slashCommandInteractionEvent, Command command) {
if(!slashCommandInteractionEvent.hasGuild()) {
return ConditionResult.SUCCESS;
}
Long serverId = slashCommandInteractionEvent.getGuild().getIdLong();
return checkFeatureModeCondition(command, serverId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public void checkConditions(CommandConfiguration commandConfig, List<Object> par

@Override
public CompletableFuture<ConditionResult> shouldExecuteAsync(SlashCommandInteractionEvent event, Command command) {
if(!event.hasGuild()) {
return CompletableFuture.completedFuture(ConditionResult.SUCCESS);
}
CommandConfiguration commandConfig = command.getConfiguration();
if(commandConfig.getEffects().isEmpty()) {
return ConditionResult.fromAsyncSuccess();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class CommandConfiguration {
private String name;
private String module;
private String description;
@Builder.Default
private boolean userInstallable = false;

@Builder.Default
private boolean async = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.concurrent.CompletableFuture;

public interface SlashCommandService {
void convertCommandConfigToCommandData(CommandConfiguration commandConfiguration, List<Pair<List<CommandConfiguration>, SlashCommandData>> existingCommands, Long serverId);
void convertCommandConfigToCommandData(CommandConfiguration commandConfiguration, List<Pair<List<CommandConfiguration>, SlashCommandData>> existingCommands, Long serverId, boolean userCommandsOnly);
void convertCommandConfigToCommandData(CommandConfiguration commandConfiguration, List<Pair<List<CommandConfiguration>, SlashCommandData>> existingCommands);
CompletableFuture<List<Command>> updateGuildSlashCommand(Guild guild, List<Pair<List<CommandConfiguration>, SlashCommandData>> commandData);
CompletableFuture<Void> deleteGuildSlashCommands(Guild guild, List<Long> slashCommandId, List<Long> commandInServerIdsToUnset);
Expand Down
4 changes: 2 additions & 2 deletions abstracto-application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,9 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>net.dv8tion</groupId>
<groupId>io.github.freya022</groupId>
<artifactId>JDA</artifactId>
<version>${jda.version}</version>
<version>73d3694</version>
<exclusions>
<exclusion>
<groupId>club.minnced</groupId>
Expand Down

0 comments on commit 03eef96

Please sign in to comment.