Search in sources :

Example 1 with MessageAction

use of github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.MessageAction in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class DiscordMessageContent method toJDAMessageRestAction.

@SuppressWarnings("deprecation")
public RestAction<List<Message>> toJDAMessageRestAction(TextChannel channel) {
    Map<MessageAction, Set<String>> actions = new LinkedHashMap<>();
    Set<String> rootAttachments = new HashSet<>();
    rootAttachments.add(authorIconUrl);
    EmbedBuilder embed = new EmbedBuilder().setAuthor(authorName, null, authorIconUrl).setColor(color).setThumbnail(thumbnail);
    if (description.size() > 0) {
        embed.setDescription(description.get(0));
    }
    if (imageUrl.size() > 0) {
        String url = imageUrl.get(0);
        embed.setImage(url);
        rootAttachments.add(url);
    }
    if (imageUrl.size() == 1 || description.size() == 1) {
        if (footer != null) {
            if (footerImageUrl == null) {
                embed.setFooter(footer);
            } else {
                embed.setFooter(footer, footerImageUrl);
                rootAttachments.add(footerImageUrl);
            }
        }
    }
    actions.put(channel.sendMessage(embed.build()), rootAttachments);
    for (int i = 1; i < imageUrl.size() || i < description.size(); i++) {
        Set<String> usedAttachments = new HashSet<>();
        EmbedBuilder otherEmbed = new EmbedBuilder().setColor(color);
        if (i < imageUrl.size()) {
            String url = imageUrl.get(i);
            otherEmbed.setImage(url);
            usedAttachments.add(url);
        }
        if (i < description.size()) {
            otherEmbed.setDescription(description.get(i));
        }
        if (!(i + 1 < imageUrl.size() || i + 1 < description.size())) {
            if (footer != null) {
                if (footerImageUrl == null) {
                    otherEmbed.setFooter(footer);
                } else {
                    otherEmbed.setFooter(footer, footerImageUrl);
                }
            }
        }
        if (!otherEmbed.isEmpty()) {
            actions.put(channel.sendMessage(otherEmbed.build()), usedAttachments);
        }
    }
    Set<String> embeddedAttachments = new HashSet<>();
    for (Entry<MessageAction, Set<String>> entry : actions.entrySet()) {
        MessageAction action = entry.getKey();
        Set<String> neededUrls = entry.getValue();
        for (Entry<String, byte[]> attachment : attachments.entrySet()) {
            String attachmentName = attachment.getKey();
            if (neededUrls.contains("attachment://" + attachmentName)) {
                action.addFile(attachment.getValue(), attachmentName);
                embeddedAttachments.add(attachmentName);
            }
        }
    }
    MessageAction lastAction = actions.keySet().stream().skip(actions.size() - 1).findFirst().get();
    for (Entry<String, byte[]> attachment : attachments.entrySet()) {
        String attachmentName = attachment.getKey();
        if (!embeddedAttachments.contains(attachmentName)) {
            lastAction.addFile(attachment.getValue(), attachmentName);
        }
    }
    return RestAction.allOf(actions.keySet());
}
Also used : EmbedBuilder(github.scarsz.discordsrv.dependencies.jda.api.EmbedBuilder) WebhookEmbedBuilder(club.minnced.discord.webhook.send.WebhookEmbedBuilder) Set(java.util.Set) HashSet(java.util.HashSet) MessageAction(github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.MessageAction) LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet)

Example 2 with MessageAction

use of github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.MessageAction in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class OutboundToDiscordEvents method discordMessageSent.

