Skip to content

Commit

Permalink
fix hidden roll reveal (#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
twonirwana authored Mar 23, 2024
1 parent f0c35fc commit ee4e258
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 40 deletions.
5 changes: 3 additions & 2 deletions bot/src/main/java/de/janno/discord/bot/Bot.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ public static void main(final String[] args) throws Exception {
CustomParameterCommand customParameterCommand = new CustomParameterCommand(persistenceManager, cachingDiceEvaluator);
SumCustomSetCommand sumCustomSetCommand = new SumCustomSetCommand(persistenceManager, cachingDiceEvaluator);
RpgSystemCommandPreset rpgSystemCommandPreset = new RpgSystemCommandPreset(persistenceManager, customParameterCommand, customDiceCommand, sumCustomSetCommand);

WelcomeCommand welcomeCommand = new WelcomeCommand(persistenceManager, rpgSystemCommandPreset);
HiddenDirectRollCommand hiddenDirectRollCommand = new HiddenDirectRollCommand(persistenceManager, cachingDiceEvaluator);

DiscordConnectorImpl.createAndStart(
List.of(customDiceCommand,
new DirectRollCommand(persistenceManager, cachingDiceEvaluator),
new AliasRollCommand(persistenceManager, cachingDiceEvaluator),
new HiddenDirectRollCommand(persistenceManager, cachingDiceEvaluator),
hiddenDirectRollCommand,
new ValidationCommand(persistenceManager, cachingDiceEvaluator),
new ChannelConfigCommand(persistenceManager),
sumCustomSetCommand,
Expand All @@ -85,6 +85,7 @@ public static void main(final String[] args) throws Exception {
sumCustomSetCommand,
customParameterCommand,
welcomeCommand,
hiddenDirectRollCommand,
//legacy, to be removed
new FateCommand(persistenceManager),
new CountSuccessesCommand(persistenceManager),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ public interface ComponentInteractEventHandler {
Mono<Void> handleComponentInteractEvent(@NonNull ButtonEventAdaptor event);

boolean matchingComponentCustomId(String buttonCustomId);

@NonNull String getCommandId();
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,51 +152,65 @@ private AutoCompleteRequest fromEvent(CommandAutoCompleteInteractionEvent event)
public void onSlashCommandInteraction(@NonNull SlashCommandInteractionEvent event) {
log.trace("ChatInputEvent: {} from {}", event.getInteraction().getCommandId(),
event.getInteraction().getUser().getName());
Flux.fromIterable(slashCommands)
List<SlashCommand> matchingHandler = slashCommands.stream()
.filter(command -> command.getCommandId().equals(event.getName()))
.next()
.flatMap(command -> {
Locale userLocale = LocaleConverter.toLocale(event.getInteraction().getUserLocale());
JdaMetrics.userLocalInteraction(userLocale);
return command.handleSlashCommandEvent(new SlashEventAdapterImpl(event,
new Requester(event.getInteraction().getUser().getName(),
event.getChannel().getName(),
Optional.ofNullable(event.getGuild()).map(Guild::getName).orElse(""),
event.getJDA().getShardInfo().getShardString(),
userLocale)
), UUID::randomUUID, LocaleConverter.toLocale(event.getUserLocale()));
})
.onErrorResume(e -> {
log.error("SlashCommandEvent Exception: ", e);
return Mono.empty();
})
.subscribeOn(scheduler)
.subscribe();
.toList();

Locale userLocale = LocaleConverter.toLocale(event.getInteraction().getUserLocale());

Requester requester = new Requester(event.getInteraction().getUser().getName(),
event.getChannel().getName(),
Optional.ofNullable(event.getInteraction().getGuild()).map(Guild::getName).orElse(""),
event.getJDA().getShardInfo().getShardString(),
userLocale);
if (matchingHandler.size() != 1) {
log.error("{}: Invalid handler for {} -> {}", requester, event.getInteraction().getCommandId(), matchingHandler.stream().map(SlashCommand::getCommandId).toList());
} else {
Mono.just(matchingHandler.getFirst())
.flatMap(command -> {

JdaMetrics.userLocalInteraction(userLocale);
return command.handleSlashCommandEvent(new SlashEventAdapterImpl(event,
requester
), UUID::randomUUID, LocaleConverter.toLocale(event.getUserLocale()));
})
.onErrorResume(e -> {
log.error("SlashCommandEvent Exception: ", e);
return Mono.empty();
})
.subscribeOn(scheduler)
.subscribe();
}
}

@Override
public void onButtonInteraction(@NonNull ButtonInteractionEvent event) {
log.trace("ComponentEvent: {} from {}", event.getInteraction().getComponentId(), event.getInteraction().getUser().getName());
Flux.fromIterable(componentInteractEventHandlers)
List<ComponentInteractEventHandler> matchingHandler = componentInteractEventHandlers.stream()
.filter(command -> command.matchingComponentCustomId(event.getInteraction().getComponentId()))
.next()
.flatMap(command -> {
Locale userLocale = LocaleConverter.toLocale(event.getInteraction().getUserLocale());
JdaMetrics.userLocalInteraction(userLocale);
return command.handleComponentInteractEvent(new ButtonEventAdapterImpl(event,
new Requester(event.getInteraction().getUser().getName(),
event.getChannel().getName(),
Optional.ofNullable(event.getInteraction().getGuild()).map(Guild::getName).orElse(""),
event.getJDA().getShardInfo().getShardString(),
userLocale
)));
})
.onErrorResume(e -> {
log.error("ButtonInteractEvent Exception: ", e);
return Mono.empty();
})
.subscribeOn(scheduler)
.subscribe();
.toList();
Locale userLocale = LocaleConverter.toLocale(event.getInteraction().getUserLocale());

Requester requester = new Requester(event.getInteraction().getUser().getName(),
event.getChannel().getName(),
Optional.ofNullable(event.getInteraction().getGuild()).map(Guild::getName).orElse(""),
event.getJDA().getShardInfo().getShardString(),
userLocale);
if (matchingHandler.size() != 1) {
log.error("{}: Invalid handler for {} -> {}", requester, event.getInteraction().getComponentId(), matchingHandler.stream().map(ComponentInteractEventHandler::getCommandId).toList());
} else {
Mono.just(matchingHandler.getFirst())
.flatMap(command -> {
JdaMetrics.userLocalInteraction(userLocale);
return command.handleComponentInteractEvent(new ButtonEventAdapterImpl(event, requester));
})
.onErrorResume(e -> {
log.error("ButtonInteractEvent Exception: ", e);
return Mono.empty();
})
.subscribeOn(scheduler)
.subscribe();
}
}
}
)
Expand Down

0 comments on commit ee4e258

Please sign in to comment.