Search in sources :

Example 1 with DiscordImageEvent

use of com.loohp.interactivechatdiscordsrvaddon.api.events.DiscordImageEvent 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

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 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 MessageAction (github.scarsz.discordsrv.dependencies.jda.api.requests.restaction.MessageAction)1 ArrayList (java.util.ArrayList)1 LinkedHashSet (java.util.LinkedHashSet)1