// =====
@Subscribe(priority = ListenerPriority.HIGHEST)
public void discordMessageSent(DiscordGuildMessageSentEvent event) {
    Debug.debug("Triggered discordMessageSent");
    Message message = event.getMessage();
    String textOriginal = message.getContentRaw();
    TextChannel channel = event.getChannel();
    if (!InteractiveChatDiscordSrvAddon.plugin.isEnabled()) {
        return;
    }
    Bukkit.getScheduler().runTaskAsynchronously(InteractiveChatDiscordSrvAddon.plugin, () -> {
        String text = textOriginal;
        if (!text.contains("<ICD=")) {
            return;
        }
        Set<Integer> matches = new LinkedHashSet<>();
        synchronized (DATA) {
            for (int key : DATA.keySet()) {
                if (text.contains("<ICD=" + key + ">")) {
                    text = text.replace("<ICD=" + key + ">", "");
                    matches.add(key);
                }
            }
        }
        if (matches.isEmpty()) {
            Debug.debug("discordMessageSent keys empty");
            return;
        }
        message.editMessage(text + " ...").queue();
        ICPlayer player = DATA.get(matches.iterator().next()).getPlayer();
        List<DiscordDisplayData> dataList = new ArrayList<>();
        for (int key : matches) {
            DiscordDisplayData data = DATA.remove(key);
            if (data != null) {
                dataList.add(data);
            }
        }
        dataList.sort(DISPLAY_DATA_COMPARATOR);
        Debug.debug("discordMessageSent creating contents");
        List<DiscordMessageContent> contents = createContents(dataList, player.isLocal() ? player.getLocalPlayer() : (Bukkit.getOnlinePlayers().isEmpty() ? null : Bukkit.getOnlinePlayers().iterator().next()));
        DiscordImageEvent discordImageEvent = new DiscordImageEvent(channel, textOriginal, text, contents, false, true);
        Bukkit.getPluginManager().callEvent(discordImageEvent);
        Debug.debug("discordMessageSent sending to discord, Cancelled: " + discordImageEvent.isCancelled());
        if (discordImageEvent.isCancelled()) {
            message.editMessage(discordImageEvent.getOriginalMessage()).queue();
        } else {
            text = discordImageEvent.getNewMessage();
            MessageAction action = message.editMessage(text);
            List<MessageEmbed> embeds = new ArrayList<>();
            for (DiscordMessageContent content : contents) {
                embeds.addAll(content.toJDAMessageEmbeds());
                for (Entry<String, byte[]> attachment : content.getAttachments().entrySet()) {
                    action = action.addFile(attachment.getValue(), attachment.getKey());
                }
            }
            action.setEmbeds(embeds).queue();
        }
    });
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ICPlayer(com.loohp.interactivechat.objectholders.ICPlayer) MessageEmbed(github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed) Message(github.scarsz.discordsrv.dependencies.jda.api.entities.Message) DiscordImageEvent(com.loohp.interactivechatdiscordsrvaddon.api.events.DiscordImageEvent) DiscordDisplayData(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordDisplayData) DiscordMessageContent(com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent) ArrayList(java.util.ArrayList) TextChannel(github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel) MessageAction(github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.MessageAction) Subscribe(github.scarsz.discordsrv.api.Subscribe)

Aggregations

MessageAction (github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.MessageAction)2 WebhookEmbedBuilder (club.minnced.discord.webhook.send.WebhookEmbedBuilder)1 ICPlayer (com.loohp.interactivechat.objectholders.ICPlayer)1 DiscordImageEvent (com.loohp.interactivechatdiscordsrvaddon.api.events.DiscordImageEvent)1 DiscordDisplayData (com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordDisplayData)1 DiscordMessageContent (com.loohp.interactivechatdiscordsrvaddon.objectholders.DiscordMessageContent)1 Subscribe (github.scarsz.discordsrv.api.Subscribe)1 EmbedBuilder (github.scarsz.discordsrv.dependencies.jda.api.EmbedBuilder)1 Message (github.scarsz.discordsrv.dependencies.jda.api.entities.Message)1 MessageEmbed (github.scarsz.discordsrv.dependencies.jda.api.entities.MessageEmbed)1 TextChannel (github.scarsz.discordsrv.dependencies.jda.api.entities.TextChannel)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Set (java.util.Set)1