A user sends a message, and the bot replies, and in another discord channel, there are buttons that can delete the message, but when you click on the delete message button, the bot's message is deleted and not the user's, how do I fix it?.
public void onButtonInteraction(ButtonInteractionEvent event) {
if(event.getComponentId().equals("acc")) {
event.getChannel().sendMessage(messageID).queue();
event.getChannel()
.sendMessage(event.getJDA().getTextChannelById("1195039391563907082").getLatestMessageId()).queue();
event.deferEdit().queue();
}
}
at first I tried to check if it was a bot or not, but then I thought about it
The issue with your code is that you get the last message of the channel, which was sent by the bot.
A simpler solution would be to store the message ID, and use that ID to delete the message
@Override
public void onMessageReceived(MessageReceivedEvent event) {
event.getChannel().sendMessage(event.getJumpUrl()).addActionRow(Button.danger("delete"+event.getMessageId(), "Delete Message")).queue();
}
@Override
public void onButtonInteraction(ButtonInteractionEvent event) {
if(event.getId().contains("delete")) {
String id;
id = event.getId().replace("delete", "");
event.getJDA().getTextChannelById("1195039391563907082").deleteMessageById(id).queue();
}
}
Make sure you change this based on your requirement.