Skip to content

Commit

Permalink
Add getUser() method to Interaction events (#1217)
Browse files Browse the repository at this point in the history
* Add getUser() method to Interaction events

* Update ExampleInteractions

* Adjust implementation

* Add documentation
  • Loading branch information
jasonmokk committed May 23, 2024
1 parent 27aa1e0 commit d23707f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import discord4j.core.event.domain.Event;
import discord4j.core.object.command.Interaction;
import discord4j.core.object.entity.Message;
import discord4j.core.object.entity.User;
import discord4j.discordjson.json.*;
import discord4j.discordjson.possible.Possible;
import discord4j.gateway.ShardInfo;
Expand Down Expand Up @@ -73,6 +74,16 @@ public Interaction getInteraction() {
return interaction;
}

/**
* Gets the {@link discord4j.core.object.entity.User} associated with the event.
* The User is retrieved from the {@link #getInteraction} method.
*
* @return The {@link discord4j.core.object.entity.User} associated with the event.
*/
public User getUser() {
return interaction.getUser();
}

@Deprecated
protected Mono<Void> createInteractionResponse(InteractionResponseType responseType,
@Nullable InteractionApplicationCommandCallbackData data) {
Expand Down
4 changes: 2 additions & 2 deletions core/src/test/java/discord4j/core/ExampleInteractions.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public static void main(String[] args) {

Publisher<?> onSelect = client.on(SelectMenuInteractionEvent.class, event -> {
if (REPLY_MODE_SELECT.equals(event.getCustomId())) {
Snowflake user = event.getInteraction().getUser().getId();
Snowflake user = event.getUser().getId();
String selected = event.getValues().get(0);
String previous = modeByUser.put(user, selected);
if (previous == null) {
Expand All @@ -134,7 +134,7 @@ public static void main(String[] args) {
// create a listener that handles the button click
Publisher<?> onButton = client.on(ButtonInteractionEvent.class, event -> {
if (ACTION_BUTTON.equals(event.getCustomId())) {
Snowflake user = event.getInteraction().getUser().getId();
Snowflake user = event.getUser().getId();
String mode = modeByUser.getOrDefault(user, DEFER_REPLY);
switch (mode) {
case DEFER_REPLY:
Expand Down

0 comments on commit d23707f

Please sign in to comment.