added reminders command

fixed embedbuilder list not containing an initial embed builder
This commit is contained in:
Sheldan
2020-04-08 16:22:58 +02:00
parent c30d7aab92
commit ef8dcb61d9
9 changed files with 119 additions and 6 deletions

View File

@@ -0,0 +1,60 @@
package dev.sheldan.abstracto.utility.commands.remind;
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.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.execution.ContextConverter;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.templating.model.MessageToSend;
import dev.sheldan.abstracto.templating.service.TemplateService;
import dev.sheldan.abstracto.utility.Utility;
import dev.sheldan.abstracto.utility.config.UtilityFeatures;
import dev.sheldan.abstracto.utility.models.database.Reminder;
import dev.sheldan.abstracto.utility.models.template.commands.reminder.RemindersModel;
import dev.sheldan.abstracto.utility.service.management.ReminderManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class Reminders extends AbstractConditionableCommand {
@Autowired
private ReminderManagementService reminderManagementService;
@Autowired
private ChannelService channelService;
@Autowired
private TemplateService templateService;
@Override
public CommandResult execute(CommandContext commandContext) {
List<Reminder> activeReminders = reminderManagementService.getActiveRemindersForUser(commandContext.getUserInitiatedContext().getAUserInAServer());
RemindersModel model = (RemindersModel) ContextConverter.fromCommandContext(commandContext, RemindersModel.class);
model.setReminders(activeReminders);
MessageToSend messageToSend = templateService.renderEmbedTemplate("reminders_response", model);
channelService.sendMessageToEndInTextChannel(messageToSend, commandContext.getChannel());
return CommandResult.fromSuccess();
}
@Override
public CommandConfiguration getConfiguration() {
HelpInfo helpInfo = HelpInfo.builder().longHelp("Shows the current active reminders").usage("reminders").build();
return CommandConfiguration.builder()
.name("reminders")
.module(Utility.UTILITY)
.description("Shows the current active reminders")
.causesReaction(true)
.help(helpInfo)
.build();
}
@Override
public String getFeature() {
return UtilityFeatures.REMIND;
}
}

View File

@@ -1,8 +1,11 @@
package dev.sheldan.abstracto.utility.repository;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.utility.models.database.Reminder;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ReminderRepository extends JpaRepository<Reminder, Long> {
import java.util.List;
public interface ReminderRepository extends JpaRepository<Reminder, Long> {
List<Reminder> getByRemindedUserAndRemindedFalse(AUserInAServer aUserInAServer);
}

View File

@@ -97,7 +97,7 @@ public class RemindServiceBean implements ReminderService {
Optional<TextChannel> channelToAnswerIn = bot.getTextChannelFromServer(server.getId(), channel.getId());
// only send the message if the channel still exists, if not, only set the reminder to reminded.
if(channelToAnswerIn.isPresent()) {
AUser userReference = reminderToRemindFor.getToBeReminded().getUserReference();
AUser userReference = reminderToRemindFor.getRemindedUser().getUserReference();
Member memberInServer = bot.getMemberInServer(server.getId(), userReference.getId());
ExecutedReminderModel build = ExecutedReminderModel
.builder()

View File

@@ -1,12 +1,14 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.models.AServerAChannelAUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.utility.models.database.Reminder;
import dev.sheldan.abstracto.utility.repository.ReminderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.List;
@Component
public class ReminderManagementServiceBean implements ReminderManagementService {
@@ -19,7 +21,7 @@ public class ReminderManagementServiceBean implements ReminderManagementService
Reminder reminder = Reminder.builder()
.channel(userToBeReminded.getChannel())
.server(userToBeReminded.getGuild())
.toBeReminded(userToBeReminded.getAUserInAServer())
.remindedUser(userToBeReminded.getAUserInAServer())
.reminded(false)
.text(text)
.reminderDate(Instant.now())
@@ -42,4 +44,9 @@ public class ReminderManagementServiceBean implements ReminderManagementService
reminderRepository.save(reminder);
}
@Override
public List<Reminder> getActiveRemindersForUser(AUserInAServer aUserInAServer) {
return reminderRepository.getByRemindedUserAndRemindedFalse(aUserInAServer);
}
}

View File

@@ -0,0 +1,23 @@
{
"author": {
"name": "${member.effectiveName}",
"avatar": "${member.user.effectiveAvatarUrl}"
},
"color" : {
"r": 200,
"g": 0,
"b": 255
},
"title": {
"title": "Currently active reminders"
},
"fields": [
<#list reminders as reminder>
{
"name": "Reminder ${reminder.id}",
"value": "Due on ${reminder.targetDate} with text ${reminder.text}"
}
<#sep>,
</#list>
]
}

View File

@@ -25,7 +25,7 @@ public class Reminder {
@Getter
@ManyToOne
@JoinColumn(name = "remindedUser")
private AUserInAServer toBeReminded;
private AUserInAServer remindedUser;
@Getter
private Long messageId;

View File

@@ -0,0 +1,16 @@
package dev.sheldan.abstracto.utility.models.template.commands.reminder;
import dev.sheldan.abstracto.core.models.context.UserInitiatedServerContext;
import dev.sheldan.abstracto.utility.models.database.Reminder;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import java.util.List;
@Getter
@Setter
@SuperBuilder
public class RemindersModel extends UserInitiatedServerContext {
private List<Reminder> reminders;
}

View File

@@ -1,12 +1,15 @@
package dev.sheldan.abstracto.utility.service.management;
import dev.sheldan.abstracto.core.models.AServerAChannelAUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.utility.models.database.Reminder;
import java.time.Instant;
import java.util.List;
public interface ReminderManagementService {
Reminder createReminder(AServerAChannelAUser userToBeReminded, String text, Instant timeToBeRemindedAt, Long messageId);
Reminder loadReminder(Long reminderId);
void setReminded(Reminder reminder);
List<Reminder> getActiveRemindersForUser(AUserInAServer aUserInAServer);
}

View File

@@ -44,6 +44,7 @@ public class TemplateServiceBean implements TemplateService {
public MessageToSend renderEmbedTemplate(String key, Object model) {
String embedConfig = this.renderTemplate(key + "_embed", model);
List<EmbedBuilder> embedBuilders = new ArrayList<>();
embedBuilders.add(new EmbedBuilder());
EmbedConfiguration configuration = gson.fromJson(embedConfig, EmbedConfiguration.class);
String description = configuration.getDescription();
if(description != null) {
@@ -116,8 +117,8 @@ public class TemplateServiceBean implements TemplateService {
return FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(key), parameters);
} catch (IOException | TemplateException e) {
log.warn("Failed to render template. ", e);
throw new RuntimeException(e);
}
return "";
}
@Override
@@ -126,7 +127,7 @@ public class TemplateServiceBean implements TemplateService {
return FreeMarkerTemplateUtils.processTemplateIntoString(configuration.getTemplate(key), model);
} catch (IOException | TemplateException e) {
log.warn("Failed to render template. ", e);
return null;
throw new RuntimeException(e);
}
}
}