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();
}
});
}
Aggregations