use of com.loohp.interactivechatdiscordsrvaddon.utils.ThrowingSupplier in project InteractiveChat-DiscordSRV-Addon by LOOHP.
the class InboundToGameEvents method onReceiveMessageFromDiscordPost.
@Subscribe(priority = ListenerPriority.HIGH)
public void onReceiveMessageFromDiscordPost(DiscordGuildMessagePostProcessEvent event) {
Debug.debug("Triggering onReceiveMessageFromDiscordPost");
Message message = event.getMessage();
github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component component = event.getMinecraftMessage();
DiscordSRV srv = InteractiveChatDiscordSrvAddon.discordsrv;
User author = message.getAuthor();
if (InteractiveChatDiscordSrvAddon.plugin.translateMentions) {
Debug.debug("onReceiveMessageFromDiscordPost translating mentions");
Set<UUID> mentionTitleSent = new HashSet<>();
Map<Member, UUID> channelMembers = new HashMap<>();
TextChannel channel = event.getChannel();
Guild guild = channel.getGuild();
Member authorAsMember = guild.getMember(author);
String senderDiscordName = authorAsMember == null ? author.getName() : authorAsMember.getEffectiveName();
UUID senderUUID = srv.getAccountLinkManager().getUuid(author.getId());
for (Entry<UUID, String> entry : srv.getAccountLinkManager().getManyDiscordIds(Bukkit.getOnlinePlayers().stream().map(each -> each.getUniqueId()).collect(Collectors.toSet())).entrySet()) {
Member member = guild.getMemberById(entry.getValue());
if (member != null && member.hasAccess(channel)) {
channelMembers.put(member, entry.getKey());
}
}
if (message.mentionsEveryone()) {
// github.scarsz.discordsrv.dependencies.kyori.adventure.text.event.HoverEvent<Component> hover = Component.text(InteractiveChatDiscordSrvAddon.plugin.mentionHover.replace("{DiscordUser}", senderDiscordName).replace("{TextChannel}", "#" + channel.getName()).replace("{Guild}", guild.getName())).asHoverEvent();
component = component.replaceText(github.scarsz.discordsrv.dependencies.kyori.adventure.text.TextReplacementConfig.builder().matchLiteral("@here").replacement(github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component.text(InteractiveChatDiscordSrvAddon.plugin.mentionHighlight.replace("{DiscordMention}", "@here"))).build()).replaceText(github.scarsz.discordsrv.dependencies.kyori.adventure.text.TextReplacementConfig.builder().matchLiteral("@everyone").replacement(github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component.text(InteractiveChatDiscordSrvAddon.plugin.mentionHighlight.replace("{DiscordMention}", "@everyone"))).build());
for (UUID uuid : channelMembers.values()) {
mentionTitleSent.add(uuid);
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
DiscordToGameMention.playTitleScreen(senderDiscordName, channel.getName(), guild.getName(), player);
}
}
}
List<Role> mentionedRoles = message.getMentionedRoles();
for (Role role : mentionedRoles) {
// github.scarsz.discordsrv.dependencies.kyori.adventure.text.event.HoverEvent<Component> hover = Component.text(InteractiveChatDiscordSrvAddon.plugin.mentionHover.replace("{DiscordUser}", senderDiscordName).replace("{TextChannel}", "#" + channel.getName()).replace("{Guild}", guild.getName())).asHoverEvent();
component = component.replaceText(github.scarsz.discordsrv.dependencies.kyori.adventure.text.TextReplacementConfig.builder().matchLiteral("@" + role.getName()).replacement(github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component.text(InteractiveChatDiscordSrvAddon.plugin.mentionHighlight.replace("{DiscordMention}", "@" + role.getName()))).build());
for (Entry<Member, UUID> entry : channelMembers.entrySet()) {
UUID uuid = entry.getValue();
if (!mentionTitleSent.contains(uuid) && entry.getKey().getRoles().contains(role)) {
mentionTitleSent.add(uuid);
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
DiscordToGameMention.playTitleScreen(senderDiscordName, channel.getName(), guild.getName(), player);
}
}
}
}
List<User> mentionedUsers = message.getMentionedUsers();
if (!mentionedUsers.isEmpty()) {
for (User user : mentionedUsers) {
// github.scarsz.discordsrv.dependencies.kyori.adventure.text.event.HoverEvent<Component> hover = Component.text(InteractiveChatDiscordSrvAddon.plugin.mentionHover.replace("{DiscordUser}", senderDiscordName).replace("{TextChannel}", "#" + channel.getName()).replace("{Guild}", guild.getName())).asHoverEvent();
component = component.replaceText(github.scarsz.discordsrv.dependencies.kyori.adventure.text.TextReplacementConfig.builder().matchLiteral("@" + user.getName()).replacement(github.scarsz.discordsrv.dependencies.kyori.adventure.text.Component.text(InteractiveChatDiscordSrvAddon.plugin.mentionHighlight.replace("{DiscordMention}", "@" + user.getName()))).build());
Member member = guild.getMember(user);
if (member != null) {
UUID uuid = channelMembers.get(member);
if (uuid != null && !mentionTitleSent.contains(uuid) && (senderUUID == null || !senderUUID.equals(uuid))) {
mentionTitleSent.add(uuid);
Player player = Bukkit.getPlayer(uuid);
if (player != null) {
DiscordToGameMention.playTitleScreen(senderDiscordName, channel.getName(), guild.getName(), player);
}
}
}
}
}
event.setMinecraftMessage(component);
}
String processedMessage = MessageUtil.toLegacy(component);
if (InteractiveChatDiscordSrvAddon.plugin.convertDiscordAttachments) {
Debug.debug("onReceiveMessageFromDiscordPre converting discord attachments");
Set<String> processedUrl = new HashSet<>();
for (Attachment attachment : message.getAttachments()) {
InteractiveChatDiscordSrvAddon.plugin.attachmentCounter.incrementAndGet();
String url = attachment.getUrl();
if (processedMessage.contains(url)) {
processedUrl.add(url);
if ((attachment.isImage() || attachment.isVideo()) && attachment.getSize() <= InteractiveChatDiscordSrvAddon.plugin.discordAttachmentsPreviewLimit) {
InteractiveChatDiscordSrvAddon.plugin.attachmentImageCounter.incrementAndGet();
List<ThrowingSupplier<InputStream>> methods = new ArrayList<>();
methods.add(() -> attachment.retrieveInputStream().get());
if (URLRequestUtils.isAllowed(attachment.getUrl())) {
methods.add(() -> URLRequestUtils.getInputStream0(attachment.getUrl()));
}
if (URLRequestUtils.isAllowed(attachment.getProxyUrl())) {
methods.add(() -> URLRequestUtils.getInputStream0(attachment.getProxyUrl()));
}
try (InputStream stream = URLRequestUtils.retrieveInputStreamUntilSuccessful(methods)) {
GraphicsToPacketMapWrapper map;
boolean isVideo = false;
if (url.toLowerCase().endsWith(".gif")) {
map = new GraphicsToPacketMapWrapper(InteractiveChatDiscordSrvAddon.plugin.playbackBarEnabled, InteractiveChatDiscordSrvAddon.plugin.discordAttachmentsMapBackgroundColor);
GifReader.readGif(stream, InteractiveChatDiscordSrvAddon.plugin.mediaReadingService, (frames, e) -> {
if (e != null) {
e.printStackTrace();
map.completeFuture(null);
} else {
map.completeFuture(frames);
}
});
} else {
BufferedImage image = ImageIO.read(stream);
map = new GraphicsToPacketMapWrapper(image, InteractiveChatDiscordSrvAddon.plugin.discordAttachmentsMapBackgroundColor);
}
DiscordAttachmentData data = new DiscordAttachmentData(attachment.getFileName(), url, map, isVideo);
DiscordAttachmentConversionEvent dace = new DiscordAttachmentConversionEvent(url, data);
Bukkit.getPluginManager().callEvent(dace);
DATA.put(data.getUniqueId(), data);
Bukkit.getScheduler().runTaskLater(InteractiveChatDiscordSrvAddon.plugin, () -> DATA.remove(data.getUniqueId()), InteractiveChatDiscordSrvAddon.plugin.discordAttachmentTimeout);
} catch (IOException e) {
e.printStackTrace();
DiscordAttachmentData data = new DiscordAttachmentData(attachment.getFileName(), url);
DiscordAttachmentConversionEvent dace = new DiscordAttachmentConversionEvent(url, data);
Bukkit.getPluginManager().callEvent(dace);
DATA.put(data.getUniqueId(), data);
Bukkit.getScheduler().runTaskLater(InteractiveChatDiscordSrvAddon.plugin, () -> DATA.remove(data.getUniqueId()), InteractiveChatDiscordSrvAddon.plugin.discordAttachmentTimeout);
}
} else {
DiscordAttachmentData data = new DiscordAttachmentData(attachment.getFileName(), url);
DiscordAttachmentConversionEvent dace = new DiscordAttachmentConversionEvent(url, data);
Bukkit.getPluginManager().callEvent(dace);
DATA.put(data.getUniqueId(), data);
Bukkit.getScheduler().runTaskLater(InteractiveChatDiscordSrvAddon.plugin, () -> DATA.remove(data.getUniqueId()), InteractiveChatDiscordSrvAddon.plugin.discordAttachmentTimeout);
}
}
}
Matcher matcher = URLRequestUtils.IMAGE_URL_PATTERN.matcher(message.getContentRaw());
while (matcher.find()) {
String url = matcher.group();
String extension = matcher.group(1);
if (!processedUrl.contains(url) && URLRequestUtils.isAllowed(url)) {
long size = HTTPRequestUtils.getContentSize(url);
if (size >= 0 && size <= InteractiveChatDiscordSrvAddon.plugin.discordAttachmentsPreviewLimit) {
InteractiveChatDiscordSrvAddon.plugin.attachmentImageCounter.incrementAndGet();
try (InputStream stream = URLRequestUtils.getInputStream(url)) {
GraphicsToPacketMapWrapper map;
boolean isVideo = false;
if (extension.equals("gif")) {
map = new GraphicsToPacketMapWrapper(InteractiveChatDiscordSrvAddon.plugin.playbackBarEnabled, InteractiveChatDiscordSrvAddon.plugin.discordAttachmentsMapBackgroundColor);
GifReader.readGif(stream, InteractiveChatDiscordSrvAddon.plugin.mediaReadingService, (frames, e) -> {
if (e != null) {
e.printStackTrace();
map.completeFuture(null);
} else {
map.completeFuture(frames);
}
});
} else {
BufferedImage image = ImageIO.read(stream);
map = new GraphicsToPacketMapWrapper(image, InteractiveChatDiscordSrvAddon.plugin.discordAttachmentsMapBackgroundColor);
}
int end = matcher.end(1);
String name = url.lastIndexOf("/") < 0 ? url.substring(0, end) : url.substring(url.lastIndexOf("/") + 1, end);
DiscordAttachmentData data = new DiscordAttachmentData(name, url, map, isVideo);
DiscordAttachmentConversionEvent dace = new DiscordAttachmentConversionEvent(url, data);
Bukkit.getPluginManager().callEvent(dace);
DATA.put(data.getUniqueId(), data);
Bukkit.getScheduler().runTaskLater(InteractiveChatDiscordSrvAddon.plugin, () -> DATA.remove(data.getUniqueId()), InteractiveChatDiscordSrvAddon.plugin.discordAttachmentTimeout);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
Aggregations