-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AB-xxx] adding generic input format exception handling
adding level_action to send a message to a channel once a level is reached
- Loading branch information
Showing
7 changed files
with
169 additions
and
3 deletions.
There are no files selected for viewing
99 changes: 99 additions & 0 deletions
99
.../main/java/dev/sheldan/abstracto/experience/listener/SendMessageToChannelLevelAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
package dev.sheldan.abstracto.experience.listener; | ||
|
||
import com.google.gson.Gson; | ||
import dev.sheldan.abstracto.core.exception.InputFormatException; | ||
import dev.sheldan.abstracto.core.models.ServerUser; | ||
import dev.sheldan.abstracto.core.models.template.display.MemberDisplay; | ||
import dev.sheldan.abstracto.core.service.ChannelService; | ||
import dev.sheldan.abstracto.core.service.MemberService; | ||
import dev.sheldan.abstracto.core.templating.model.MessageToSend; | ||
import dev.sheldan.abstracto.core.templating.service.TemplateService; | ||
import dev.sheldan.abstracto.core.utils.FutureUtils; | ||
import dev.sheldan.abstracto.core.utils.ParseUtils; | ||
import dev.sheldan.abstracto.experience.model.LevelActionPayload; | ||
import dev.sheldan.abstracto.experience.model.SendMessageToChannelLevelActionMessageModel; | ||
import dev.sheldan.abstracto.experience.model.SendMessageToChannelLevelActionPayload; | ||
import dev.sheldan.abstracto.experience.model.database.AUserExperience; | ||
import dev.sheldan.abstracto.experience.model.database.LevelAction; | ||
import lombok.extern.slf4j.Slf4j; | ||
import net.dv8tion.jda.api.entities.Guild; | ||
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel; | ||
import net.dv8tion.jda.api.entities.channel.middleman.GuildMessageChannel; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
public class SendMessageToChannelLevelAction implements LevelActionListener { | ||
|
||
public static final String ACTION_NAME = "send_message_to_channel_above_level"; | ||
private static final String LEVEL_ACTION_SEND_MESSAGE_TEMPLATE_KEY = "levelAction_sendMessageToChannel_template"; | ||
|
||
@Autowired | ||
private Gson gson; | ||
|
||
@Autowired | ||
private ChannelService channelService; | ||
|
||
@Autowired | ||
private TemplateService templateService; | ||
|
||
@Autowired | ||
private MemberService memberService; | ||
|
||
@Override | ||
public String getName() { | ||
return ACTION_NAME; | ||
} | ||
|
||
@Override | ||
public void apply(AUserExperience userExperience, LevelAction levelAction, MemberActionModification container) { | ||
SendMessageToChannelLevelActionPayload payload = (SendMessageToChannelLevelActionPayload) levelAction.getLoadedPayload(); | ||
SendMessageToChannelLevelActionMessageModel.SendMessageToChannelLevelActionMessageModelBuilder messageModelBuilder = SendMessageToChannelLevelActionMessageModel | ||
.builder() | ||
.level(userExperience.getLevelOrDefault()) | ||
.templateKey(payload.getTemplateKey()) | ||
.experience(userExperience.getExperience()); | ||
ServerUser serverUser = ServerUser.fromAUserInAServer(userExperience.getUser()); | ||
memberService.getMemberInServerAsync(serverUser).thenAccept(member -> { | ||
messageModelBuilder.memberDisplay(MemberDisplay.fromMember(member)); | ||
SendMessageToChannelLevelActionMessageModel model = messageModelBuilder.build(); | ||
MessageToSend messageToSend = templateService.renderEmbedTemplate(LEVEL_ACTION_SEND_MESSAGE_TEMPLATE_KEY, model, serverUser.getServerId()); | ||
GuildMessageChannel targetChannel = channelService.getMessageChannelFromServer(serverUser.getServerId(), payload.getChannelId()); | ||
FutureUtils.toSingleFutureGeneric(channelService.sendMessageToSendToChannel(messageToSend, targetChannel)).thenAccept(unused -> { | ||
log.info("Send message to channel action sent a message to channel {} for user {} in server {}.", payload.getChannelId(), serverUser.getUserId(), serverUser.getServerId()); | ||
}).exceptionally(throwable -> { | ||
log.warn("Send message to channel action failed to send a message to channel {} for user {} in server {}.", payload.getChannelId(), serverUser.getUserId(), serverUser.getServerId(), throwable); | ||
return null; | ||
}); | ||
}).exceptionally(throwable -> { | ||
log.warn("Failed to load member {} in server {} for send message level action towards channel {}.", serverUser.getUserId(), serverUser.getServerId(), payload.getChannelId()); | ||
return null; | ||
}); | ||
} | ||
|
||
@Override | ||
public boolean shouldExecute(AUserExperience aUserExperience, LevelAction levelAction) { | ||
return aUserExperience.getLevelOrDefault() >= levelAction.getLevel().getLevel(); | ||
} | ||
|
||
@Override | ||
public void prepareAction(LevelAction levelAction) { | ||
levelAction.setLoadedPayload(gson.fromJson(levelAction.getPayload(), SendMessageToChannelLevelActionPayload.class)); | ||
} | ||
|
||
@Override | ||
public LevelActionPayload createPayload(Guild guild, String input) { | ||
if(!input.contains(";")) { | ||
throw new InputFormatException(input, "<#channel>;template_key"); | ||
} | ||
String channelPart = input.substring(0, input.indexOf(";")); | ||
GuildChannel channel = ParseUtils.parseGuildChannelFromText(channelPart, guild); | ||
String templateKey = input.substring(input.indexOf(";") + 1); | ||
return SendMessageToChannelLevelActionPayload | ||
.builder() | ||
.channelId(channel.getIdLong()) | ||
.templateKey(templateKey) | ||
.build(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
...a/dev/sheldan/abstracto/experience/model/SendMessageToChannelLevelActionMessageModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dev.sheldan.abstracto.experience.model; | ||
|
||
import dev.sheldan.abstracto.core.models.template.display.MemberDisplay; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class SendMessageToChannelLevelActionMessageModel implements LevelActionPayload { | ||
private MemberDisplay memberDisplay; | ||
private Integer level; | ||
private Long experience; | ||
private String templateKey; | ||
} |
11 changes: 11 additions & 0 deletions
11
...n/java/dev/sheldan/abstracto/experience/model/SendMessageToChannelLevelActionPayload.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dev.sheldan.abstracto.experience.model; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
|
||
@Getter | ||
@Builder | ||
public class SendMessageToChannelLevelActionPayload implements LevelActionPayload { | ||
private Long channelId; | ||
private String templateKey; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...ore/core-int/src/main/java/dev/sheldan/abstracto/core/exception/InputFormatException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package dev.sheldan.abstracto.core.exception; | ||
|
||
import dev.sheldan.abstracto.core.models.exception.InputFormatExceptionModel; | ||
import dev.sheldan.abstracto.core.templating.Templatable; | ||
|
||
public class InputFormatException extends AbstractoRunTimeException implements Templatable { | ||
|
||
private final InputFormatExceptionModel model; | ||
|
||
|
||
public InputFormatException(String wrongFormat, String validFormat) { | ||
super("Input format exception "); | ||
this.model = InputFormatExceptionModel | ||
.builder() | ||
.invalidFormat(wrongFormat) | ||
.validFormat(validFormat) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public String getTemplateName() { | ||
return "input_invalid_format_exception"; | ||
} | ||
|
||
@Override | ||
public Object getTemplateModel() { | ||
return model; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
.../src/main/java/dev/sheldan/abstracto/core/models/exception/InputFormatExceptionModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package dev.sheldan.abstracto.core.models.exception; | ||
|
||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import java.io.Serializable; | ||
|
||
@Setter | ||
@Getter | ||
@Builder | ||
public class InputFormatExceptionModel implements Serializable { | ||
private final String invalidFormat; | ||
private final String validFormat; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters