|
| 1 | +package net.javadiscord.javabot.listener; |
| 2 | + |
| 3 | +import java.util.Collections; |
| 4 | +import java.util.List; |
| 5 | +import java.util.Optional; |
| 6 | +import java.util.concurrent.CompletableFuture; |
| 7 | + |
| 8 | +import javax.annotation.Nonnull; |
| 9 | + |
| 10 | +import net.dv8tion.jda.api.entities.BaseGuildMessageChannel; |
| 11 | +import net.dv8tion.jda.api.entities.GuildMessageChannel; |
| 12 | +import net.dv8tion.jda.api.entities.Message; |
| 13 | +import net.dv8tion.jda.api.entities.Message.Attachment; |
| 14 | +import net.dv8tion.jda.api.entities.TextChannel; |
| 15 | +import net.dv8tion.jda.api.entities.Webhook; |
| 16 | +import net.dv8tion.jda.api.entities.WebhookType; |
| 17 | +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
| 18 | +import net.dv8tion.jda.api.hooks.ListenerAdapter; |
| 19 | +import net.dv8tion.jda.api.utils.AttachmentOption; |
| 20 | +import net.dv8tion.jda.internal.entities.WebhookImpl; |
| 21 | +import net.dv8tion.jda.internal.requests.restaction.WebhookMessageActionImpl; |
| 22 | +import net.javadiscord.javabot.Bot; |
| 23 | + |
| 24 | +/** |
| 25 | + * Replaces all occurences of 'fuck' in incoming messages with 'hug'. |
| 26 | + */ |
| 27 | +public class HugListener extends ListenerAdapter { |
| 28 | + @Override |
| 29 | + public void onMessageReceived(@Nonnull MessageReceivedEvent event) { |
| 30 | + if(!event.isFromGuild()) { |
| 31 | + return; |
| 32 | + } |
| 33 | + if (Bot.autoMod.hasSuspiciousLink(event.getMessage()) || Bot.autoMod.hasAdvertisingLink(event.getMessage())) { |
| 34 | + return; |
| 35 | + } |
| 36 | + TextChannel tc=event.getTextChannel(); |
| 37 | + if(event.isFromThread()) { |
| 38 | + GuildMessageChannel parentChannel = event.getThreadChannel().getParentMessageChannel(); |
| 39 | + if(parentChannel instanceof TextChannel textChannel) { |
| 40 | + tc=textChannel; |
| 41 | + } |
| 42 | + } |
| 43 | + if(tc==null) { |
| 44 | + return; |
| 45 | + } |
| 46 | + final TextChannel textChannel=tc; |
| 47 | + String content=event.getMessage().getContentRaw(); |
| 48 | + String lowerCaseContent=content.toLowerCase(); |
| 49 | + if(lowerCaseContent.contains("fuck")) { |
| 50 | + StringBuilder sb=new StringBuilder(content.length()); |
| 51 | + int index=0; |
| 52 | + int indexBkp=index; |
| 53 | + while((index=lowerCaseContent.indexOf("fuck",index))!=-1) { |
| 54 | + sb.append(content.substring(indexBkp,index)); |
| 55 | + sb.append("hug"); |
| 56 | + indexBkp=index+++4; |
| 57 | + } |
| 58 | + |
| 59 | + sb.append(content.substring(indexBkp,content.length())); |
| 60 | + event.getMessage().delete().queue(unused->{ |
| 61 | + textChannel.retrieveWebhooks().queue(webhooks->{ |
| 62 | + Optional<Webhook> hook = webhooks |
| 63 | + .stream() |
| 64 | + .filter(webhook->webhook.getChannel().getIdLong()==event.getChannel().getIdLong()) |
| 65 | + .filter(wh->wh.getToken()!=null) |
| 66 | + .findAny(); |
| 67 | + if(hook.isPresent()) { |
| 68 | + buildMessage(hook.get(), event.getMessage(), sb.toString(),(BaseGuildMessageChannel) event.getGuildChannel()); |
| 69 | + }else { |
| 70 | + textChannel |
| 71 | + .createWebhook("JavaBot-hug") |
| 72 | + .queue(wh-> |
| 73 | + buildMessage(wh, event.getMessage(), sb.toString(),(BaseGuildMessageChannel) event.getGuildChannel()) |
| 74 | + ); |
| 75 | + } |
| 76 | + }); |
| 77 | + }); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private void buildMessage(Webhook webhook, Message originalMessage, String newMessageContent, BaseGuildMessageChannel channel){ |
| 82 | + WebhookImpl webhookImpl = new WebhookImpl(channel, webhook.getJDA(), webhook.getIdLong(), WebhookType.INCOMING); |
| 83 | + webhookImpl.setToken(webhook.getToken()); |
| 84 | + WebhookMessageActionImpl<Void> ret = webhookImpl |
| 85 | + .sendMessage(newMessageContent) |
| 86 | + .allowedMentions(Collections.emptyList()); |
| 87 | + |
| 88 | + List<Attachment> attachments = originalMessage.getAttachments(); |
| 89 | + @SuppressWarnings("unchecked") |
| 90 | + CompletableFuture<?>[] futures=new CompletableFuture<?>[attachments.size()]; |
| 91 | + |
| 92 | + for(int i = 0; i < attachments.size(); i++){ |
| 93 | + Attachment attachment = attachments.get(i); |
| 94 | + futures[i]=attachment |
| 95 | + .getProxy() |
| 96 | + .download() |
| 97 | + .thenAccept(is-> |
| 98 | + ret.addFile( |
| 99 | + is, |
| 100 | + attachment.getFileName(), |
| 101 | + attachment.isSpoiler() ? new AttachmentOption[] { AttachmentOption.SPOILER } : new AttachmentOption[0] |
| 102 | + ) |
| 103 | + ); |
| 104 | + } |
| 105 | + CompletableFuture.allOf(futures) |
| 106 | + .thenAccept(unused->ret.queue()); |
| 107 | + } |
| 108 | +} |
0 commit comments