Skip to content

Commit

Permalink
[AB-xxx] changing the meaning of the context utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Sheldan committed Jul 2, 2024
1 parent 79ea84d commit 5e98b8e
Show file tree
Hide file tree
Showing 31 changed files with 100 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ public class CreateCustomCommand extends AbstractConditionableCommand {
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
String name = slashCommandParameterService.getCommandOption(CUSTOM_COMMAND_NAME_PARAMETER, event, String.class);
String content = slashCommandParameterService.getCommandOption(CUSTOM_COMMAND_CONTENT_PARAMETER, event, String.class);
if(ContextUtils.isGuildKnown(event)) {
customCommandService.createCustomCommand(name, content, event.getMember());
} else {
if(ContextUtils.isUserCommand(event)) {
customCommandService.createUserCustomCommand(name, content, event.getUser());
} else {
customCommandService.createCustomCommand(name, content, event.getMember());
}
return interactionService.replyEmbed(CREATE_CUSTOM_COMMAND_RESPONSE_TEMPLATE_KEY, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ public class DeleteCustomCommand extends AbstractConditionableCommand {
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
String name = slashCommandParameterService.getCommandOption(CUSTOM_COMMAND_NAME_PARAMETER, event, String.class);
if(ContextUtils.isGuildKnown(event)) {
customCommandService.deleteCustomCommand(name, event.getGuild());
} else {
if(ContextUtils.isUserCommand(event)) {
customCommandService.deleteUserCustomCommand(name, event.getUser());
} else {
customCommandService.deleteCustomCommand(name, event.getGuild());
}
return interactionService.replyEmbed(DELETE_CUSTOM_COMMAND_RESPONSE_TEMPLATE_KEY, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@ public class GetCustomCommand extends AbstractConditionableCommand {
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
String name = slashCommandParameterService.getCommandOption(CUSTOM_COMMAND_NAME_PARAMETER, event, String.class);
CustomCommand customCommand;
if(ContextUtils.isGuildKnown(event)) {
customCommand = customCommandService.getCustomCommand(name, event.getGuild());
} else {
if(ContextUtils.isUserCommand(event)) {
customCommand = customCommandService.getUserCustomCommand(name, event.getUser());
} else {
customCommand = customCommandService.getCustomCommand(name, event.getGuild());
}
CustomCommandResponseModel model = CustomCommandResponseModel
.builder()
Expand All @@ -68,7 +68,7 @@ public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEven
public List<String> performAutoComplete(CommandAutoCompleteInteractionEvent event) {
if(slashCommandAutoCompleteService.matchesParameter(event.getFocusedOption(), CUSTOM_COMMAND_NAME_PARAMETER)) {
String input = event.getFocusedOption().getValue();
if(ContextUtils.isGuildKnown(event)) {
if(ContextUtils.isNotUserCommand(event)) {
return customCommandService.getCustomCommandsStartingWith(input, event.getGuild())
.stream()
.map(CustomCommand::getName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ public class ListCustomCommands extends AbstractConditionableCommand {
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
List<CustomCommand> customCommands;
if(ContextUtils.isGuildKnown(event)) {
customCommands = customCommandService.getCustomCommands(event.getGuild());
} else {
if(ContextUtils.isUserCommand(event)) {
customCommands = customCommandService.getUserCustomCommands(event.getUser());
} else {
customCommands = customCommandService.getCustomCommands(event.getGuild());
}
if(customCommands.isEmpty()) {
return interactionService.replyEmbed(NO_CUSTOM_COMMANDS_TEMPLATE_KEY, event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public CustomCommand createCustomCommand(String name, String content, AUserInASe
.additionalMessage(content)
.server(creator.getServerReference())
.creator(creator)
.userSpecific(false)
.creatorUser(creator.getUserReference())
.build();
return repository.save(customCommand);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEven
Integer credit = null;
Long serverId;
boolean economyEnabled = false;
if(ContextUtils.isGuildKnown(event)) {
if(ContextUtils.isNotUserCommand(event)) {
serverId = event.getGuild().getIdLong();
economyEnabled = featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, serverId);
if(economyEnabled){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ButtonClickedListenerResult execute(ButtonClickedListenerModel model) {
GameService.MineResult mineResult = gameService.uncoverField(mineBoard, payload.getX(), payload.getY());
mineBoard.setState(mineResult);
if(mineBoard.getState() != GameService.MineResult.CONTINUE) {
if(ContextUtils.isGuildKnown(model.getEvent())) {
if(ContextUtils.isNotUserCommand(model.getEvent())) {
if(featureFlagService.getFeatureFlagValue(EntertainmentFeatureDefinition.ECONOMY, model.getServerId())){
gameService.evaluateCreditChanges(mineBoard);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public ConditionResult shouldExecute(CommandContext commandContext, Command comm

@Override
public ConditionResult shouldExecute(SlashCommandInteractionEvent slashCommandInteractionEvent, Command command) {
if(ContextUtils.isGuildNotKnown(slashCommandInteractionEvent)) {
if(ContextUtils.isUserCommand(slashCommandInteractionEvent)) {
return ConditionResult.SUCCESS;
}
Optional<ModMailThread> threadOptional = modMailThreadManagementService.getByChannelOptional(channelManagementService.loadChannel(slashCommandInteractionEvent.getChannel()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEven
String reminderText = slashCommandParameterService.getCommandOption(REMIND_TEXT_PARAMETER, event, String.class, String.class);
String joinButtonId;
Reminder createdReminder;
if(!ContextUtils.isUserCommand(event)) {
if(ContextUtils.isUserCommand(event)) {
joinButtonId = null;
AUser aUser = userManagementService.loadOrCreateUser(event.getUser().getIdLong());
createdReminder = remindService.createReminderInForUser(aUser, reminderText, duration);
} else {
joinButtonId = componentService.generateComponentId();
Long snowFlake = SnowflakeUtils.createSnowFlake();
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(event.getMember());
createdReminder = remindService.createReminderInForUser(aUserInAServer, reminderText, duration, event.getChannel().getIdLong(), snowFlake);
} else {
joinButtonId = null;
AUser aUser = userManagementService.loadOrCreateUser(event.getUser().getIdLong());
createdReminder = remindService.createReminderInForUser(aUser, reminderText, duration);
}
ReminderModel remindModel = ReminderModel
.builder()
Expand All @@ -143,7 +143,7 @@ public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEven
.reminder(ReminderDisplay.fromReminder(createdReminder))
.build();

if(!ContextUtils.isUserCommand(event)) {
if(ContextUtils.isNotUserCommand(event)) {
Long serverId = event.getGuild().getIdLong();
AServer server = serverManagementService.loadServer(serverId);
JoinReminderPayload payload = JoinReminderPayload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,8 @@ public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEven
if(ContextUtils.isUserCommand(event)) {
messageToSend = getUserReminders(event.getUser());
} else {
Long serverId = event.getGuild().getIdLong();
Member member = event.getMember();
Long serverId = event.getGuild().getIdLong();
messageToSend = getServerReminders(serverId, member);
}
return interactionService.replyMessageToSend(messageToSend, event)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,18 @@ public CompletableFuture<InteractionHook> sendResponse(IReplyCallback callback,
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
boolean userCommand = ContextUtils.isUserCommandInGuild(event);
boolean guildAware = ContextUtils.isGuildKnown(event);
boolean knownGuild = ContextUtils.isGuildKnown(event);
UserInfoModel model = UserInfoModel
.builder()
.build();
if(guildAware) {
if(knownGuild) {
Member memberToShow;
if(slashCommandParameterService.hasCommandOption(MEMBER_PARAMETER, event)) {
memberToShow = slashCommandParameterService.getCommandOption(MEMBER_PARAMETER, event, Member.class);
} else {
memberToShow = event.getMember();
}
if(ContextUtils.isGuildKnown(event) && !memberToShow.getGuild().equals(event.getGuild())) {
if(!memberToShow.getGuild().equals(event.getGuild())) {
throw new EntityGuildMismatchException();
}
if(!memberToShow.hasTimeJoined()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEven
boolean sendInfo;
MessageToSend linkEmbed = templateService.renderEmbedTemplate(YOUTUBE_SEARCH_COMMAND_RESPONSE_LINK_TEMPLATE_KEY, model, ContextUtils.serverIdOrNull(event));
MessageToSend infoEmbed;
if(ContextUtils.isGuildKnown(event)) {
if(ContextUtils.isNotUserCommand(event)) {
sendInfo = featureModeService.featureModeActive(WebserviceFeatureDefinition.YOUTUBE, event.getGuild().getIdLong(), YoutubeWebServiceFeatureMode.VIDEO_DETAILS);
infoEmbed = templateService.renderEmbedTemplate(YOUTUBE_SEARCH_COMMAND_RESPONSE_TEMPLATE_KEY, model, ContextUtils.serverIdOrNull(event));
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void execute(CommandContext commandContext, CommandResult commandResult,

@Override
public void executeSlash(SlashCommandInteractionEvent interaction, CommandResult commandResult, Command command) {
if(commandResult.getResult().equals(ResultState.CONDITION) && ContextUtils.isGuildKnown(interaction)
if(commandResult.getResult().equals(ResultState.CONDITION) && ContextUtils.isNotUserCommand(interaction)
&& commandResult.getConditionResult() != null &&
!commandResult.getConditionResult().isResult()
&& commandResult.getConditionResult().getConditionDetail() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public boolean supportsSlash() {
public void executeSlash(SlashCommandInteractionEvent interaction, CommandResult commandResult, Command command) {
ResultState result = commandResult.getResult();
if(result.equals(ResultState.SUCCESSFUL) || result.equals(ResultState.IGNORED)) {
if(ContextUtils.isGuildKnown(interaction)) {
if(ContextUtils.isNotUserCommand(interaction)) {
commandCoolDownService.updateCoolDowns(command, interaction);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void executeSlash(SlashCommandInteractionEvent interaction, CommandResult
if(result.equals(ResultState.ERROR)) {
Throwable throwable = commandResult.getThrowable();
if(throwable != null) {
if(throwable instanceof CommandNotFoundException && ContextUtils.isGuildKnown(interaction)){
if(throwable instanceof CommandNotFoundException && ContextUtils.isNotUserCommand(interaction)){
String configValue = configService.getStringValueOrConfigDefault(CoreFeatureConfig.NO_COMMAND_REPORTING_CONFIG_KEY, interaction.getGuild().getIdLong());
if(!BooleanUtils.toBoolean(configValue)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public void reportExceptionToInteraction(Throwable exception, IReplyCallback cal

@Override
public void reportSlashException(Throwable exception, SlashCommandInteractionEvent event, Command command) {
if(ContextUtils.isGuildKnown(event)) {
if(ContextUtils.hasGuild(event)) {
log.info("Reporting exception of {} command {} in channel {} in guild {} from user {}.",
exception.getClass().getSimpleName(), command.getConfiguration().getName(),
event.getChannel().getIdLong(), event.getGuild().getIdLong(), event.getMember().getIdLong(), exception);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,10 @@ public List<CompletableFuture<Message>> sendMessageToInteraction(MessageToSend m
if(messageToSend.getEphemeral()) {
Interaction interaction = interactionHook.getInteraction();
interactionHook.setEphemeral(messageToSend.getEphemeral());
if(ContextUtils.isGuildKnown(interaction)) {
if(ContextUtils.hasGuild(interaction)) {
log.info("Sending ephemeral message to interaction in guild {} in channel {} for user {}.",
interaction.getGuild().getIdLong(), interaction.getChannel().getId(),
interaction.getMember().getIdLong());
interaction.getUser().getIdLong());
}
metricService.incrementCounter(EPHEMERAL_MESSAGES_SEND);
}
Expand Down Expand Up @@ -175,10 +175,10 @@ public CompletableFuture<Message> editOriginal(MessageToSend messageToSend, Inte

if(messageToSend.getEphemeral()) {
Interaction interaction = interactionHook.getInteraction();
if(ContextUtils.isGuildKnown(interaction)) {
if(ContextUtils.hasGuild(interaction)) {
log.info("Sending ephemeral message to interaction in guild {} in channel {} for user {}.",
interaction.getGuild().getIdLong(), interaction.getChannel().getId(),
interaction.getMember().getIdLong());
interaction.getUser().getIdLong());
} else {
log.info("Sending ephemeral message to interaction for user {}", interactionHook.getInteraction().getUser().getIdLong());
}
Expand Down Expand Up @@ -223,18 +223,20 @@ public CompletableFuture<Message> editOriginal(MessageToSend messageToSend, Inte
action = interactionHook.editOriginal(".");
}
action = action.setComponents(actionRows);
AServer server;
if(ContextUtils.isGuildKnown(interactionHook.getInteraction())) {
AServer server = serverManagementService.loadServer(serverId);
actionRows.forEach(components -> components.forEach(component -> {
if(component instanceof ActionComponent) {
String id = ((ActionComponent)component).getId();
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
if(payload != null && payload.getPersistCallback()) {
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
}
server = serverManagementService.loadServer(serverId);
} else {
server = null; }
actionRows.forEach(components -> components.forEach(component -> {
if(component instanceof ActionComponent) {
String id = ((ActionComponent)component).getId();
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
if(payload != null && payload.getPersistCallback()) {
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
}
}));
}
}
}));
}

if(action == null) {
Expand Down Expand Up @@ -280,26 +282,28 @@ public CompletableFuture<InteractionHook> replyMessageToSend(MessageToSend messa
action = callback.reply(".");
}
action = action.setComponents(actionRows);
AServer server;
if(ContextUtils.isGuildKnown(callback)) {
Long serverId = callback.getGuild().getIdLong();
AServer server = serverManagementService.loadServer(serverId);
actionRows.forEach(components -> components.forEach(component -> {
if(component instanceof ActionComponent) {
String id = ((ActionComponent)component).getId();
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
if(payload != null && payload.getPersistCallback()) {
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
}
}
}));
server = serverManagementService.loadServer(callback.getGuild().getIdLong());
} else {
server = null;
}
actionRows.forEach(components -> components.forEach(component -> {
if(component instanceof ActionComponent) {
String id = ((ActionComponent)component).getId();
MessageToSend.ComponentConfig payload = messageToSend.getComponentPayloads().get(id);
if(payload != null && payload.getPersistCallback()) {
componentPayloadManagementService.createPayload(id, payload.getPayload(), payload.getPayloadType(), payload.getComponentOrigin(), server, payload.getComponentType());
}
}
}));
}

if(messageToSend.getEphemeral()) {
if(ContextUtils.isGuildKnown(callback)) {
if(ContextUtils.hasGuild(callback)) {
log.info("Sending ephemeral message to interaction in guild {} in channel {} for user {}.",
callback.getGuild().getIdLong(), callback.getChannel().getId(),
callback.getMember().getIdLong());
callback.getUser().getIdLong());
} else {
log.info("Sending ephemeral message to user {}.", callback.getUser().getIdLong());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private List<ButtonClickedListener> filterFeatureAwareListener(List<ButtonClicke
if(!model.getEvent().isFromGuild()) {
return true;
}
if(ContextUtils.isGuildNotKnown(model.getEvent())) {
if(ContextUtils.isUserCommand(model.getEvent())) {
return true;
}
if (!featureFlagService.isFeatureEnabled(feature, model.getServerId())) {
Expand Down
Loading

0 comments on commit 5e98b8e

Please sign in to comment.