mirror of
https://github.com/Sheldan/abstracto.git
synced 2026-03-24 13:44:33 +00:00
[AB-210] fixing some issues in which futures were not completed, when lists were empty
fixing async issue with caching reactions adding a few more log statements to caching
This commit is contained in:
@@ -4,7 +4,6 @@ import dev.sheldan.abstracto.core.metric.OkHttpMetrics;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.dv8tion.jda.api.JDA;
|
import net.dv8tion.jda.api.JDA;
|
||||||
import net.dv8tion.jda.api.JDABuilder;
|
import net.dv8tion.jda.api.JDABuilder;
|
||||||
import net.dv8tion.jda.api.requests.GatewayIntent;
|
|
||||||
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
import net.dv8tion.jda.api.utils.MemberCachePolicy;
|
||||||
import net.dv8tion.jda.internal.utils.IOUtil;
|
import net.dv8tion.jda.internal.utils.IOUtil;
|
||||||
import okhttp3.OkHttpClient;
|
import okhttp3.OkHttpClient;
|
||||||
@@ -26,7 +25,7 @@ public class BotServiceBean implements BotService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void login() throws LoginException {
|
public void login() throws LoginException {
|
||||||
JDABuilder builder = JDABuilder.create(System.getenv("TOKEN"), GatewayIntent.GUILD_MEMBERS, GUILD_VOICE_STATES,
|
JDABuilder builder = JDABuilder.create(System.getenv("TOKEN"), GUILD_VOICE_STATES,
|
||||||
GUILD_EMOJIS, GUILD_MEMBERS, GUILD_MESSAGE_REACTIONS, GUILD_MESSAGES,
|
GUILD_EMOJIS, GUILD_MEMBERS, GUILD_MESSAGE_REACTIONS, GUILD_MESSAGES,
|
||||||
GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGE_REACTIONS, DIRECT_MESSAGES, GUILD_PRESENCES);
|
GUILD_MESSAGE_REACTIONS, DIRECT_MESSAGE_REACTIONS, DIRECT_MESSAGES, GUILD_PRESENCES);
|
||||||
|
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
|||||||
}
|
}
|
||||||
List<MessageEmbed.Field> fields = embed.getFields();
|
List<MessageEmbed.Field> fields = embed.getFields();
|
||||||
if(!fields.isEmpty()) {
|
if(!fields.isEmpty()) {
|
||||||
|
log.trace("Caching {} fields.", fields.size());
|
||||||
List<CachedEmbedField> cachedEmbedFields = new ArrayList<>();
|
List<CachedEmbedField> cachedEmbedFields = new ArrayList<>();
|
||||||
fields.forEach(field -> {
|
fields.forEach(field -> {
|
||||||
CachedEmbedField build = CachedEmbedField
|
CachedEmbedField build = CachedEmbedField
|
||||||
@@ -159,16 +160,27 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
|||||||
|
|
||||||
List<ServerUser> aUsers = new ArrayList<>();
|
List<ServerUser> aUsers = new ArrayList<>();
|
||||||
users.forEachAsync(user -> {
|
users.forEachAsync(user -> {
|
||||||
|
log.trace("Loading user {} for reaction.", user.getIdLong());
|
||||||
concreteSelf.loadUser(reaction, aUsers, user);
|
concreteSelf.loadUser(reaction, aUsers, user);
|
||||||
return false;
|
return false;
|
||||||
}).thenAccept(o -> future.complete(builder.build()))
|
}).thenAccept(o -> {
|
||||||
.exceptionally(throwable -> {
|
log.trace("Users have been loaded. Completing future.");
|
||||||
log.error("Failed to load reaction users.", throwable);
|
builder.users(aUsers);
|
||||||
return null;
|
builder.self(reaction.isSelf());
|
||||||
});
|
builder.emote(getCachedEmoteFromEmote(reaction.getReactionEmote(), reaction.getGuild()));
|
||||||
builder.users(aUsers);
|
future.complete(builder.build());
|
||||||
builder.self(reaction.isSelf());
|
})
|
||||||
builder.emote(getCachedEmoteFromEmote(reaction.getReactionEmote(), reaction.getGuild()));
|
.exceptionally(throwable -> {
|
||||||
|
log.error("Failed to load reaction users.", throwable);
|
||||||
|
return null;
|
||||||
|
});
|
||||||
|
if(users.isEmpty()) {
|
||||||
|
log.trace("Reaction had no users. Completing future anyway.");
|
||||||
|
builder.users(aUsers);
|
||||||
|
builder.self(reaction.isSelf());
|
||||||
|
builder.emote(getCachedEmoteFromEmote(reaction.getReactionEmote(), reaction.getGuild()));
|
||||||
|
future.complete(builder.build());
|
||||||
|
}
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -183,22 +195,25 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
|||||||
public CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message) {
|
public CompletableFuture<CachedMessage> buildCachedMessageFromMessage(Message message) {
|
||||||
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
|
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
|
||||||
List<CachedAttachment> attachments = new ArrayList<>();
|
List<CachedAttachment> attachments = new ArrayList<>();
|
||||||
|
log.trace("Caching {} attachments.", message.getAttachments().size());
|
||||||
message.getAttachments().forEach(attachment ->
|
message.getAttachments().forEach(attachment ->
|
||||||
attachments.add(getCachedAttachment(attachment))
|
attachments.add(getCachedAttachment(attachment))
|
||||||
);
|
);
|
||||||
|
log.trace("Caching {} embeds.", message.getEmbeds().size());
|
||||||
List<CachedEmbed> embeds = new ArrayList<>();
|
List<CachedEmbed> embeds = new ArrayList<>();
|
||||||
message.getEmbeds().forEach(embed ->
|
message.getEmbeds().forEach(embed ->
|
||||||
embeds.add(getCachedEmbedFromEmbed(embed))
|
embeds.add(getCachedEmbedFromEmbed(embed))
|
||||||
);
|
);
|
||||||
|
|
||||||
|
log.trace("Caching {} emotes.", message.getEmbeds().size());
|
||||||
List<CachedEmote> emotes = new ArrayList<>();
|
List<CachedEmote> emotes = new ArrayList<>();
|
||||||
if(message.isFromGuild()) {
|
if(message.isFromGuild()) {
|
||||||
message.getEmotesBag().forEach(emote -> emotes.add(getCachedEmoteFromEmote(emote, message.getGuild())));
|
message.getEmotesBag().forEach(emote -> emotes.add(getCachedEmoteFromEmote(emote, message.getGuild())));
|
||||||
}
|
}
|
||||||
|
|
||||||
List<CompletableFuture<CachedReactions>> futures = new ArrayList<>();
|
List<CompletableFuture<CachedReactions>> futures = new ArrayList<>();
|
||||||
|
log.trace("Caching {} reactions.", message.getReactions().size());
|
||||||
message.getReactions().forEach(messageReaction -> futures.add(getCachedReactionFromReaction(messageReaction)));
|
message.getReactions().forEach(messageReaction -> futures.add(getCachedReactionFromReaction(messageReaction)));
|
||||||
|
|
||||||
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(aVoid ->
|
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenAccept(aVoid ->
|
||||||
{
|
{
|
||||||
CachedAuthor cachedAuthor = CachedAuthor.builder().authorId(message.getAuthor().getIdLong()).isBot(message.getAuthor().isBot()).build();
|
CachedAuthor cachedAuthor = CachedAuthor.builder().authorId(message.getAuthor().getIdLong()).isBot(message.getAuthor().isBot()).build();
|
||||||
@@ -222,6 +237,23 @@ public class CacheEntityServiceBean implements CacheEntityService {
|
|||||||
log.error("Failed to load reactions for message {}. ", message.getId(), throwable);
|
log.error("Failed to load reactions for message {}. ", message.getId(), throwable);
|
||||||
return null;
|
return null;
|
||||||
});
|
});
|
||||||
|
if(message.getReactions().isEmpty()) {
|
||||||
|
CachedAuthor cachedAuthor = CachedAuthor.builder().authorId(message.getAuthor().getIdLong()).isBot(message.getAuthor().isBot()).build();
|
||||||
|
CachedMessage.CachedMessageBuilder builder = CachedMessage.builder()
|
||||||
|
.author(cachedAuthor)
|
||||||
|
.messageId(message.getIdLong())
|
||||||
|
.channelId(message.getChannel().getIdLong())
|
||||||
|
.content(message.getContentRaw())
|
||||||
|
.embeds(embeds)
|
||||||
|
.emotes(emotes)
|
||||||
|
.timeCreated(Instant.from(message.getTimeCreated()))
|
||||||
|
.attachments(attachments);
|
||||||
|
if(message.isFromGuild()) {
|
||||||
|
builder.serverId(message.getGuild().getIdLong());
|
||||||
|
}
|
||||||
|
future.complete(builder
|
||||||
|
.build());
|
||||||
|
}
|
||||||
return future;
|
return future;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ public class MessageCacheBean implements MessageCache {
|
|||||||
@Override
|
@Override
|
||||||
@CachePut(key = "#message.id")
|
@CachePut(key = "#message.id")
|
||||||
public CompletableFuture<CachedMessage> putMessageInCache(Message message) {
|
public CompletableFuture<CachedMessage> putMessageInCache(Message message) {
|
||||||
log.trace("Adding message {} to cache", message.getId());
|
log.info("Adding message {} to cache.", message.getId());
|
||||||
return cacheEntityService.buildCachedMessageFromMessage(message);
|
return cacheEntityService.buildCachedMessageFromMessage(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,26 +50,27 @@ public class MessageCacheBean implements MessageCache {
|
|||||||
@Override
|
@Override
|
||||||
@CachePut(key = "#message.messageId.toString()")
|
@CachePut(key = "#message.messageId.toString()")
|
||||||
public CompletableFuture<CachedMessage> putMessageInCache(CachedMessage message) {
|
public CompletableFuture<CachedMessage> putMessageInCache(CachedMessage message) {
|
||||||
log.trace("Adding cached message to cache");
|
log.info("Adding cached message {} to cache.", message.getMessageId());
|
||||||
return CompletableFuture.completedFuture(message);
|
return CompletableFuture.completedFuture(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "#message.id")
|
@Cacheable(key = "#message.id")
|
||||||
public CompletableFuture<CachedMessage> getMessageFromCache(Message message) {
|
public CompletableFuture<CachedMessage> getMessageFromCache(Message message) {
|
||||||
log.trace("Retrieving message {}", message.getId());
|
log.trace("Retrieving message {}.", message.getId());
|
||||||
return getMessageFromCache(message.getGuild().getIdLong(), message.getChannel().getIdLong(), message.getIdLong());
|
return getMessageFromCache(message.getGuild().getIdLong(), message.getChannel().getIdLong(), message.getIdLong());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Cacheable(key = "#messageId.toString()")
|
@Cacheable(key = "#messageId.toString()")
|
||||||
public CompletableFuture<CachedMessage> getMessageFromCache(Long guildId, Long textChannelId, Long messageId) {
|
public CompletableFuture<CachedMessage> getMessageFromCache(Long guildId, Long textChannelId, Long messageId) {
|
||||||
log.trace("Retrieving message {} with parameters.", messageId);
|
log.info("Retrieving message {}.", messageId);
|
||||||
return concreteSelf.loadMessage(guildId, textChannelId, messageId);
|
return concreteSelf.loadMessage(guildId, textChannelId, messageId);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public CompletableFuture<CachedMessage> loadMessage(Long guildId, Long textChannelId, Long messageId) {
|
public CompletableFuture<CachedMessage> loadMessage(Long guildId, Long textChannelId, Long messageId) {
|
||||||
|
log.info("Loading message {} from channel {} in server {}.", messageId, textChannelId, guildId);
|
||||||
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
|
CompletableFuture<CachedMessage> future = new CompletableFuture<>();
|
||||||
Optional<Guild> guildOptional = guildService.getGuildByIdOptional(guildId);
|
Optional<Guild> guildOptional = guildService.getGuildByIdOptional(guildId);
|
||||||
if(guildOptional.isPresent()) {
|
if(guildOptional.isPresent()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user