Skip to content

Commit

Permalink
Delete replies if original message is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
rtm516 committed Mar 8, 2024
1 parent 980757c commit 9e90b5e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/geysermc/discordbot/GeyserBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ public static void main(String[] args) throws IOException, LoginException {
new BadLinksHandler(),
new HelpHandler(),
new SupportHandler(),
new DeleteHandler(),
client.build(),
tagClient.build())
.build();
Expand Down
48 changes: 48 additions & 0 deletions src/main/java/org/geysermc/discordbot/listeners/DeleteHandler.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2020-2024 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/GeyserDiscordBot
*/

package org.geysermc.discordbot.listeners;

import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.MessageHistory;
import net.dv8tion.jda.api.entities.MessageType;
import net.dv8tion.jda.api.events.message.MessageDeleteEvent;
import net.dv8tion.jda.api.hooks.ListenerAdapter;
import org.jetbrains.annotations.NotNull;

public class DeleteHandler extends ListenerAdapter {
@Override
public void onMessageDelete(@NotNull MessageDeleteEvent event) {
// Get the last 10 messages in the channel and delete any that are inline replies to the deleted message
MessageHistory closeMessages = MessageHistory.getHistoryAround(event.getChannel(), event.getMessageId()).limit(10).complete();
for (Message message : closeMessages.getRetrievedHistory()) {
if (message.getType() == MessageType.INLINE_REPLY
&& message.getAuthor() == event.getJDA().getSelfUser()
&& message.getMessageReference().getMessageId().equals(event.getMessageId())) {
message.delete().complete();
}
}
}
}

0 comments on commit 9e90b5e

Please sign in to comment.