|
| 1 | +/* |
| 2 | + * MMDBot - https://github.com/MinecraftModDevelopment/MMDBot |
| 3 | + * Copyright (C) 2016-2022 <MMD - MinecraftModDevelopment> |
| 4 | + * |
| 5 | + * This library is free software; you can redistribute it and/or |
| 6 | + * modify it under the terms of the GNU Lesser General Public |
| 7 | + * License as published by the Free Software Foundation; |
| 8 | + * Specifically version 2.1 of the License. |
| 9 | + * |
| 10 | + * This library is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + * Lesser General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU Lesser General Public |
| 16 | + * License along with this library; if not, write to the Free Software |
| 17 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + * USA |
| 19 | + * https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html |
| 20 | + */ |
| 21 | +package com.mcmoddev.mmdbot.commander.eventlistener; |
| 22 | + |
| 23 | +import net.dv8tion.jda.api.MessageBuilder; |
| 24 | +import net.dv8tion.jda.api.events.GenericEvent; |
| 25 | +import net.dv8tion.jda.api.events.message.MessageReceivedEvent; |
| 26 | +import net.dv8tion.jda.api.hooks.EventListener; |
| 27 | +import net.dv8tion.jda.api.interactions.components.ActionRow; |
| 28 | +import net.dv8tion.jda.api.interactions.components.Component; |
| 29 | +import net.dv8tion.jda.api.interactions.components.buttons.Button; |
| 30 | +import org.jetbrains.annotations.NotNull; |
| 31 | + |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.List; |
| 34 | + |
| 35 | +public class FilePreviewListener implements EventListener { |
| 36 | + public FilePreviewListener() {} |
| 37 | + |
| 38 | + public static final List<String> BLACKLISTED_EXTENSIONS = List.of( |
| 39 | + "png", "jpeg", "jpg", "jpe", "jif", "jfif", "jfi", "jp2", |
| 40 | + "tiff", "tif", "mp4", "avi", "mp3", "wav", "gif", "webp", "psd", "bpm" |
| 41 | + ); |
| 42 | + |
| 43 | + public static final String URL = "https://discordbot.matyrobbrt.com/fpreview?url="; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void onEvent(@NotNull final GenericEvent gE) { |
| 47 | + if (!(gE instanceof MessageReceivedEvent event)) return; |
| 48 | + final var attachments = event.getMessage().getAttachments(); |
| 49 | + if (attachments.isEmpty()) return; |
| 50 | + final var messageBuilder = new MessageBuilder() |
| 51 | + .append("Paste version of "); |
| 52 | + final var rows = new ArrayList<List<Button>>(); |
| 53 | + for (var i = 0; i < attachments.size(); i++) { |
| 54 | + final var attach = attachments.get(i); |
| 55 | + if (!BLACKLISTED_EXTENSIONS.contains(attach.getFileExtension())) { |
| 56 | + final var url = URL + attach.getUrl(); |
| 57 | + messageBuilder |
| 58 | + .append('`') |
| 59 | + .append(attach.getFileName()) |
| 60 | + .append('`'); |
| 61 | + |
| 62 | + if (i != attachments.size() - 1) { |
| 63 | + messageBuilder.append(", "); |
| 64 | + } else { |
| 65 | + messageBuilder.append(' '); |
| 66 | + } |
| 67 | + addButton(rows, Button.link(url, trimIfTooLong("View " + attach.getFileName()))); |
| 68 | + } |
| 69 | + } |
| 70 | + if (!rows.isEmpty()) { |
| 71 | + messageBuilder.append("from ") |
| 72 | + .append(event.getAuthor().getAsMention()); |
| 73 | + event.getMessage().reply(messageBuilder.build()) |
| 74 | + .setActionRows(rows.stream().map(ActionRow::of).toList()) |
| 75 | + .mentionRepliedUser(false) |
| 76 | + .allowedMentions(List.of()) |
| 77 | + .queue(); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + private static String trimIfTooLong(String str) { |
| 82 | + if (str.length() > Button.LABEL_MAX_LENGTH - 3) { |
| 83 | + return str.substring(0, Button.LABEL_MAX_LENGTH - 3) + "..."; |
| 84 | + } else { |
| 85 | + return str; |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + private static void addButton(List<List<Button>> list, Button button) { |
| 90 | + if (list.isEmpty()) { |
| 91 | + final var nL = new ArrayList<Button>(); |
| 92 | + nL.add(button); |
| 93 | + list.add(nL); |
| 94 | + } else { |
| 95 | + final var lastList = list.get(list.size() - 1); |
| 96 | + if (lastList.size() >= Component.Type.BUTTON.getMaxPerRow()) { |
| 97 | + final var newList = new ArrayList<Button>(); |
| 98 | + newList.add(button); |
| 99 | + if (list.size() < 5) { |
| 100 | + list.add(newList); |
| 101 | + } |
| 102 | + } else { |
| 103 | + lastList.add(button); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments