All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.supplier.StoreEntitySupplier.kt Maven / Gradle / Ivy

There is a newer version: 0.15.0
Show newest version
package dev.kord.core.supplier

import dev.kord.cache.api.DataCache
import dev.kord.cache.api.put
import dev.kord.common.entity.Snowflake
import dev.kord.core.entity.*
import dev.kord.core.entity.application.ApplicationCommandPermissions
import dev.kord.core.entity.application.GlobalApplicationCommand
import dev.kord.core.entity.application.GuildApplicationCommand
import dev.kord.core.entity.automoderation.AutoModerationRule
import dev.kord.core.entity.channel.Channel
import dev.kord.core.entity.channel.TopGuildChannel
import dev.kord.core.entity.channel.thread.ThreadChannel
import dev.kord.core.entity.channel.thread.ThreadMember
import dev.kord.core.entity.interaction.followup.FollowupMessage
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.onEach
import kotlinx.datetime.Instant

/**
 * [EntitySupplier] that delegates to another [EntitySupplier] to resolve entities.
 *
 * Resolved entities will always be stored in [cache] if it wasn't null or empty for flows.
 */
public class StoreEntitySupplier(
    private val supplier: EntitySupplier,
    private val cache: DataCache,
) : EntitySupplier {

    override val guilds: Flow
        get() = storeOnEach(supplier.guilds) { it.data }

    override val regions: Flow
        get() = storeOnEach(supplier.regions) { it.data }


    override suspend fun getGuildOrNull(id: Snowflake): Guild? {
        return storeAndReturn(supplier.getGuildOrNull(id)) { it.data }
    }

    override suspend fun getGuildPreviewOrNull(guildId: Snowflake): GuildPreview? {
        return storeAndReturn(supplier.getGuildPreviewOrNull(guildId)) { it.data }
    }

    override suspend fun getGuildWidgetOrNull(guildId: Snowflake): GuildWidget? {
        return storeAndReturn(supplier.getGuildWidgetOrNull(guildId)) { it.data }
    }

    override suspend fun getChannelOrNull(id: Snowflake): Channel? {
        return storeAndReturn(supplier.getChannelOrNull(id)) { it.data }
    }

    override fun getGuildChannels(guildId: Snowflake): Flow {
        return storeOnEach(supplier.getGuildChannels(guildId)) { it.data }

    }

    override fun getChannelPins(channelId: Snowflake): Flow {
        return storeOnEach(supplier.getChannelPins(channelId)) { it.data }

    }

    override suspend fun getMemberOrNull(guildId: Snowflake, userId: Snowflake): Member? {
        return storeAndReturn(supplier.getMemberOrNull(guildId, userId)) { it.data }
    }

    override suspend fun getMessageOrNull(channelId: Snowflake, messageId: Snowflake): Message? {
        return storeAndReturn(supplier.getMessageOrNull(channelId, messageId)) { it.data }
    }

    override fun getMessagesAfter(messageId: Snowflake, channelId: Snowflake, limit: Int?): Flow {
        return storeOnEach(supplier.getMessagesAfter(messageId, channelId, limit)) { it.data }
    }

    override fun getMessagesBefore(messageId: Snowflake, channelId: Snowflake, limit: Int?): Flow {
        return storeOnEach(supplier.getMessagesBefore(messageId, channelId, limit)) { it.data }
    }

    override fun getMessagesAround(messageId: Snowflake, channelId: Snowflake, limit: Int): Flow {
        return storeOnEach(supplier.getMessagesAround(messageId, channelId, limit)) { it.data }
    }

    override suspend fun getSelfOrNull(): User? {
        return storeAndReturn(supplier.getSelfOrNull()) { it.data }
    }

    override suspend fun getUserOrNull(id: Snowflake): User? {
        return storeAndReturn(supplier.getUserOrNull(id)) { it.data }
    }

    override suspend fun getRoleOrNull(guildId: Snowflake, roleId: Snowflake): Role? {
        return storeAndReturn(supplier.getRoleOrNull(guildId, roleId)) { it.data }
    }

    override fun getGuildRoles(guildId: Snowflake): Flow {
        return storeOnEach(supplier.getGuildRoles(guildId)) { it.data }
    }

    override suspend fun getGuildBanOrNull(guildId: Snowflake, userId: Snowflake): Ban? {
        return storeAndReturn(supplier.getGuildBanOrNull(guildId, userId)) { it.data }
    }

    override fun getGuildBans(guildId: Snowflake, limit: Int?): Flow {
        return storeOnEach(supplier.getGuildBans(guildId, limit)) { it.data }
    }

    override fun getGuildMembers(guildId: Snowflake, limit: Int?): Flow {
        return storeOnEach(supplier.getGuildMembers(guildId, limit)) { it.data }
    }

    override fun getGuildVoiceRegions(guildId: Snowflake): Flow {
        return storeOnEach(supplier.getGuildVoiceRegions(guildId)) { it.data }
    }

    override suspend fun getEmojiOrNull(guildId: Snowflake, emojiId: Snowflake): GuildEmoji? {
        return storeAndReturn(supplier.getEmojiOrNull(guildId, emojiId)) { it.data }
    }

    override fun getEmojis(guildId: Snowflake): Flow {
        return storeOnEach(supplier.getEmojis(guildId)) { it.data }

    }

    override fun getCurrentUserGuilds(limit: Int?): Flow {
        return storeOnEach(supplier.getCurrentUserGuilds(limit)) { it.data }

    }

    override fun getChannelWebhooks(channelId: Snowflake): Flow {
        return storeOnEach(supplier.getChannelWebhooks(channelId)) { it.data }
    }

    override fun getGuildWebhooks(guildId: Snowflake): Flow {
        return storeOnEach(supplier.getGuildWebhooks(guildId)) { it.data }
    }

    override suspend fun getWebhookOrNull(id: Snowflake): Webhook? {
        return storeAndReturn(supplier.getWebhookOrNull(id)) { it.data }
    }

    override suspend fun getWebhookWithTokenOrNull(id: Snowflake, token: String): Webhook? {
        return storeAndReturn(supplier.getWebhookWithTokenOrNull(id, token)) { it.data }
    }

    override suspend fun getWebhookMessageOrNull(
        webhookId: Snowflake,
        token: String,
        messageId: Snowflake,
        threadId: Snowflake?,
    ): Message? {
        return storeAndReturn(supplier.getWebhookMessageOrNull(webhookId, token, messageId, threadId)) { it.data }
    }

    override suspend fun getTemplateOrNull(code: String): Template? {
        return storeAndReturn(supplier.getTemplateOrNull(code)) { it.data }
    }

    override fun getTemplates(guildId: Snowflake): Flow