mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2024-11-01 08:39:31 +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 org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.Set;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -38,7 +38,7 @@ public interface ReceivedDiscordMessageCluster {
|
|||||||
* @return the messages in this cluster
|
* @return the messages in this cluster
|
||||||
*/
|
*/
|
||||||
@NotNull
|
@NotNull
|
||||||
List<? extends ReceivedDiscordMessage> getMessages();
|
Set<? extends ReceivedDiscordMessage> getMessages();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Deletes all the messages from this cluster, one request per message.
|
* 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.DiscordGuildImpl;
|
||||||
import com.discordsrv.common.discord.api.entity.guild.DiscordRoleImpl;
|
import com.discordsrv.common.discord.api.entity.guild.DiscordRoleImpl;
|
||||||
import com.discordsrv.common.function.CheckedSupplier;
|
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.AsyncCacheLoader;
|
||||||
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
|
import com.github.benmanes.caffeine.cache.AsyncLoadingCache;
|
||||||
import com.github.benmanes.caffeine.cache.Expiry;
|
import com.github.benmanes.caffeine.cache.Expiry;
|
||||||
@ -320,9 +321,7 @@ public class DiscordAPIImpl implements DiscordAPI {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public <T> CompletableFuture<T> notReady() {
|
public <T> CompletableFuture<T> notReady() {
|
||||||
CompletableFuture<T> future = new CompletableFuture<>();
|
return CompletableFutureUtil.failed(new NotReadyException());
|
||||||
future.completeExceptionally(new NotReadyException());
|
|
||||||
return future;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -398,11 +397,9 @@ public class DiscordAPIImpl implements DiscordAPI {
|
|||||||
return notReady();
|
return notReady();
|
||||||
}
|
}
|
||||||
|
|
||||||
CompletableFuture<WebhookClient> future = new CompletableFuture<>();
|
|
||||||
TextChannel textChannel = jda.getTextChannelById(channelId);
|
TextChannel textChannel = jda.getTextChannelById(channelId);
|
||||||
if (textChannel == null) {
|
if (textChannel == null) {
|
||||||
future.completeExceptionally(new IllegalArgumentException("Channel could not be found"));
|
return CompletableFutureUtil.failed(new IllegalArgumentException("Channel could not be found"));
|
||||||
return future;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return textChannel.retrieveWebhooks().submit().thenApply(webhooks -> {
|
return textChannel.retrieveWebhooks().submit().thenApply(webhooks -> {
|
||||||
@ -422,9 +419,7 @@ public class DiscordAPIImpl implements DiscordAPI {
|
|||||||
return hook;
|
return hook;
|
||||||
}).thenCompose(webhook -> {
|
}).thenCompose(webhook -> {
|
||||||
if (webhook != null) {
|
if (webhook != null) {
|
||||||
CompletableFuture<Webhook> completableFuture = new CompletableFuture<>();
|
return CompletableFuture.completedFuture(webhook);
|
||||||
completableFuture.complete(webhook);
|
|
||||||
return completableFuture;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return textChannel.createWebhook("DiscordSRV").submit();
|
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.ReceivedDiscordMessage;
|
||||||
import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessageCluster;
|
import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessageCluster;
|
||||||
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
|
import com.discordsrv.api.discord.api.entity.message.SendableDiscordMessage;
|
||||||
|
import com.discordsrv.common.future.util.CompletableFutureUtil;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
|
|
||||||
public class ReceivedDiscordMessageClusterImpl implements ReceivedDiscordMessageCluster {
|
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;
|
this.messages = messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull List<ReceivedDiscordMessage> getMessages() {
|
public @NotNull Set<ReceivedDiscordMessage> getMessages() {
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull CompletableFuture<Void> deleteAll() {
|
public @NotNull CompletableFuture<Void> deleteAll() {
|
||||||
CompletableFuture<Void>[] futures = new CompletableFuture[messages.size()];
|
List<CompletableFuture<Void>> futures = new ArrayList<>(messages.size());
|
||||||
for (int i = 0; i < messages.size(); i++) {
|
for (ReceivedDiscordMessage message : messages) {
|
||||||
futures[i] = messages.get(i).delete();
|
futures.add(message.delete());
|
||||||
}
|
}
|
||||||
|
|
||||||
return CompletableFuture.allOf(futures);
|
return CompletableFutureUtil.combine(futures).thenApply(v -> null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull CompletableFuture<ReceivedDiscordMessageCluster> editAll(SendableDiscordMessage newMessage) {
|
public @NotNull CompletableFuture<ReceivedDiscordMessageCluster> editAll(SendableDiscordMessage newMessage) {
|
||||||
CompletableFuture<ReceivedDiscordMessage>[] futures = new CompletableFuture[messages.size()];
|
List<CompletableFuture<ReceivedDiscordMessage>> futures = new ArrayList<>(messages.size());
|
||||||
for (int i = 0; i < messages.size(); i++) {
|
for (ReceivedDiscordMessage message : messages) {
|
||||||
futures[i] = messages.get(i).edit(newMessage);
|
futures.add(message.edit(newMessage));
|
||||||
}
|
}
|
||||||
|
|
||||||
return CompletableFuture.allOf(futures)
|
return CompletableFutureUtil.combine(futures).thenApply(ReceivedDiscordMessageClusterImpl::new);
|
||||||
.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);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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.channel.DiscordMessageChannelImpl;
|
||||||
import com.discordsrv.common.discord.api.entity.guild.DiscordGuildMemberImpl;
|
import com.discordsrv.common.discord.api.entity.guild.DiscordGuildMemberImpl;
|
||||||
import com.discordsrv.common.function.OrDefault;
|
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.Member;
|
||||||
import net.dv8tion.jda.api.entities.Message;
|
import net.dv8tion.jda.api.entities.Message;
|
||||||
import net.dv8tion.jda.api.entities.MessageEmbed;
|
import net.dv8tion.jda.api.entities.MessageEmbed;
|
||||||
@ -260,9 +261,7 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple
|
|||||||
public @NotNull CompletableFuture<Void> delete() {
|
public @NotNull CompletableFuture<Void> delete() {
|
||||||
DiscordTextChannel textChannel = discordSRV.discordAPI().getTextChannelById(channelId).orElse(null);
|
DiscordTextChannel textChannel = discordSRV.discordAPI().getTextChannelById(channelId).orElse(null);
|
||||||
if (textChannel == null) {
|
if (textChannel == null) {
|
||||||
CompletableFuture<Void> future = new CompletableFuture<>();
|
return CompletableFutureUtil.failed(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL));
|
||||||
future.completeExceptionally(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL));
|
|
||||||
return future;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return textChannel.deleteMessageById(getId(), fromSelf && getWebhookUsername().isPresent());
|
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);
|
DiscordTextChannel textChannel = discordSRV.discordAPI().getTextChannelById(channelId).orElse(null);
|
||||||
if (textChannel == null) {
|
if (textChannel == null) {
|
||||||
CompletableFuture<ReceivedDiscordMessage> future = new CompletableFuture<>();
|
return CompletableFutureUtil.failed(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL));
|
||||||
future.completeExceptionally(new RestErrorResponseException(ErrorResponse.UNKNOWN_CHANNEL));
|
|
||||||
return future;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return textChannel.editMessageById(getId(), message);
|
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.BaseChannelConfig;
|
||||||
import com.discordsrv.common.config.main.channels.base.IChannelConfig;
|
import com.discordsrv.common.config.main.channels.base.IChannelConfig;
|
||||||
import com.discordsrv.common.function.OrDefault;
|
import com.discordsrv.common.function.OrDefault;
|
||||||
|
import com.discordsrv.common.future.util.CompletableFutureUtil;
|
||||||
import com.discordsrv.common.module.type.AbstractModule;
|
import com.discordsrv.common.module.type.AbstractModule;
|
||||||
import com.github.benmanes.caffeine.cache.Cache;
|
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) -> {
|
CompletableFutureUtil.combine(messageFutures).whenComplete((messages, t2) -> {
|
||||||
Set<MessageReference> messages = new HashSet<>();
|
Set<MessageReference> references = new HashSet<>();
|
||||||
for (CompletableFuture<ReceivedDiscordMessage> messageFuture : messageFutures) {
|
for (ReceivedDiscordMessage msg : messages) {
|
||||||
if (messageFuture.isCompletedExceptionally()) {
|
references.add(getReference(msg));
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
|
mapping.put(getReference(message), references);
|
||||||
messages.add(getReference(messageFuture.join()));
|
|
||||||
}
|
|
||||||
|
|
||||||
mapping.put(getReference(message), messages);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -40,10 +40,7 @@ import net.kyori.adventure.text.Component;
|
|||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.LinkedHashMap;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.CompletableFuture;
|
import java.util.concurrent.CompletableFuture;
|
||||||
import java.util.concurrent.CompletionException;
|
import java.util.concurrent.CompletionException;
|
||||||
import java.util.concurrent.CopyOnWriteArrayList;
|
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]))
|
CompletableFuture.allOf(messageFutures.keySet().toArray(new CompletableFuture[0]))
|
||||||
.whenComplete((vo, t2) -> {
|
.whenComplete((vo, t2) -> {
|
||||||
List<ReceivedDiscordMessage> messages = new ArrayList<>();
|
Set<ReceivedDiscordMessage> messages = new LinkedHashSet<>();
|
||||||
for (Map.Entry<CompletableFuture<ReceivedDiscordMessage>, DiscordMessageChannel> entry : messageFutures.entrySet()) {
|
for (Map.Entry<CompletableFuture<ReceivedDiscordMessage>, DiscordMessageChannel> entry : messageFutures.entrySet()) {
|
||||||
CompletableFuture<ReceivedDiscordMessage> future = entry.getKey();
|
CompletableFuture<ReceivedDiscordMessage> future = entry.getKey();
|
||||||
if (future.isCompletedExceptionally()) {
|
if (future.isCompletedExceptionally()) {
|
||||||
|
Loading…
Reference in New Issue
Block a user