Together-Java/TJ-Plays

Only the original player can interact

Taz03 opened this issue · 6 comments

Taz03 commented

Right now anyone can interact with a 2048 game, make it so that only the user who started the game can interact with it.

Implementation wise, pass the id of user who started the game to button and check weather it is still the same user interacting, otherwise just ignore the interaction or send a ephemeral message saying "You can't interact with this game" or similar.

Add user id here after command name

private MessageCreateData gameMessage(Renderer2048 gameRenderer) {
Button resetButton = Button.success(COMMAND_NAME + " reset", Emoji.fromUnicode("🔃"));
Button upButton = Button.primary(COMMAND_NAME + " up", Emoji.fromUnicode("⬆️"));
Button deleteButton = Button.danger(COMMAND_NAME + " delete", Emoji.fromUnicode("🗑️"));
Button leftButton = Button.primary(COMMAND_NAME + " left", Emoji.fromUnicode("⬅️"));
Button downButton = Button.primary(COMMAND_NAME + " down", Emoji.fromUnicode("⬇️"));
Button rightButton = Button.primary(COMMAND_NAME + " right", Emoji.fromUnicode("➡️"));

check user id this method after checking command name

public void onButtonInteraction(ButtonInteractionEvent event) {

getUser method available from SlashCommand package. https://ci.dv8tion.net/job/JDA/javadoc/net/dv8tion/jda/api/events/interaction/SlashCommandEvent.html

Suggestion: save Initial ID of user that creates game via slash command, then read ID of user initiating action and if they are not equal, ignore the action/ return a message.

@Zabuzard or @Taz03 can you assign this to me so I can try my hand at it? I think you just need to save the user ID on the initial slash command event and then do a check on the button interaction event to see if UserId is the same

Taz03 commented

@ooplesoft sure, assigned u. Yes thats the correct approach

Issue was fixed. Please reference my forked commit. Preview screenshot of message is here:
Screenshot 2023-04-01 094544

Taz03 commented

@ooplesoft u will have to make a pr on this repo, which will then be reviewed and get merged

Thanks for letting me know! This is my first time doing this :)
#9