Skip to content

Commit

Permalink
[AB-xxx] changing context Utils
Browse files Browse the repository at this point in the history
porting reminder module to support user commands
upgrading to new commit of JDA
  • Loading branch information
Sheldan committed Jun 18, 2024
1 parent abe383b commit 6e01092
Show file tree
Hide file tree
Showing 48 changed files with 333 additions and 580 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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.isGuildAware(event)) {
if(ContextUtils.isGuildKnown(event)) {
customCommandService.createCustomCommand(name, content, event.getMember());
} else {
customCommandService.createUserCustomCommand(name, content, event.getUser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ 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.isGuildAware(event)) {
if(ContextUtils.isGuildKnown(event)) {
customCommandService.deleteCustomCommand(name, event.getGuild());
} else {
customCommandService.deleteUserCustomCommand(name, event.getUser());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ 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.isGuildAware(event)) {
if(ContextUtils.isGuildKnown(event)) {
customCommand = customCommandService.getCustomCommand(name, event.getGuild());
} else {
customCommand = customCommandService.getUserCustomCommand(name, event.getUser());
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.isGuildAware(event)) {
if(ContextUtils.isGuildKnown(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,7 +45,7 @@ public class ListCustomCommands extends AbstractConditionableCommand {
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
List<CustomCommand> customCommands;
if(ContextUtils.isGuildAware(event)) {
if(ContextUtils.isGuildKnown(event)) {
customCommands = customCommandService.getCustomCommands(event.getGuild());
} else {
customCommands = customCommandService.getUserCustomCommands(event.getUser());
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.isGuildAware(event)) {
if(ContextUtils.isGuildKnown(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.isGuildAware(model.getEvent())) {
if(ContextUtils.isGuildKnown(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.isNotGuildAware(slashCommandInteractionEvent)) {
if(ContextUtils.isGuildNotKnown(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 @@ -5,6 +5,7 @@
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.UserCommandConfig;
import dev.sheldan.abstracto.core.interaction.ComponentPayloadService;
import dev.sheldan.abstracto.core.interaction.ComponentService;
import dev.sheldan.abstracto.core.interaction.slash.SlashCommandConfig;
Expand All @@ -14,12 +15,17 @@
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.ServerChannelMessage;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.models.template.display.MemberNameDisplay;
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserManagementService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.core.utils.ContextUtils;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.core.utils.ParseUtils;
import dev.sheldan.abstracto.core.utils.SnowflakeUtils;
Expand Down Expand Up @@ -73,6 +79,12 @@ public class Remind extends AbstractConditionableCommand {
@Autowired
private ComponentPayloadService componentPayloadService;

@Autowired
private UserManagementService userManagementService;

@Autowired
private ServerManagementService serverManagementService;

@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
List<Object> parameters = commandContext.getParameters().getParameters();
Expand All @@ -85,7 +97,7 @@ public CompletableFuture<CommandResult> executeAsync(CommandContext commandConte
ReminderModel remindModel = ReminderModel
.builder()
.remindText(text)
.memberDisplay(MemberNameDisplay.fromMember(commandContext.getAuthor()))
.userDisplay(UserDisplay.fromUser(commandContext.getAuthor().getUser()))
.joinButtonId(joinButtonId)
.reminder(ReminderDisplay.fromReminder(createdReminder))
.message(ServerChannelMessage.fromMessage(commandContext.getMessage()))
Expand All @@ -111,30 +123,42 @@ public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEven
String durationString = slashCommandParameterService.getCommandOption(DURATION_PARAMETER, event, Duration.class, String.class);
Duration duration = ParseUtils.parseDuration(durationString);
String reminderText = slashCommandParameterService.getCommandOption(REMIND_TEXT_PARAMETER, event, String.class, String.class);
Long serverId = event.getGuild().getIdLong();
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(event.getMember());
Long snowFlake = SnowflakeUtils.createSnowFlake();
String joinButtonId = componentService.generateComponentId();
Reminder createdReminder = remindService.createReminderInForUser(aUserInAServer, reminderText, duration, event.getChannel().getIdLong(), snowFlake);
String joinButtonId;
Reminder createdReminder;
if(!ContextUtils.isUserCommand(event)) {
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()
.remindText(reminderText)
.joinButtonId(joinButtonId)
.memberDisplay(MemberNameDisplay.fromMember(event.getMember()))
.userDisplay(UserDisplay.fromUser(event.getUser()))
.reminder(ReminderDisplay.fromReminder(createdReminder))
.build();

JoinReminderPayload payload = JoinReminderPayload
.builder()
.remindedUserId(event.getMember().getIdLong())
.reminderId(createdReminder.getId())
.serverId(serverId)
.build();
if(!ContextUtils.isUserCommand(event)) {
Long serverId = event.getGuild().getIdLong();
AServer server = serverManagementService.loadServer(serverId);
JoinReminderPayload payload = JoinReminderPayload
.builder()
.remindedUserId(event.getUser().getIdLong())
.reminderId(createdReminder.getId())
.serverId(serverId)
.build();
componentPayloadService.createButtonPayload(joinButtonId, payload, REMINDER_JOIN_BUTTON_ORIGIN, server);
}

componentPayloadService.createButtonPayload(joinButtonId, payload, REMINDER_JOIN_BUTTON_ORIGIN, aUserInAServer.getServerReference());

log.info("Notifying user {} about reminder being scheduled.", event.getMember().getId());
MessageToSend messageToSend = templateService.renderEmbedTemplate(REMINDER_EMBED_KEY, remindModel, serverId);

log.info("Notifying user {} about reminder being scheduled.", event.getUser().getId());
MessageToSend messageToSend = templateService.renderEmbedTemplate(REMINDER_EMBED_KEY, remindModel, ContextUtils.serverIdOrNull(event));
return interactionService.replyMessageToSend(messageToSend, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
Expand Down Expand Up @@ -164,6 +188,8 @@ public CommandConfiguration getConfiguration() {
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.userInstallable(true)
.userCommandConfig(UserCommandConfig.all())
.rootCommandName(RemindSlashCommandNames.REMIND)
.commandName("create")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.UserCommandConfig;
import dev.sheldan.abstracto.core.interaction.slash.SlashCommandConfig;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.models.ServerChannelMessage;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.models.template.display.UserDisplay;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserManagementService;
import dev.sheldan.abstracto.core.templating.model.MessageToSend;
import dev.sheldan.abstracto.core.templating.service.TemplateService;
import dev.sheldan.abstracto.core.utils.ContextUtils;
import dev.sheldan.abstracto.core.utils.FutureUtils;
import dev.sheldan.abstracto.remind.config.RemindFeatureDefinition;
import dev.sheldan.abstracto.remind.config.RemindSlashCommandNames;
Expand All @@ -26,6 +30,7 @@
import dev.sheldan.abstracto.remind.service.management.ReminderParticipantManagementService;
import lombok.extern.slf4j.Slf4j;
import net.dv8tion.jda.api.entities.Member;
import net.dv8tion.jda.api.entities.User;
import net.dv8tion.jda.api.events.interaction.command.SlashCommandInteractionEvent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
Expand Down Expand Up @@ -57,16 +62,19 @@ public class Reminders extends AbstractConditionableCommand {
@Autowired
private ReminderParticipantManagementService reminderParticipantManagementService;

@Autowired
private UserManagementService userManagementService;

@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
Long serverId = commandContext.getGuild().getIdLong();
Member member = commandContext.getAuthor();
MessageToSend messageToSend = getMessageToSend(serverId, member);
MessageToSend messageToSend = getServerReminders(serverId, member);
return FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, commandContext.getChannel()))
.thenApply(aVoid -> CommandResult.fromIgnored());
}

private MessageToSend getMessageToSend(Long serverId, Member member) {
private MessageToSend getServerReminders(Long serverId, Member member) {
AUserInAServer aUserInAServer = userInServerManagementService.loadOrCreateUser(member);
List<Reminder> activeReminders = reminderManagementService.getActiveRemindersForUser(aUserInAServer);
List<Reminder> joinedReminders = reminderParticipantManagementService.getActiveReminders(aUserInAServer)
Expand All @@ -85,17 +93,37 @@ private MessageToSend getMessageToSend(Long serverId, Member member) {
RemindersModel model = RemindersModel
.builder()
.reminders(reminders)
.member(member)
.userDisplay(UserDisplay.fromUser(member.getUser()))
.build();
log.info("Showing {} reminders for user {} in server {}.", activeReminders.size(), aUserInAServer.getUserReference().getId(), serverId);
return templateService.renderEmbedTemplate(REMINDERS_RESPONSE_TEMPLATE, model, serverId);
}

private MessageToSend getUserReminders(User user) {
AUser aUser = userManagementService.loadOrCreateUser(user.getIdLong());
List<Reminder> activeReminders = reminderManagementService.getActiveUserRemindersForUser(aUser);
List<ReminderDisplay> reminders = activeReminders
.stream()
.map(ReminderDisplay::fromReminder)
.collect(Collectors.toList());
RemindersModel model = RemindersModel
.builder()
.reminders(reminders)
.userDisplay(UserDisplay.fromUser(user))
.build();
return templateService.renderEmbedTemplate(REMINDERS_RESPONSE_TEMPLATE, model);
}

@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Long serverId = event.getGuild().getIdLong();
Member member = event.getMember();
MessageToSend messageToSend = getMessageToSend(serverId, member);
MessageToSend messageToSend;
if(ContextUtils.isUserCommand(event)) {
messageToSend = getUserReminders(event.getUser());
} else {
Long serverId = event.getGuild().getIdLong();
Member member = event.getMember();
messageToSend = getServerReminders(serverId, member);
}
return interactionService.replyMessageToSend(messageToSend, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
Expand All @@ -110,6 +138,8 @@ public CommandConfiguration getConfiguration() {
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.userInstallable(true)
.userCommandConfig(UserCommandConfig.all())
.rootCommandName(RemindSlashCommandNames.REMIND)
.commandName("list")
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import dev.sheldan.abstracto.core.config.FeatureDefinition;
import dev.sheldan.abstracto.core.interaction.InteractionService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserManagementService;
import dev.sheldan.abstracto.core.utils.ContextUtils;
import dev.sheldan.abstracto.remind.config.RemindFeatureDefinition;
import dev.sheldan.abstracto.remind.config.RemindSlashCommandNames;
import dev.sheldan.abstracto.remind.service.ReminderService;
Expand All @@ -36,6 +38,9 @@ public class UnRemind extends AbstractConditionableCommand {
@Autowired
private UserInServerManagementService userInServerManagementService;

@Autowired
private UserManagementService userManagementService;

@Autowired
private SlashCommandParameterService slashCommandParameterService;

Expand All @@ -52,7 +57,11 @@ public CommandResult execute(CommandContext commandContext) {
@Override
public CompletableFuture<CommandResult> executeSlash(SlashCommandInteractionEvent event) {
Long reminderId = slashCommandParameterService.getCommandOption(REMINDER_ID_PARAMETER, event, Long.class, Integer.class).longValue();
reminderService.unRemind(reminderId, userInServerManagementService.loadOrCreateUser(event.getMember()));
if(ContextUtils.isUserCommand(event)) {
reminderService.unRemind(reminderId, userManagementService.loadOrCreateUser(event.getUser().getIdLong()));
} else {
reminderService.unRemind(reminderId, userInServerManagementService.loadOrCreateUser(event.getMember()));
}
return interactionService.replyEmbed(UN_REMIND_RESPONSE, event)
.thenApply(interactionHook -> CommandResult.fromSuccess());
}
Expand All @@ -74,6 +83,8 @@ public CommandConfiguration getConfiguration() {
SlashCommandConfig slashCommandConfig = SlashCommandConfig
.builder()
.enabled(true)
.userInstallable(true)
.userCommandConfig(UserCommandConfig.all())
.rootCommandName(RemindSlashCommandNames.REMIND)
.commandName("cancel")
.build();
Expand Down
Loading

0 comments on commit 6e01092

Please sign in to comment.