From 373e866346b537ffb826e9341a97696d31a35010 Mon Sep 17 00:00:00 2001 From: Vankka Date: Sun, 6 Mar 2022 21:18:20 +0200 Subject: [PATCH] Fix some bugs related to threads --- .../api/discord/api/DiscordAPI.java | 23 +++++++++++++------ .../common/discord/api/DiscordAPIImpl.java | 13 +++++++++++ .../message/ReceivedDiscordMessageImpl.java | 6 ++++- .../DiscordMessageMirroringModule.java | 4 ++-- 4 files changed, 36 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/com/discordsrv/api/discord/api/DiscordAPI.java b/api/src/main/java/com/discordsrv/api/discord/api/DiscordAPI.java index d5b40d37..730d7843 100644 --- a/api/src/main/java/com/discordsrv/api/discord/api/DiscordAPI.java +++ b/api/src/main/java/com/discordsrv/api/discord/api/DiscordAPI.java @@ -27,6 +27,7 @@ import com.discordsrv.api.discord.api.entity.DiscordUser; import com.discordsrv.api.discord.api.entity.channel.DiscordDMChannel; import com.discordsrv.api.discord.api.entity.channel.DiscordMessageChannel; import com.discordsrv.api.discord.api.entity.channel.DiscordTextChannel; +import com.discordsrv.api.discord.api.entity.channel.DiscordThreadChannel; import com.discordsrv.api.discord.api.entity.guild.DiscordGuild; import com.discordsrv.api.discord.api.entity.guild.DiscordRole; import org.jetbrains.annotations.NotNull; @@ -40,7 +41,7 @@ import java.util.concurrent.CompletableFuture; public interface DiscordAPI { /** - * Gets a Discord message channel by id, the provided entity should not be cached. + * Gets a Discord message channel by id, the provided entity should not be stored for long periods of time. * @param id the id for the message channel * @return the message channel */ @@ -48,7 +49,7 @@ public interface DiscordAPI { Optional getMessageChannelById(long id); /** - * Gets a Discord direct message channel by id, the provided entity should not be cached. + * Gets a Discord direct message channel by id, the provided entity should not be stored for long periods of time. * @param id the id for the direct message channel * @return the direct message channel */ @@ -56,7 +57,7 @@ public interface DiscordAPI { Optional getDirectMessageChannelById(long id); /** - * Gets a Discord text channel by id, the provided entity should not be cached. + * Gets a Discord text channel by id, the provided entity should not be stored for long periods of time. * @param id the id for the text channel * @return the text channel */ @@ -64,7 +65,15 @@ public interface DiscordAPI { Optional getTextChannelById(long id); /** - * Gets a Discord server by id, the provided entity should not be cached. + * Gets a Discord thread channel by id from the cache, the provided entity should not be stored for long periods of time. + * @param id the id for the thread channel + * @return the thread channel + */ + @NotNull + Optional getCachedThreadChannelById(long id); + + /** + * Gets a Discord server by id, the provided entity should not be stored for long periods of time. * @param id the id for the Discord server * @return the Discord server */ @@ -72,7 +81,7 @@ public interface DiscordAPI { Optional getGuildById(long id); /** - * Gets a Discord user by id, the provided entity should not be cached. + * Gets a Discord user by id, the provided entity should not be stored for long periods of time. * This will always return an empty optional if {@link #isUserCachingEnabled()} returns {@code false}. * @param id the id for the Discord user * @return the Discord user @@ -82,7 +91,7 @@ public interface DiscordAPI { Optional getUserById(long id); /** - * Looks up a Discord user by id from Discord, the provided entity can be cached but will not be updated if the entity changes on Discord. + * Looks up a Discord user by id from Discord, the provided entity should not be stored for long periods of time. * @param id the id for the Discord user * @return a future that will result in a {@link DiscordUser} for the id or throw a */ @@ -96,7 +105,7 @@ public interface DiscordAPI { boolean isUserCachingEnabled(); /** - * Gets a Discord role by id, the provided entity should not be cached. + * Gets a Discord role by id, the provided entity should not be stored for long periods of time. * @param id the id for the Discord role * @return the Discord role */ diff --git a/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java b/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java index 25de2557..9a039137 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/DiscordAPIImpl.java @@ -38,6 +38,7 @@ import com.discordsrv.common.config.main.channels.base.ThreadConfig; import com.discordsrv.common.discord.api.entity.DiscordUserImpl; import com.discordsrv.common.discord.api.entity.channel.DiscordDMChannelImpl; import com.discordsrv.common.discord.api.entity.channel.DiscordTextChannelImpl; +import com.discordsrv.common.discord.api.entity.channel.DiscordThreadChannelImpl; import com.discordsrv.common.discord.api.entity.guild.DiscordGuildImpl; import com.discordsrv.common.discord.api.entity.guild.DiscordRoleImpl; import com.discordsrv.common.function.CheckedSupplier; @@ -331,6 +332,11 @@ public class DiscordAPIImpl implements DiscordAPI { return textChannel; } + Optional threadChannel = getCachedThreadChannelById(id); + if (threadChannel.isPresent()) { + return threadChannel; + } + return getDirectMessageChannelById(id); } @@ -348,6 +354,13 @@ public class DiscordAPIImpl implements DiscordAPI { .map(textChannel -> new DiscordTextChannelImpl(discordSRV, textChannel)); } + @Override + public @NotNull Optional getCachedThreadChannelById(long id) { + return discordSRV.jda() + .map(jda -> jda.getThreadChannelById(id)) + .map(threadChannel -> new DiscordThreadChannelImpl(discordSRV, threadChannel)); + } + @Override public @NotNull Optional getGuildById(long id) { return discordSRV.jda() diff --git a/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java b/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java index d88e767e..2f3d11a6 100644 --- a/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java +++ b/common/src/main/java/com/discordsrv/common/discord/api/entity/message/ReceivedDiscordMessageImpl.java @@ -29,6 +29,7 @@ import com.discordsrv.api.discord.api.entity.DiscordUser; import com.discordsrv.api.discord.api.entity.channel.DiscordDMChannel; import com.discordsrv.api.discord.api.entity.channel.DiscordMessageChannel; import com.discordsrv.api.discord.api.entity.channel.DiscordTextChannel; +import com.discordsrv.api.discord.api.entity.channel.DiscordThreadChannel; import com.discordsrv.api.discord.api.entity.guild.DiscordGuildMember; import com.discordsrv.api.discord.api.entity.message.DiscordMessageEmbed; import com.discordsrv.api.discord.api.entity.message.ReceivedDiscordMessage; @@ -81,7 +82,10 @@ public class ReceivedDiscordMessageImpl extends SendableDiscordMessageImpl imple if (webhookMessage) { CompletableFuture clientFuture = discordSRV.discordAPI() .getCachedClients() - .getIfPresent(message.getChannel().getIdLong()); + .getIfPresent(channel instanceof DiscordThreadChannel + ? ((DiscordThreadChannel) channel).getParentChannel().getId() + : channel.getId() + ); if (clientFuture != null) { long clientId = clientFuture.join().getId(); diff --git a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java index 3505994f..65ed54c8 100644 --- a/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java +++ b/common/src/main/java/com/discordsrv/common/messageforwarding/discord/DiscordMessageMirroringModule.java @@ -111,7 +111,7 @@ public class DiscordMessageMirroringModule extends AbstractModule { }, futures); } - CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).whenComplete((v, t) -> { + CompletableFutureUtil.combine(futures).whenComplete((v, t) -> { List>>> messageFutures = new ArrayList<>(); for (Pair> pair : mirrorChannels) { DiscordMessageChannel mirrorChannel = pair.getKey(); @@ -239,7 +239,7 @@ public class DiscordMessageMirroringModule extends AbstractModule { return getCacheKey(channel.getId(), 0L, messageId); } else if (channel instanceof DiscordThreadChannel) { long parentId = ((DiscordThreadChannel) channel).getParentChannel().getId(); - return getCacheKey(channel.getId(), parentId, messageId); + return getCacheKey(parentId, channel.getId(), messageId); } throw new IllegalStateException("Unexpected channel type: " + channel.getClass().getName()); }