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