Skip to content

Commit

Permalink
Improve welcome command and update grade (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
twonirwana authored Sep 29, 2024
1 parent 671a32e commit f4bedb6
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 33 deletions.
17 changes: 10 additions & 7 deletions bot/src/main/java/de/janno/discord/bot/BaseCommandUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@

public class BaseCommandUtils {

public static MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID,
@Nullable Long guildId,
long channelId,
long messageId,
String commandId,
PersistenceManager persistenceManager) {
/**
* Create a new message data with empty state, delete other states for this and save it
*/
public static MessageDataDTO createCleanupAndSaveEmptyMessageData(@NonNull UUID configUUID,
@Nullable Long guildId,
long channelId,
long messageId,
String commandId,
PersistenceManager persistenceManager) {
MessageDataDTO messageDataDTO = new MessageDataDTO(configUUID, guildId, channelId, messageId, commandId, Mapper.NO_PERSISTED_STATE, null);
//should not be needed but sometimes there is a retry ect and then there is already a state
//should not be needed but sometimes there is a retry and then there is already a state
persistenceManager.deleteStateForMessage(channelId, messageId);
persistenceManager.saveMessageData(messageDataDTO);
return messageDataDTO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ protected boolean shouldKeepExistingButtonMessage(@NonNull ButtonEventAdaptor ev
return AbstractCommand.this.getCurrentMessageContentChange(config, state);
}

@Override
public @NonNull MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID, @Nullable Long guildId, long channelId, long messageId) {
return AbstractCommand.this.createEmptyMessageData(configUUID, guildId, channelId, messageId);
}
};
slashCommand = new AbstractSlashCommand<>(persistenceManager) {
@Override
Expand Down Expand Up @@ -152,6 +156,11 @@ protected Collection<CommandDefinitionOption> additionalCommandOptions() {
protected @NonNull Optional<String> getStartOptionsValidationMessage(@NonNull CommandInteractionOption options, long channelId, long userId, @NonNull Locale userLocale) {
return AbstractCommand.this.getStartOptionsValidationMessage(options, channelId, userId, userLocale);
}

@Override
public @NonNull MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID, @Nullable Long guildId, long channelId, long messageId) {
return AbstractCommand.this.createEmptyMessageData(configUUID, guildId, channelId, messageId);
}
};
}

Expand Down Expand Up @@ -199,11 +208,11 @@ protected abstract ConfigAndState<C, S> getMessageDataAndUpdateWithButtonValue(@
* On the creation of a message an empty state need to be saved so we know the message exists and we can remove it later, even on concurrent actions
*/
@VisibleForTesting
public MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID,
public @NonNull MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID,
@Nullable Long guildId,
long channelId,
long messageId) {
return BaseCommandUtils.createEmptyMessageData(configUUID, guildId, channelId, messageId, getCommandId(), persistenceManager);
return BaseCommandUtils.createCleanupAndSaveEmptyMessageData(configUUID, guildId, channelId, messageId, getCommandId(), persistenceManager);
}

