mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2024-12-28 17:37:52 +01:00
Convert things to CompletableFutureUtil
This commit is contained in:
parent
634c123937
commit
1097a8f2f8
@ -25,7 +25,7 @@ package com.discordsrv.api.discord.api.entity.message;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
/**
|
||||
@ -38,7 +38,7 @@ public interface ReceivedDiscordMessageCluster {
|
||||
* @return the messages in this cluster
|
||||
*/
|
||||
@NotNull
|
||||
List<? extends ReceivedDiscordMessage> getMessages();
|
||||
Set<? extends ReceivedDiscordMessage> getMessages();
|
||||
|
||||
/**
|
||||
* Deletes all the messages from this cluster, one request per message.
|
||||
|
@ -41,6 +41,7 @@ import com.discordsrv.common.discord.api.entity.channel.DiscordTextChannelImpl;
|
||||
import com.discordsrv.common.discord.api.entity.guild.DiscordGuildImpl;
|
||||
import com.discordsrv.common.discord.api.entity.guild.DiscordRoleImpl;
|
||||
import com.discordsrv.common.function.CheckedSupplier;
|
||||
import com.discordsrv.common.future.util.CompletableFutureUtil;
|
||||
import com.github.benmanes.caffeine.cache.AsyncCacheLoader;
|
||||
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
|
||||
import com.github.benmanes.caffeine.cache.Expiry;
|
||||
@ -320,9 +321,7 @@ public class DiscordAPIImpl implements DiscordAPI {
|
||||
}
|
||||
|
||||
public <T> CompletableFuture<T> notReady() {
|
||||
CompletableFuture<T> future = new CompletableFuture<>();
|
||||
future.completeExceptionally(new NotReadyException());
|
||||
return future;
|
||||
return CompletableFutureUtil.failed(new NotReadyException());
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -398,11 +397,9 @@ public class DiscordAPIImpl implements DiscordAPI {
|
||||
return notReady();
|
||||
}
|
||||
|
||||
CompletableFuture<WebhookClient> future = new CompletableFuture<>();
|
||||
TextChannel textChannel = jda.getTextChannelById(channelId);
|
||||
if (textChannel == null) {
|
||||
future.completeExceptionally(new IllegalArgumentException("Channel could not be found"));
|
||||
return future;
|
||||
return CompletableFutureUtil.failed(new IllegalArgumentException("Channel could not be found"));
|
||||
}
|
||||
|
||||
return textChannel.retrieveWebhooks().submit().thenApply(webhooks -> {
|
||||
@ -422,9 +419,7 @@ public class DiscordAPIImpl implements DiscordAPI {
|
||||
return hook;
|
||||
}).thenCompose(webhook -> {
|
||||
if (webhook != null) {
|
||||
CompletableFuture<Webhook> completableFuture = new CompletableFuture<>();
|
||||
completableFuture.complete(webhook);
|
||||
return completableFuture;
|
||||
return CompletableFuture.completedFuture(webhook);
|
||||
}
|
||||
|
||||
return textChannel.createWebhook("DiscordSRV").submit();
|
||||
|
@ -21,54 +21,44 @@ package com.discordsrv.common.discord.api.entity.message;
|
||||
import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessage;
|
||||
import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessageCluster;
|
||||
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
|
||||
import com.discordsrv.common.future.util.CompletableFutureUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
|
||||
public class ReceivedDiscordMessageClusterImpl implements ReceivedDiscordMessageCluster {
|
||||
|
||||
private final List<ReceivedDiscordMessage> messages;
|
||||
private final Set<ReceivedDiscordMessage> messages;
|
||||
|
||||
public ReceivedDiscordMessageClusterImpl(List<ReceivedDiscordMessage> messages) {
|
||||
public ReceivedDiscordMessageClusterImpl(Set<ReceivedDiscordMessage> messages) {
|
||||
this.messages = messages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull List<ReceivedDiscordMessage> getMessages() {
|
||||
public @NotNull Set<ReceivedDiscordMessage> getMessages() {
|
||||
return messages;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public @NotNull CompletableFuture<Void> deleteAll() {
|
||||
CompletableFuture<Void>[] futures = new CompletableFuture[messages.size()];
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
futures[i] = messages.get(i).delete();
|
||||
List<CompletableFuture<Void>> futures = new ArrayList<>(messages.size());
|
||||
for (ReceivedDiscordMessage message : messages) {
|
||||
futures.add(message.delete());
|
||||
}
|
||||
|
||||
return CompletableFuture.allOf(futures);
|
||||
return CompletableFutureUtil.combine(futures).thenApply(v -> null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public @NotNull CompletableFuture<ReceivedDiscordMessageCluster> editAll(SendableDiscordMessage newMessage) {
|
||||
CompletableFuture<ReceivedDiscordMessage>[] futures = new CompletableFuture[messages.size()];
|
||||
for (int i = 0; i < messages.size(); i++) {
|
||||
futures[i] = messages.get(i).edit(newMessage);
|
||||
List<CompletableFuture<ReceivedDiscordMessage>> futures = new ArrayList<>(messages.size());
|
||||
for (ReceivedDiscordMessage message : messages) {
|
||||
futures.add(message.edit(newMessage));
|
||||
}
|
||||
|
||||
return CompletableFuture.allOf(futures)
|
||||
.thenApply(v -> {
|
||||
List<ReceivedDiscordMessage> messages = new ArrayList<>();
|
||||
for (CompletableFuture<ReceivedDiscordMessage> future : futures) {
|
||||
// All the futures are done, so we're just going to get the results from all of them
|
||||
messages.add(
|
||||
future.join());
|
||||
}
|
||||
|
||||
return new ReceivedDiscordMessageClusterImpl(messages);
|
||||
});
|
||||
return CompletableFutureUtil.combine(futures).thenApply(ReceivedDiscordMessageClusterImpl::new);
|
||||
}
|
||||
}
|
||||
|
@ -44,6 +44,7 @@ import com.discordsrv.common.discord.api.entity.DiscordUserImpl;
|
||||
import com.discordsrv.common.discord.api.entity.channel.DiscordMessageChannelImpl;
|
||||
import com.discordsrv.common.discord.api.entity.guild.DiscordGuildMemberImpl;
|
||||
import com.discordsrv.common.function.OrDefault;
|
||||
import com.discordsrv.common.future.util.CompletableFutureUtil;
|
||||
import net.dv8tion.jda.api.entities.Member;
|
||||
import net.dv8tion.jda.api.entities.Message;
|
||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||
@ -260,9 +261,7 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
|
||||
public @NotNull CompletableFuture<Void> delete() {
|
||||
DiscordTextChannel textChannel = discordSRV.discordAPI().getTextChannelById(channelId).orElse(null);
|
||||
if (textChannel == null) {
|
||||
CompletableFuture<Void> future = new CompletableFuture<>();
|
||||
future.completeExceptionally(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL));
|
||||
return future;
|
||||
return CompletableFutureUtil.failed(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL));
|
||||
}
|
||||
|
||||
return textChannel.deleteMessageById(getId(), fromSelf && getWebhookUsername().isPresent());
|
||||
@ -276,9 +275,7 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
|
||||
|
||||
DiscordTextChannel textChannel = discordSRV.discordAPI().getTextChannelById(channelId).orElse(null);
|
||||
if (textChannel == null) {
|
||||
CompletableFuture<ReceivedDiscordMessage> future = new CompletableFuture<>();
|
||||
future.completeExceptionally(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL));
|
||||
return future;
|
||||
return CompletableFutureUtil.failed(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL));
|
||||
}
|
||||
|
||||
return textChannel.editMessageById(getId(), message);
|
||||
|
@ -37,6 +37,7 @@ import com.discordsrv.common.config.main.channels.MirroringConfig;
|
||||
import com.discordsrv.common.config.main.channels.base.BaseChannelConfig;
|
||||
import com.discordsrv.common.config.main.channels.base.IChannelConfig;
|
||||
import com.discordsrv.common.function.OrDefault;
|
||||
import com.discordsrv.common.future.util.CompletableFutureUtil;
|
||||
import com.discordsrv.common.module.type.AbstractModule;
|
||||
import com.github.benmanes.caffeine.cache.Cache;
|
||||
|
||||
@ -134,17 +135,12 @@ public class DiscordMessageMirroringModule extends AbstractModule<DiscordSRV> {
|
||||
}
|
||||
}
|
||||
|
||||
CompletableFuture.allOf(messageFutures.toArray(new CompletableFuture[0])).whenComplete((v2, t2) -> {
|
||||
Set<MessageReference> messages = new HashSet<>();
|
||||
for (CompletableFuture<ReceivedDiscordMessage> messageFuture : messageFutures) {
|
||||
if (messageFuture.isCompletedExceptionally()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
messages.add(getReference(messageFuture.join()));
|
||||
CompletableFutureUtil.combine(messageFutures).whenComplete((messages, t2) -> {
|
||||
Set<MessageReference> references = new HashSet<>();
|
||||
for (ReceivedDiscordMessage msg : messages) {
|
||||
references.add(getReference(msg));
|
||||
}
|
||||
|
||||
mapping.put(getReference(message), messages);
|
||||
mapping.put(getReference(message), references);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
@ -40,10 +40,7 @@ import net.kyori.adventure.text.Component;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionException;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
@ -126,7 +123,7 @@ public abstract class AbstractGameMessageModule<T> extends AbstractModule<Discor
|
||||
|
||||
CompletableFuture.allOf(messageFutures.keySet().toArray(new CompletableFuture[0]))
|
||||
.whenComplete((vo, t2) -> {
|
||||
List<ReceivedDiscordMessage> messages = new ArrayList<>();
|
||||
Set<ReceivedDiscordMessage> messages = new LinkedHashSet<>();
|
||||
for (Map.Entry<CompletableFuture<ReceivedDiscordMessage>, DiscordMessageChannel> entry : messageFutures.entrySet()) {
|
||||
CompletableFuture<ReceivedDiscordMessage> future = entry.getKey();
|
||||
if (future.isCompletedExceptionally()) {
|
||||
|
Loading…
Reference in New Issue
Block a user