JDA discord bot. After a ButtonInteractionEvent is fired, how do I delete the message…
- User
JDA discord bot. After a ButtonInteractionEvent is fired, how do I delete the message and send a new one? More specifically, I need to acknowledge the button press in JDA, delete the message, and then send a new message
- Assistant
To delete the original message and send a new one after a
ButtonInteractionEventis fired in JDA, you can use the following steps:-
Inside your
onButtonClick()method, you can access theMessageobject of the button's original message usingButtonInteractionEvent#getButton(). It will return aButtoninstance, and you can retrieve the original message by callingButton#getFinalMessage(). -
To delete the original message, you can call the
Message#delete()method on the retrieved message. -
After deleting the original message, you can send a new message using a
TextChannelobject. Depending on your requirements, you can send a simple text message or a more complex embed.
Here's an example implementation:
@Override public void onButtonClick(ButtonInteractionEvent event) { // Get the original message Button button = event.getButton(); Message originalMessage = button.getFinalMessage(); // Delete the original message originalMessage.delete().queue(); // Get the text channel to send the new message TextChannel channel = event.getTextChannel(); // Send a new message channel.sendMessage("Button pressed and acknowledged!").queue(); } -