Skip to content

Commit

Permalink
Handle news channels
Browse files Browse the repository at this point in the history
  • Loading branch information
xdnw committed Jun 18, 2024
1 parent 3c74011 commit 19434cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,28 +105,27 @@ public CompletableFuture<IMessageBuilder> send(IMessageBuilder builder) {
discMsg.embeds.clear();
}
if (discMsg.content.length() > 2000) {
DiscordUtil.sendMessage(channel, discMsg.content.toString());
CompletableFuture<List<Message>> future1 = DiscordUtil.sendMessage(channel, discMsg.content.toString());
discMsg.content.setLength(0);
}
CompletableFuture<IMessageBuilder> msgFuture = null;
boolean sendFiles = true;
if (!discMsg.content.isEmpty() || !discMsg.buttons.isEmpty() || !discMsg.embeds.isEmpty()) {
MessageCreateData message = discMsg.build(true);

if (message.getContent().length() > 20000) {
Message result = null;
if (!discMsg.buttons.isEmpty() || !discMsg.embeds.isEmpty()) {
message = discMsg.build(false);
result = RateLimitUtil.complete(channel.sendMessage(message));
}
CompletableFuture<Message> future = DiscordUtil.sendMessage(channel, discMsg.content.toString());
CompletableFuture<List<Message>> future = DiscordUtil.sendMessage(channel, discMsg.content.toString());
if (result != null) {
assert future != null;
msgFuture = future.thenApply(f -> new DiscordMessageBuilder(this, f));
msgFuture = future.thenApply(f -> new DiscordMessageBuilder(this, f.getLast()));
}
} else {
sendFiles = false;
CompletableFuture<Message> future =RateLimitUtil.queue(channel.sendMessage(message));
CompletableFuture<Message> future = RateLimitUtil.queue(channel.sendMessage(message));
msgFuture = future.thenApply(f -> new DiscordMessageBuilder(this, f));
}

Expand Down
27 changes: 22 additions & 5 deletions src/main/java/link/locutus/discord/util/RateLimitUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@
import link.locutus.discord.util.scheduler.CaughtRunnable;
import link.locutus.discord.util.discord.DiscordUtil;
import link.locutus.discord.web.commands.WebIO;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.NewsChannel;
import net.dv8tion.jda.api.entities.channel.middleman.GuildChannel;
import net.dv8tion.jda.api.entities.channel.middleman.MessageChannel;
import net.dv8tion.jda.api.entities.channel.unions.MessageChannelUnion;
import net.dv8tion.jda.api.requests.RestAction;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -89,13 +94,11 @@ private static <T> RestAction<T> addRequest(RestAction<T> action) {
lastLimitTime = now;
lastLimitTotal = requestsThisMinute.size();
String msg = getRateLimitMessage(category, cutoff);

lastErrorMsg.put(action.getClass(), msg);
}
} else {
lastLimitTotal = 0;
}

return action;
}

Expand All @@ -107,7 +110,6 @@ public static void queueMessage(MessageChannel channel, String message, boolean
}
public static void queueMessage(MessageChannel channel, String message, boolean condense, Integer bufferSeconds) {
if (message.isBlank()) return;

DiscordChannelIO io = new DiscordChannelIO(channel);
queueMessage(io, new Function<IMessageBuilder, Boolean>() {
@Override
Expand Down Expand Up @@ -232,11 +234,26 @@ public void runUnsafe() throws InterruptedException {
}

public static <T> T complete(RestAction<T> action) {
return (T) addRequest(action).complete();
return (T) handleNews(addRequest(action).complete());
}

public static <T> CompletableFuture<T> queue(RestAction<T> action) {
if (action == null) return null;
return addRequest(action).submit();
return handleNews(addRequest(action).submit());
}

private static <T> T handleNews(T t) {
if (t instanceof Message msg) {
MessageChannelUnion channel = msg.getChannel();
if (channel instanceof NewsChannel news && msg.getGuild().getSelfMember().hasAccess(news)) {
news.crosspostMessageById(msg.getIdLong()).queue();
}
}
return t;
}

private static <T> CompletableFuture<T> handleNews(CompletableFuture<T> future) {
future.thenAccept(RateLimitUtil::handleNews);
return future;
}
}
21 changes: 15 additions & 6 deletions src/main/java/link/locutus/discord/util/discord/DiscordUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import static link.locutus.discord.util.MathMan.parseFilter;
import static link.locutus.discord.util.MathMan.parseStringFilter;
Expand Down Expand Up @@ -983,10 +984,10 @@ public static CompletableFuture<Message> sendMessage(InteractionHook hook, Strin
return null;
}

public static CompletableFuture<Message> sendMessage(MessageChannel channel, String message) {
public static CompletableFuture<List<Message>> sendMessage(MessageChannel channel, String message) {
if (message.length() > 20000) {
if (message.length() < 200000) {
return DiscordUtil.upload(channel, "message.txt", message);
return DiscordUtil.upload(channel, "message.txt", message).thenApply(List::of);
}
new Exception().printStackTrace();
throw new IllegalArgumentException("Cannot send message of this length: " + message.length());
Expand All @@ -997,25 +998,33 @@ public static CompletableFuture<Message> sendMessage(MessageChannel channel, Str
if (message.contains("@here")) {
message = message.replace("@here", "");
}
message = WordUtils.wrap(message, 2000, "\n", true, ",");
if (message.length() > 2000) {
message = WordUtils.wrap(message, 2000, "\n", true, "[,\n ]");
String[] lines = message.split("\\r?\\n");
List<CompletableFuture<Message>> futures = new ArrayList<>();
StringBuilder buffer = new StringBuilder();
for (String line : lines) {
if (buffer.length() + 1 + line.length() > 2000) {
String str = buffer.toString().trim();
if (!str.isEmpty()) {
RateLimitUtil.complete(channel.sendMessage(str));
futures.add(RateLimitUtil.queue(channel.sendMessage(str)));
}
buffer.setLength(0);
}
buffer.append('\n').append(line);
}
if (buffer.length() != 0) {
return RateLimitUtil.queue(channel.sendMessage(buffer));
String str = buffer.toString().trim();
if (!str.isEmpty()) {
futures.add(RateLimitUtil.queue(channel.sendMessage(str)));
}
}
return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]))
.thenApply(v -> futures.stream()
.map(CompletableFuture::join)
.collect(Collectors.toList()));
} else if (!message.isEmpty()) {
return RateLimitUtil.queue(channel.sendMessage(message));
return RateLimitUtil.queue(channel.sendMessage(message)).thenApply(List::of);
}
return null;
}
Expand Down

0 comments on commit 19434cc

Please sign in to comment.