From 27293fc051a2ab0bfc03c2290233476caff33579 Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Tue, 7 Sep 2021 16:49:32 +0200 Subject: [PATCH] Add Message Spliting Signed-off-by: Chaoscaot --- .../bot/listeners/DiscordTicketListener.java | 41 ++++++++++++------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java b/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java index 8c8c4fa7..f7a2fa22 100644 --- a/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java +++ b/src/de/steamwar/bungeecore/bot/listeners/DiscordTicketListener.java @@ -75,29 +75,40 @@ public class DiscordTicketListener extends BasicDiscordListener { } else if(event.getComponentId().startsWith("close-")) { TextChannel logChannel = event.getGuild().getTextChannelById(SteamwarDiscordBotConfig.TICKET_LOG); - MessageBuilder builder = new MessageBuilder(); - StringBuilder stringBuilder = new StringBuilder(); + LinkedList stringBuilders = new LinkedList<>(); + stringBuilders.add(new StringBuilder()); new LinkedList<>(event.getTextChannel().getIterableHistory().complete()).descendingIterator().forEachRemaining(message -> { if(message.getAuthor().isSystem() || message.getAuthor().isBot()) return; - stringBuilder.append(" ") + StringBuilder currentBuilder = new StringBuilder(); + currentBuilder.append(" ") .append("**") .append(message.getAuthor().getName()) .append("**: ") .append(message.getContentRaw()); if(!message.getAttachments().isEmpty()) { - stringBuilder.append("\n") + currentBuilder.append("\n") .append("Files: ").append("\n"); - message.getAttachments().forEach(attachment -> stringBuilder.append(attachment.getUrl()).append("\n")); + message.getAttachments().forEach(attachment -> currentBuilder.append(attachment.getUrl()).append("\n")); } - stringBuilder.append("\n"); + currentBuilder.append("\n"); + if(currentBuilder.length() >= 4096) { + stringBuilders.getLast().append(currentBuilder.substring(0, 4090)); + stringBuilders.add(new StringBuilder(currentBuilder.substring(4090, currentBuilder.length() - 1))); + } else if (currentBuilder.length() + stringBuilders.getLast().length() >= 4096) { + stringBuilders.add(new StringBuilder(currentBuilder.toString())); + } else { + stringBuilders.getLast().append(currentBuilder); + } }); - stringBuilder.append("\n"); - stringBuilder.append(" ") - .append("**").append(event.getUser().getName()).append("**: ") - .append("Ticket geschlossen"); + String footer = " **" + event.getUser().getName() + "**: Ticket geschlossen"; + if(stringBuilders.getLast().length() + footer.length() > 4090) { + stringBuilders.add(new StringBuilder(footer)); + } else { + stringBuilders.getLast().append(footer); + } EmbedBuilder embedBuilder = new EmbedBuilder(); embedBuilder.setColor(Color.GREEN); @@ -107,11 +118,13 @@ public class DiscordTicketListener extends BasicDiscordListener { embedBuilder.setAuthor(user.getName(), swuser==null?"https://steamwar.de/":("https://steamwar.de/users/" + swuser.getUserName().toLowerCase() + "/"), user.getAvatarUrl()); embedBuilder.setTimestamp(Instant.now()); embedBuilder.setTitle(event.getTextChannel().getName()); - embedBuilder.setDescription(stringBuilder); + stringBuilders.forEach(stringBuilder -> { + embedBuilder.setDescription(stringBuilder.toString()); + MessageBuilder builder = new MessageBuilder(); + builder.setEmbeds(embedBuilder.build()); - builder.setEmbeds(embedBuilder.build()); - - logChannel.sendMessage(builder.build()).complete(); + logChannel.sendMessage(builder.build()).queue(); + }); Message.team("DISCORD_TICKET_CLOSED", event.getTextChannel().getName()); event.getTextChannel().delete().reason("Closed").queue();