Skip to content

Commit

Permalink
Fixed ephemeral messages preventing event finalization.
Browse files Browse the repository at this point in the history
  • Loading branch information
ygimenez committed Aug 14, 2023
1 parent ca0e558 commit 59d0d05
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 31 deletions.
33 changes: 16 additions & 17 deletions src/main/java/com/github/ygimenez/listener/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,12 @@ private void execute(GenericMessageReactionEvent evt) {
return;
}

evt.retrieveUser().submit()
.whenComplete((u, t) -> processEvent(
evt.retrieveMessage().submit().whenComplete((m, t) ->
evt.retrieveUser().submit().whenComplete((u, thr) -> processEvent(
t, id, u,
new PaginationEventWrapper(evt, u, evt.getChannel(), evt.getMessageId(), evt.getReaction(), evt.isFromGuild())
));
new PaginationEventWrapper(evt, u, evt.getChannel(), m, evt.getReaction(), evt.isFromGuild())
))
);
}

@Override
Expand All @@ -157,14 +158,13 @@ public void onButtonInteraction(@NotNull ButtonInteractionEvent evt) {
return;
}

evt.deferEdit().submit()
.whenComplete((hook, t) -> {
User u = hook.getInteraction().getUser();
processEvent(
t, id, u,
new PaginationEventWrapper(evt, u, evt.getChannel(), evt.getMessageId(), evt.getButton(), evt.isFromGuild())
);
});
evt.deferEdit().submit().whenComplete((hook, t) -> {
User u = hook.getInteraction().getUser();
processEvent(
t, id, u,
new PaginationEventWrapper(evt, u, evt.getChannel(), evt.getMessage(), evt.getButton(), evt.isFromGuild())
);
});
}

private void processEvent(Throwable t, String id, User u, PaginationEventWrapper evt) {
Expand Down Expand Up @@ -207,11 +207,10 @@ public void onGenericSelectMenuInteraction(@NotNull GenericSelectMenuInteraction
return;
}

evt.deferEdit().submit()
.whenComplete((hook, t) ->
dropdownValues.computeIfAbsent(id, k -> new HashMap<>())
.put(evt.getComponentId(), evt.getValues())
);
evt.deferEdit().submit().whenComplete((hook, t) ->
dropdownValues.computeIfAbsent(id, k -> new HashMap<>())
.put(evt.getComponentId(), evt.getValues())
);
}

public Map<String, List<?>> getDropdownValues(String eventId) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/com/github/ygimenez/method/Pages.java
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ public static ActionReference paginate(@NotNull Message msg, @NotNull PaginateHe

@Override
public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrapper) {
Message m = subGet(wrapper.retrieveMessage());
Message m = wrapper.retrieveMessage();

if (helper.canInteract(u)) {
if (u.isBot() || m == null || !wrapper.getMessageId().equals(msg.getId())) return;
Expand Down Expand Up @@ -865,7 +865,7 @@ public static ActionReference categorize(@NotNull Message msg, @NotNull Categori

@Override
public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrapper) {
Message m = subGet(wrapper.retrieveMessage());
Message m = wrapper.retrieveMessage();

if (helper.canInteract(u)) {
if (u.isBot() || m == null || !wrapper.getMessageId().equals(msg.getId())) return;
Expand Down Expand Up @@ -1174,7 +1174,7 @@ public static ActionReference buttonize(@NotNull Message msg, @NotNull Buttonize

@Override
public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrapper) {
Message m = subGet(wrapper.retrieveMessage());
Message m = wrapper.retrieveMessage();

if (helper.canInteract(u)) {
if (u.isBot() || m == null || !wrapper.getMessageId().equals(msg.getId())) return;
Expand Down Expand Up @@ -1513,7 +1513,7 @@ public static ActionReference lazyPaginate(@NotNull Message msg, @NotNull LazyPa

@Override
public void acceptThrows(@NotNull User u, @NotNull PaginationEventWrapper wrapper) {
Message m = subGet(wrapper.retrieveMessage());
Message m = wrapper.retrieveMessage();

if (helper.canInteract(u)) {
if (u.isBot() || m == null || !wrapper.getMessageId().equals(msg.getId())) return;
Expand Down
24 changes: 14 additions & 10 deletions src/main/java/com/github/ygimenez/model/PaginationEventWrapper.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.ygimenez.model;

import com.github.ygimenez.method.Pages;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageReaction;
Expand All @@ -9,7 +10,6 @@
import net.dv8tion.jda.api.events.message.react.GenericMessageReactionEvent;
import net.dv8tion.jda.api.interactions.InteractionHook;
import net.dv8tion.jda.api.interactions.components.buttons.Button;
import net.dv8tion.jda.api.requests.RestAction;
import org.jetbrains.annotations.Nullable;

/**
Expand All @@ -19,7 +19,7 @@ public class PaginationEventWrapper {
private final Object source;
private final User user;
private final MessageChannel channel;
private final String messageId;
private final Message message;
private final Object content;
private final InteractionHook hook;
private final boolean isFromGuild;
Expand All @@ -30,11 +30,11 @@ public class PaginationEventWrapper {
* @param source The source event, will be either a {@link GenericMessageReactionEvent} or a {@link ButtonInteractionEvent}.
* @param user The {@link User} who triggered the event.
* @param channel The {@link MessageChannel} where the event happened.
* @param messageId The {@link Message} ID.
* @param message The {@link Message}.
* @param content The button which was pressed, will be either a {@link MessageReaction} or a {@link Button}.
* @param isFromGuild Whether the event happened on a {@link Guild} or not.
*/
public PaginationEventWrapper(Object source, User user, MessageChannel channel, String messageId, Object content, boolean isFromGuild) {
public PaginationEventWrapper(Object source, User user, MessageChannel channel, Message message, Object content, boolean isFromGuild) {
if (source instanceof ButtonInteractionEvent) {
hook = ((ButtonInteractionEvent) source).getHook();
} else {
Expand All @@ -44,7 +44,7 @@ public PaginationEventWrapper(Object source, User user, MessageChannel channel,
this.source = source;
this.user = user;
this.channel = channel;
this.messageId = messageId;
this.message = message;
this.content = content;
this.isFromGuild = isFromGuild;
}
Expand Down Expand Up @@ -73,16 +73,20 @@ public User getUser() {
* @return The {@link Message} ID.
*/
public String getMessageId() {
return messageId;
return message.getId();
}

/**
* Fetch the {@link Message} from the event's {@link Message} ID.
* Retrieves the {@link Message} from the event. Will not fetch the new state if the message is ephemeral.
*
* @return The {@link RestAction} for retrieving the {@link Message}.
* @return The {@link Message}.
*/
public RestAction<Message> retrieveMessage() {
return channel.retrieveMessageById(messageId);
public Message retrieveMessage() {
if (message.isEphemeral()) {
return message;
}

return Pages.subGet(channel.retrieveMessageById(message.getId()));
}

/**
Expand Down

0 comments on commit 59d0d05

Please sign in to comment.