//visible for welcome command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,8 @@ public final Mono<Void> handleComponentInteractEvent(@NonNull ButtonEventAdaptor
if (newButtonMessage.isPresent() && answerTargetChannelId == null) {
actions.add(Mono.defer(() -> event.sendMessage(newButtonMessage.get()))
.doOnNext(newMessageId -> createEmptyMessageData(configUUID, guildId, channelId, newMessageId))
.flatMap(newMessageId -> MessageDeletionHelper.deleteOldMessageAndData(persistenceManager, newMessageId, event.getMessageId(), configUUID, channelId, event))
.delaySubscription(calculateDelay(event))
.doOnSuccess(v -> {
.doOnNext(newMessageId -> {
//delete has a delays therefore we write the metrics after the message creation
BotMetrics.timerNewButtonMessageMetricCounter(getCommandId(), stopwatch.elapsed());
if (answer.isEmpty()) {
log.info("{}: '{}'={} in {}ms",
Expand All @@ -162,6 +161,8 @@ public final Mono<Void> handleComponentInteractEvent(@NonNull ButtonEventAdaptor
);
}
})
.flatMap(newMessageId -> MessageDeletionHelper.deleteOldMessageAndData(persistenceManager, newMessageId, event.getMessageId(), configUUID, channelId, event))
.delaySubscription(calculateDelay(event))
.then());
deleteCurrentButtonMessage = !keepExistingButtonMessage;
} else {
Expand Down Expand Up @@ -240,7 +241,7 @@ public MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID,
@Nullable Long guildId,
long channelId,
long messageId) {
return BaseCommandUtils.createEmptyMessageData(configUUID, guildId, channelId, messageId, getCommandId(), persistenceManager);
return BaseCommandUtils.createCleanupAndSaveEmptyMessageData(configUUID, guildId, channelId, messageId, getCommandId(), persistenceManager);
}

protected abstract @NonNull Optional<RollAnswer> getAnswer(C config, State<S> state, long channelId, long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ protected Collection<CommandDefinitionOption> additionalCommandOptions() {
/**
* On the creation of a message an empty state need to be saved so we know the message exists and we can remove it later, even on concurrent actions
*/
protected MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID,
@Nullable Long guildId,
long channelId,
long messageId) {
return BaseCommandUtils.createEmptyMessageData(configUUID, guildId, channelId, messageId, getCommandId(), persistenceManager);
protected @NonNull MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID,
@Nullable Long guildId,
long channelId,
long messageId) {
return BaseCommandUtils.createCleanupAndSaveEmptyMessageData(configUUID, guildId, channelId, messageId, getCommandId(), persistenceManager);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import de.janno.discord.connector.api.DiscordAdapter;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import javax.annotation.Nullable;
import reactor.core.publisher.Mono;

import javax.annotation.Nullable;
import java.time.Duration;
import java.util.List;
import java.util.Objects;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,16 @@ protected EmbedOrMessageDefinition getHelpMessage(Locale userLocale) {
//ignore warning, no good way to display it
Mono<Void> answerMono = Mono.defer(() -> event.replyWithEmbedOrMessageDefinition(RollAnswerConverter.toEmbedOrMessageDefinition(answer), false));
return answerMono
.doOnSuccess(v ->
log.info("{}: '{}'={} -> {} in {}ms",
event.getRequester().toLogString(),
commandString.replace("`", ""),
diceExpression,
answer.toShortString(),
stopwatch.elapsed(TimeUnit.MILLISECONDS)
));
.doOnSuccess(v -> {
BotMetrics.timerAnswerMetricCounter(getCommandId(), stopwatch.elapsed());
log.info("{}: '{}'={} -> {} in {}ms",
event.getRequester().toLogString(),
commandString.replace("`", ""),
diceExpression,
answer.toShortString(),
stopwatch.elapsed(TimeUnit.MILLISECONDS)
);
});
}

private Mono<Void> replyValidationMessage(@NonNull SlashEventAdaptor event, @NonNull String validationMessage, @NonNull String commandString) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,18 @@ public Optional<MessageConfigDTO> createMessageConfig(@NonNull UUID configUUID,
}

BotMetrics.incrementButtonMetricCounter(COMMAND_NAME);
UUID newConfigUUID = uuidSupplier.get();

final Optional<RpgSystemCommandPreset.PresetId> presetId = getPresetIdFromButton(state.getButtonValue());
return presetId.flatMap(id -> rpgSystemCommandPreset.createMessage(id, newConfigUUID, guildId, channelId, config.getConfigLocale()));
return getPresetIdFromButton(state.getButtonValue())
.flatMap(id -> rpgSystemCommandPreset.createMessage(id, uuidSupplier.get(), guildId, channelId, config.getConfigLocale()));

}

@Override
public @NonNull MessageDataDTO createEmptyMessageData(@NonNull UUID configUUID, @Nullable Long guildId, long channelId, long messageId) {
//there should be no saved message data for the welcome message, the created new button messages will create their own message data upon first interaction
return new MessageDataDTO(configUUID, guildId, channelId, messageId, getCommandId(), Mapper.NO_PERSISTED_STATE, null);
}

private Optional<RpgSystemCommandPreset.PresetId> getPresetIdFromButton(String buttonValue) {
if (RpgSystemCommandPreset.PresetId.isValid(buttonValue)) {
return Optional.of(RpgSystemCommandPreset.PresetId.valueOf(buttonValue));
Expand Down
2 changes: 0 additions & 2 deletions discord-connector/api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ dependencies {
implementation(libs.guava)
implementation(libs.commons.lang3)
implementation(libs.emoji)
implementation("org.jetbrains:annotations:25.0.0")

compileOnly(libs.lombok)
annotationProcessor(libs.lombok)

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down

0 comments on commit f4bedb6

Please sign in to comment.