Fix some bugs related to threads

This commit is contained in:
Vankka 2022-03-06 21:18:20 +02:00
parent 2cc92d6179
commit 373e866346
No known key found for this signature in database
GPG Key ID: 6E50CB7A29B96AD0
4 changed files with 36 additions and 10 deletions

View File

@ -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<? extends DiscordMessageChannel> 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<DiscordDMChannel> 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<DiscordTextChannel> 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<DiscordThreadChannel> 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<DiscordGuild> 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<DiscordUser> 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
*/

View File

@ -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<DiscordThreadChannel> 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<DiscordThreadChannel> getCachedThreadChannelById(long id) {
return discordSRV.jda()
.map(jda -> jda.getThreadChannelById(id))
.map(threadChannel -> new DiscordThreadChannelImpl(discordSRV, threadChannel));
}
@Override
public @NotNull Optional<DiscordGuild> getGuildById(long id) {
return discordSRV.jda()

View File

@ -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<WebhookClient> 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();

View File

@ -111,7 +111,7 @@ public class DiscordMessageMirroringModule extends AbstractModule<DiscordSRV> {
}, futures);
}
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).whenComplete((v, t) -> {
CompletableFutureUtil.combine(futures).whenComplete((v, t) -> {
List<CompletableFuture<Pair<ReceivedDiscordMessage, OrDefault<MirroringConfig>>>> messageFutures = new ArrayList<>();
for (Pair<DiscordMessageChannel, OrDefault<MirroringConfig>> pair : mirrorChannels) {
DiscordMessageChannel mirrorChannel = pair.getKey();
@ -239,7 +239,7 @@ public class DiscordMessageMirroringModule extends AbstractModule<DiscordSRV> {
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());
}