diff --git a/README.md b/README.md index 14ce77be..d94dd38d 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Lily makes use of the following tools and frameworks. * [Kord](https://github.com/kordlib/kord), the Kotlin API for Discord. * [KordEx](https://github.com/Kord-Extensions/kord-extensions), an integrated commands and extensions framework for Kord. * KordEx's [Phishing](https://github.com/Kord-Extensions/kord-extensions/tree/develop/extra-modules/extra-phishing), [PluralKit](https://github.com/Kord-Extensions/kord-extensions/tree/develop/extra-modules/extra-pluralkit) and [Unsafe](https://github.com/Kord-Extensions/kord-extensions/tree/develop/modules/unsafe) modules. -* [MongoDB](https://www.mongodb.com/) and [KMongo](https://litote.org/kmongo/) +* [MongoDB](https://www.mongodb.com/)'s [Kotlin Driver](https://www.mongodb.com/docs/drivers/kotlin/) to manage the database. * [Logback](https://github.com/qos-ch/logback), a library that makes logging prettier * [Kotlin Logging](https://github.com/MicroUtils/kotlin-logging), a lightweight logging that wraps SLF4J with kotlin extensions diff --git a/build.gradle.kts b/build.gradle.kts index 23e36554..f55c55f0 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -53,6 +53,7 @@ dependencies { implementation(libs.kord.extensions.phishing) implementation(libs.kord.extensions.pluralkit) implementation(libs.kord.extensions.unsafe) + implementation(libs.kord.extensions.mongodb) implementation(libs.kotlin.stdlib) @@ -64,7 +65,7 @@ dependencies { implementation(libs.github.api) // KMongo - implementation(libs.kmongo) + implementation(libs.mongo.driver) // Cozy's welcome module implementation(libs.cozy.welcome) diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 62ed2b78..68e9ef53 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,11 +8,12 @@ grgit = "5.2.0" blossom = "1.3.1" # Libraries -kord-extensions = "1.5.9-20230809.104126-1" +#kord-extensions = "1.5.9-20230820.204324-8" +kord-extensions = "1.5.9-SNAPSHOT" logging = "5.1.0" logback = "1.4.9" github-api = "1.315" -kmongo = "4.9.0" +mongo-driver = "4.10.1" cozy-welcome = "1.0.1-SNAPSHOT" dma = "v0.2.1" docgenerator = "0.1.2-SNAPSHOT" @@ -22,11 +23,12 @@ kord-extensions-core = { module = "com.kotlindiscord.kord.extensions:kord-extens kord-extensions-phishing = { module = "com.kotlindiscord.kord.extensions:extra-phishing", version.ref = "kord-extensions" } kord-extensions-pluralkit = { module = "com.kotlindiscord.kord.extensions:extra-pluralkit", version.ref = "kord-extensions"} kord-extensions-unsafe = { module = "com.kotlindiscord.kord.extensions:unsafe", version.ref = "kord-extensions"} +kord-extensions-mongodb = { module = "com.kotlindiscord.kord.extensions:adapter-mongodb", version.ref = "kord-extensions" } kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib" } logback = { module = "ch.qos.logback:logback-classic", version.ref = "logback" } logging = { module = "io.github.oshai:kotlin-logging", version.ref = "logging" } github-api = { module = "org.kohsuke:github-api", version.ref = "github-api" } -kmongo = { module = "org.litote.kmongo:kmongo-coroutine-serialization", version.ref = "kmongo" } +mongo-driver = { module = "org.mongodb:mongodb-driver-kotlin-coroutine", version.ref = "mongo-driver" } detekt = { module = "io.gitlab.arturbosch.detekt:detekt-formatting", version.ref = "detekt"} cozy-welcome = {module = "org.quiltmc.community:module-welcome", version.ref = "cozy-welcome"} dma = { module = "org.hyacinthbots:discord-moderation-actions", version.ref = "dma"} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt b/src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt index c70d2413..321fd211 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt @@ -3,6 +3,7 @@ package org.hyacinthbots.lilybot import com.kotlindiscord.kord.extensions.ExtensibleBot +import com.kotlindiscord.kord.extensions.adapters.mongodb.mongoDB import com.kotlindiscord.kord.extensions.checks.hasPermission import com.kotlindiscord.kord.extensions.modules.extra.phishing.DetectionAction import com.kotlindiscord.kord.extensions.modules.extra.phishing.extPhishing @@ -15,7 +16,6 @@ import org.hyacinthbots.docgenerator.docsGenerator import org.hyacinthbots.docgenerator.enums.CommandTypes import org.hyacinthbots.docgenerator.enums.SupportedFileFormat import org.hyacinthbots.lilybot.database.collections.WelcomeChannelCollection -import org.hyacinthbots.lilybot.database.storage.MongoDBDataAdapter import org.hyacinthbots.lilybot.extensions.config.Config import org.hyacinthbots.lilybot.extensions.config.ConfigOptions import org.hyacinthbots.lilybot.extensions.config.GuildLogging @@ -60,7 +60,7 @@ val docFile = Path("./docs/commands.md") suspend fun main() { val bot = ExtensibleBot(BOT_TOKEN) { database(true) - dataAdapter(::MongoDBDataAdapter) + mongoDB() members { lockMemberRequests = true // Collect members one at a time to avoid hitting rate limits diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt index 7d5671ac..a9975248 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt @@ -1,10 +1,12 @@ package org.hyacinthbots.lilybot.database import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq import dev.kord.core.Kord import dev.kord.core.behavior.getChannelOfOrNull import dev.kord.core.entity.channel.thread.ThreadChannel import dev.kord.rest.request.KtorRequestException +import kotlinx.coroutines.flow.toList import kotlinx.datetime.Clock import mu.KotlinLogging import org.hyacinthbots.lilybot.database.Cleanups.cleanupGuildData @@ -24,7 +26,6 @@ import org.hyacinthbots.lilybot.database.collections.WelcomeChannelCollection import org.hyacinthbots.lilybot.database.entities.GuildLeaveTimeData import org.hyacinthbots.lilybot.database.entities.ThreadData import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This object contains the Database clean up functions, for removing old data from the database that Lily no longer @@ -38,10 +39,10 @@ object Cleanups : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val guildLeaveTimeCollection = db.mainDatabase.getCollection() + internal val guildLeaveTimeCollection = db.mainDatabase.getCollection(GuildLeaveTimeData.name) @PublishedApi - internal val threadDataCollection = db.mainDatabase.getCollection() + internal val threadDataCollection = db.mainDatabase.getCollection(ThreadData.name) @PublishedApi internal val cleanupsLogger = KotlinLogging.logger("Database Cleanups") @@ -75,7 +76,7 @@ object Cleanups : KordExKoinComponent { UtilityConfigCollection().clearConfig(it.guildId) WarnCollection().clearWarns(it.guildId) WelcomeChannelCollection().removeWelcomeChannelsForGuild(it.guildId, kord) - guildLeaveTimeCollection.deleteOne(GuildLeaveTimeData::guildId eq it.guildId) + guildLeaveTimeCollection.deleteOne(eq(GuildLeaveTimeData::guildId.name, it.guildId)) deletedGuildData += 1 // Increment the counter for logging } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/Collection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/Collection.kt new file mode 100644 index 00000000..41413e0e --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/Collection.kt @@ -0,0 +1,22 @@ +package org.hyacinthbots.lilybot.database + +/** + * This class stores the name of a collection, so it can be referenced easily, avoiding string duplication + * + * An example of how it should be used can be found below. The name property used in the get collection line comes from + * this class + * ```kt + * class MagicCollection : KordExKoinComponent { + * private val db: Database by inject() + * + * val collection = db.mainDatabase.getCollection(name) + * + * suspend fun get(): List = collection.find().toList() + * + * companion object : Collection("magicCollection") + * } + * ``` + * + * @property name The name of the collection + */ +abstract class Collection(val name: String) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/Database.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/Database.kt index 01e2d82c..dd3c19ea 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/Database.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/Database.kt @@ -1,22 +1,29 @@ package org.hyacinthbots.lilybot.database +import com.kotlindiscord.kord.extensions.adapters.mongodb.kordExCodecRegistry import com.mongodb.ConnectionString import com.mongodb.MongoClientSettings +import com.mongodb.kotlin.client.coroutine.MongoClient import org.bson.UuidRepresentation +import org.bson.codecs.configuration.CodecRegistries import org.hyacinthbots.lilybot.database.migrations.Migrator import org.hyacinthbots.lilybot.utils.MONGO_URI -import org.litote.kmongo.coroutine.coroutine -import org.litote.kmongo.reactivestreams.KMongo class Database { + private val codecRegistries = CodecRegistries.fromRegistries( + kordExCodecRegistry, + MongoClientSettings.getDefaultCodecRegistry() + ) + // Connect to the database using the provided connection URL private val settings = MongoClientSettings .builder() .uuidRepresentation(UuidRepresentation.STANDARD) .applyConnectionString(ConnectionString(MONGO_URI)) + .codecRegistry(codecRegistries) .build() - private val client = KMongo.createClient(settings).coroutine + private val client = MongoClient.create(settings) /** The main database for storing data. */ val mainDatabase get() = client.getDatabase("LilyBot") @@ -24,6 +31,8 @@ class Database { /** The database for storing per guild configuration data. */ val configDatabase get() = client.getDatabase("LilyBotConfig") + val tempKordExDatabase get() = client.getDatabase("kordex-data") + /** * Runs the migrations for both databases. * diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/_DatabaseUtils.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/_DatabaseUtils.kt new file mode 100644 index 00000000..188d7453 --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/_DatabaseUtils.kt @@ -0,0 +1,18 @@ +package org.hyacinthbots.lilybot.database + +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Filters.and +import com.mongodb.kotlin.client.coroutine.FindFlow +import com.mongodb.kotlin.client.coroutine.MongoCollection +import kotlinx.coroutines.flow.firstOrNull +import org.bson.conversions.Bson + +private fun MongoCollection.find(vararg filters: Bson?): FindFlow = find(and(*filters)) + +suspend fun MongoCollection.findOne(filter: Bson): T? = find(filter).firstOrNull() + +suspend fun MongoCollection.findOne(vararg filters: Bson?): T? = find(*filters).firstOrNull() + +suspend fun MongoCollection.deleteOne() = deleteOne(Filters.empty()) + +// TODO Make more cool useful functions diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/AutoThreadingCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/AutoThreadingCollection.kt index db1c6104..071204e1 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/AutoThreadingCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/AutoThreadingCollection.kt @@ -1,11 +1,13 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake +import kotlinx.coroutines.flow.toList import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.AutoThreadingData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [AutoThreading Database][AutoThreadingData]. This @@ -22,7 +24,7 @@ class AutoThreadingCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(AutoThreadingData.name) /** * Gets all auto threads for a given [inputGuildId]. @@ -33,7 +35,7 @@ class AutoThreadingCollection : KordExKoinComponent { * @since 4.6.0 */ suspend inline fun getAllAutoThreads(inputGuildId: Snowflake): List = - collection.find(AutoThreadingData::guildId eq inputGuildId).toList() + collection.find(eq(AutoThreadingData::guildId.name, inputGuildId)).toList() /** * Gets a single auto thread based off the channel ID. @@ -44,7 +46,7 @@ class AutoThreadingCollection : KordExKoinComponent { * @since 4.6.0 */ suspend inline fun getSingleAutoThread(inputChannelId: Snowflake): AutoThreadingData? = - collection.findOne(AutoThreadingData::channelId eq inputChannelId) + collection.findOne(eq(AutoThreadingData::channelId.name, inputChannelId)) /** * Sets a new auto thread. @@ -54,7 +56,7 @@ class AutoThreadingCollection : KordExKoinComponent { * @since 4.6.0 */ suspend inline fun setAutoThread(inputAutoThreadData: AutoThreadingData) { - collection.deleteOne(AutoThreadingData::channelId eq inputAutoThreadData.channelId) + collection.deleteOne(eq(AutoThreadingData::channelId.name, inputAutoThreadData.channelId)) collection.insertOne(inputAutoThreadData) } @@ -66,7 +68,7 @@ class AutoThreadingCollection : KordExKoinComponent { * @since 4.6.0 */ suspend inline fun deleteAutoThread(inputChannelId: Snowflake) = - collection.deleteOne(AutoThreadingData::channelId eq inputChannelId) + collection.deleteOne(eq(AutoThreadingData::channelId.name, inputChannelId)) /** * Deletes auto threads for a given guild. @@ -76,5 +78,5 @@ class AutoThreadingCollection : KordExKoinComponent { * @since 4.6.0 */ suspend inline fun deleteGuildAutoThreads(inputGuildId: Snowflake) = - collection.deleteMany(AutoThreadingData::guildId eq inputGuildId) + collection.deleteMany(eq(AutoThreadingData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ConfigCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ConfigCollection.kt index 61752bca..df428d1a 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ConfigCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ConfigCollection.kt @@ -1,13 +1,14 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.LoggingConfigData import org.hyacinthbots.lilybot.database.entities.ModerationConfigData import org.hyacinthbots.lilybot.database.entities.UtilityConfigData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [Logging Config Database][LoggingConfigData]. This class @@ -22,7 +23,7 @@ class LoggingConfigCollection : KordExKoinComponent { private val configDb: Database by inject() @PublishedApi - internal val collection = configDb.configDatabase.getCollection() + internal val collection = configDb.configDatabase.getCollection(LoggingConfigData.name) /** * Gets the logging config for the given guild using the [guildId][inputGuildId]. @@ -33,7 +34,7 @@ class LoggingConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun getConfig(inputGuildId: Snowflake): LoggingConfigData? = - collection.findOne(LoggingConfigData::guildId eq inputGuildId) + collection.findOne(eq(LoggingConfigData::guildId.name, inputGuildId)) /** * Adds the given [loggingConfig] to the database. @@ -43,7 +44,7 @@ class LoggingConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun setConfig(loggingConfig: LoggingConfigData) { - collection.deleteOne(LoggingConfigData::guildId eq loggingConfig.guildId) + collection.deleteOne(eq(LoggingConfigData::guildId.name, loggingConfig.guildId)) collection.insertOne(loggingConfig) } @@ -55,7 +56,7 @@ class LoggingConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun clearConfig(inputGuildId: Snowflake) = - collection.deleteOne(LoggingConfigData::guildId eq inputGuildId) + collection.deleteOne(eq(LoggingConfigData::guildId.name, inputGuildId)) } /** @@ -71,7 +72,7 @@ class ModerationConfigCollection : KordExKoinComponent { private val configDb: Database by inject() @PublishedApi - internal val collection = configDb.configDatabase.getCollection() + internal val collection = configDb.configDatabase.getCollection(ModerationConfigData.name) /** * Gets the Moderation config for the given guild using the [guildId][inputGuildId]. @@ -82,7 +83,7 @@ class ModerationConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun getConfig(inputGuildId: Snowflake): ModerationConfigData? = - collection.findOne(ModerationConfigData::guildId eq inputGuildId) + collection.findOne(eq(ModerationConfigData::guildId.name, inputGuildId)) /** * Adds the given [moderationConfig] to the database. @@ -92,7 +93,7 @@ class ModerationConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun setConfig(moderationConfig: ModerationConfigData) { - collection.deleteOne(ModerationConfigData::guildId eq moderationConfig.guildId) + collection.deleteOne(eq(ModerationConfigData::guildId.name, moderationConfig.guildId)) collection.insertOne(moderationConfig) } @@ -104,7 +105,7 @@ class ModerationConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun clearConfig(inputGuildId: Snowflake) = - collection.deleteOne(ModerationConfigData::guildId eq inputGuildId) + collection.deleteOne(eq(ModerationConfigData::guildId.name, inputGuildId)) } /** @@ -120,7 +121,7 @@ class UtilityConfigCollection : KordExKoinComponent { private val configDb: Database by inject() @PublishedApi - internal val collection = configDb.configDatabase.getCollection() + internal val collection = configDb.configDatabase.getCollection(UtilityConfigData.name) /** * Gets the Utility config for the given guild using the [guildId][inputGuildId]. @@ -131,7 +132,7 @@ class UtilityConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun getConfig(inputGuildId: Snowflake): UtilityConfigData? = - collection.findOne(UtilityConfigData::guildId eq inputGuildId) + collection.findOne(eq(UtilityConfigData::guildId.name, inputGuildId)) /** * Adds the given [utilityConfig] to the database. @@ -141,7 +142,7 @@ class UtilityConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun setConfig(utilityConfig: UtilityConfigData) { - collection.deleteOne(UtilityConfigData::guildId eq utilityConfig.guildId) + collection.deleteOne(eq(UtilityConfigData::guildId.name, utilityConfig.guildId)) collection.insertOne(utilityConfig) } @@ -153,5 +154,5 @@ class UtilityConfigCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun clearConfig(inputGuildId: Snowflake) = - collection.deleteOne(UtilityConfigData::guildId eq inputGuildId) + collection.deleteOne(eq(UtilityConfigData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GalleryChannelCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GalleryChannelCollection.kt index c9b75e79..d647eba5 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GalleryChannelCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GalleryChannelCollection.kt @@ -1,12 +1,13 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.and +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake +import kotlinx.coroutines.flow.toList import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.GalleryChannelData import org.koin.core.component.inject -import org.litote.kmongo.coroutine.CoroutineCollection -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [Gallery Channel Database][GalleryChannelData]. This @@ -22,17 +23,17 @@ class GalleryChannelCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(GalleryChannelData.name) /** * Collects every gallery channel in the database into a [List]. * - * @return The [CoroutineCollection] of [GalleryChannelData] for all the gallery channels in the database + * @return The [MongoCollection] of [GalleryChannelData] for all the gallery channels in the database * @author NoComment1105 * @since 3.3.0 */ suspend inline fun getChannels(inputGuildId: Snowflake): List = - collection.find(GalleryChannelData::guildId eq inputGuildId).toList() + collection.find(eq(GalleryChannelData::guildId.name, inputGuildId)).toList() /** * Stores a channel ID as input by the user, in the database, with it's corresponding guild, allowing us to find @@ -56,8 +57,10 @@ class GalleryChannelCollection : KordExKoinComponent { */ suspend inline fun removeChannel(inputGuildId: Snowflake, inputChannelId: Snowflake) = collection.deleteOne( - GalleryChannelData::channelId eq inputChannelId, - GalleryChannelData::guildId eq inputGuildId + and( + eq(GalleryChannelData::channelId.name, inputChannelId), + eq(GalleryChannelData::guildId.name, inputGuildId) + ) ) /** @@ -68,5 +71,5 @@ class GalleryChannelCollection : KordExKoinComponent { * @since 4.1.0 */ suspend inline fun removeAll(inputGuildId: Snowflake) = - collection.deleteMany(GalleryChannelData::guildId eq inputGuildId) + collection.deleteMany(eq(GalleryChannelData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GithubCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GithubCollection.kt index 98732e2f..2a95ea9b 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GithubCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GithubCollection.kt @@ -1,11 +1,12 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.GithubData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [GitHub database][GithubData]. This class contains @@ -20,7 +21,7 @@ class GithubCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(GithubData.name) /** * Gets the default repo for GitHub commands. @@ -31,7 +32,7 @@ class GithubCollection : KordExKoinComponent { * @since 4.3.0 */ suspend inline fun getDefaultRepo(inputGuildId: Snowflake): String? = - collection.findOne(GithubData::guildId eq inputGuildId)?.defaultRepo + collection.findOne(eq(GithubData::guildId.name, inputGuildId))?.defaultRepo /** * Sets the default repo for GitHub commands. @@ -42,7 +43,7 @@ class GithubCollection : KordExKoinComponent { * @since 4.3.0 */ suspend inline fun setDefaultRepo(inputGuildId: Snowflake, url: String) { - collection.deleteOne(GithubData::guildId eq inputGuildId) + collection.deleteOne(eq(GithubData::guildId.name, inputGuildId)) collection.insertOne(GithubData(inputGuildId, url)) } @@ -54,5 +55,5 @@ class GithubCollection : KordExKoinComponent { * @since 4.3.0 */ suspend inline fun removeDefaultRepo(inputGuildId: Snowflake) = - collection.deleteOne(GithubData::guildId eq inputGuildId) + collection.deleteOne(eq(GithubData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GuildLeaveTimeCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GuildLeaveTimeCollection.kt index 60b21c6b..6b1a35ec 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GuildLeaveTimeCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/GuildLeaveTimeCollection.kt @@ -1,12 +1,12 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake import kotlinx.datetime.Instant import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.GuildLeaveTimeData import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [Guild Leave Time Database][GuildLeaveTimeData]. This @@ -20,7 +20,7 @@ class GuildLeaveTimeCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(GuildLeaveTimeData.name) /** * Adds the time Lily bot left a guild with a config. @@ -43,5 +43,5 @@ class GuildLeaveTimeCollection : KordExKoinComponent { * @since 3.2.0 */ suspend inline fun removeLeaveTime(inputGuildId: Snowflake) = - collection.deleteOne(GuildLeaveTimeData::guildId eq inputGuildId) + collection.deleteOne(eq(GuildLeaveTimeData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/MetaCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/MetaCollection.kt index 01ae94aa..3eddab80 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/MetaCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/MetaCollection.kt @@ -1,11 +1,12 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.ConfigMetaData import org.hyacinthbots.lilybot.database.entities.MainMetaData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [main meta database][MainMetaData]. This class @@ -20,7 +21,7 @@ class MainMetaCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(MainMetaData.name) /** * Gets the main metadata from the database. @@ -30,7 +31,7 @@ class MainMetaCollection : KordExKoinComponent { * @since 4.0.0 */ suspend fun get(): MainMetaData? = - collection.findOne() + collection.findOne(eq(MainMetaData::id.name, "mainMeta")) /** * Sets the metadata when the table is first created. @@ -49,7 +50,7 @@ class MainMetaCollection : KordExKoinComponent { */ suspend fun update(meta: MainMetaData) = collection.findOneAndReplace( - MainMetaData::id eq "mainMeta", + eq(MainMetaData::id.name, "mainMeta"), meta ) } @@ -67,7 +68,7 @@ class ConfigMetaCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.configDatabase.getCollection() + internal val collection = db.configDatabase.getCollection(ConfigMetaData.name) /** * Gets the config metadata from the database. @@ -77,7 +78,7 @@ class ConfigMetaCollection : KordExKoinComponent { * @since 4.0.0 */ suspend fun get(): ConfigMetaData? = - collection.findOne() + collection.findOne(eq(ConfigMetaData::id.name, "configMeta")) /** * Sets the metadata when the table is first created. @@ -96,7 +97,7 @@ class ConfigMetaCollection : KordExKoinComponent { */ suspend fun update(meta: ConfigMetaData) = collection.findOneAndReplace( - ConfigMetaData::id eq "configMeta", + eq(ConfigMetaData::id.name, "configMeta"), meta ) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/NewsChannelPublishingCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/NewsChannelPublishingCollection.kt index 2851da56..1bf10b8c 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/NewsChannelPublishingCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/NewsChannelPublishingCollection.kt @@ -1,11 +1,14 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.and +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake +import kotlinx.coroutines.flow.toList import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.NewsChannelPublishingData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains functions for interacting with the [news channel publishing database][NewsChannelPublishingData]. @@ -22,7 +25,7 @@ class NewsChannelPublishingCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(NewsChannelPublishingData.name) /** * Adds a channel for auto-publishing. @@ -47,8 +50,10 @@ class NewsChannelPublishingCollection : KordExKoinComponent { */ suspend inline fun removeAutoPublishingChannel(inputGuildId: Snowflake, inputChannelId: Snowflake) = collection.deleteOne( - NewsChannelPublishingData::guildId eq inputGuildId, - NewsChannelPublishingData::channelId eq inputChannelId + and( + eq(NewsChannelPublishingData::guildId.name, inputGuildId), + eq(NewsChannelPublishingData::channelId.name, inputChannelId) + ) ) /** @@ -66,8 +71,8 @@ class NewsChannelPublishingCollection : KordExKoinComponent { inputChannelId: Snowflake ): NewsChannelPublishingData? = collection.findOne( - NewsChannelPublishingData::guildId eq inputGuildId, - NewsChannelPublishingData::channelId eq inputChannelId + eq(NewsChannelPublishingData::guildId.name, inputGuildId), + eq(NewsChannelPublishingData::channelId.name, inputChannelId) ) /** @@ -80,7 +85,7 @@ class NewsChannelPublishingCollection : KordExKoinComponent { * @since 4.7.0 */ suspend inline fun getAutoPublishingChannels(inputGuildId: Snowflake): List = - collection.find(NewsChannelPublishingData::guildId eq inputGuildId).toList() + collection.find(eq(NewsChannelPublishingData::guildId.name, inputGuildId)).toList() /** * Clears all the auto-publishing channels from a guild. @@ -91,6 +96,6 @@ class NewsChannelPublishingCollection : KordExKoinComponent { * @since 4.7.0 */ suspend inline fun clearAutoPublishingForGuild(inputGuildId: Snowflake) { - collection.deleteMany(NewsChannelPublishingData::guildId eq inputGuildId) + collection.deleteMany(eq(NewsChannelPublishingData::guildId.name, inputGuildId)) } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ReminderCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ReminderCollection.kt index 90e719d2..f4a066d9 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ReminderCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ReminderCollection.kt @@ -1,14 +1,16 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.and +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake +import kotlinx.coroutines.flow.toList import kotlinx.datetime.DateTimePeriod import kotlinx.datetime.TimeZone import kotlinx.datetime.plus import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.ReminderData import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with []the reminder database][ReminderData]. This @@ -28,7 +30,7 @@ class ReminderCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(ReminderData.name) /** * Gets all the reminders currently in the database. @@ -48,7 +50,7 @@ class ReminderCollection : KordExKoinComponent { * @since 4.2.0 */ suspend fun getRemindersForUser(userId: Snowflake): List = - collection.find(ReminderData::userId eq userId).toList() + collection.find(eq(ReminderData::userId.name, userId)).toList() /** * Gets all the reminders in the database for a specific user, in a specific guild. @@ -80,7 +82,7 @@ class ReminderCollection : KordExKoinComponent { * @since 4.2.0 */ suspend fun removeReminder(userId: Snowflake, number: Long) = - collection.deleteOne(ReminderData::userId eq userId, ReminderData::id eq number) + collection.deleteOne(and(eq(ReminderData::userId.name, userId), eq(ReminderData::id.name, number))) /** * Removes all the reminders for a given guild. @@ -89,7 +91,7 @@ class ReminderCollection : KordExKoinComponent { * @author NoComment1105 * @since 4.2.0 */ - suspend fun removeGuildReminders(guildId: Snowflake) = collection.deleteMany(ReminderData::guildId eq guildId) + suspend fun removeGuildReminders(guildId: Snowflake) = collection.deleteMany(eq(ReminderData::guildId.name, guildId)) /** * Updates a repeating reminder to be extended by the given [repeatingInterval]. diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleMenuCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleMenuCollection.kt index f8319ee5..1999ab21 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleMenuCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleMenuCollection.kt @@ -1,11 +1,12 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.RoleMenuData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [Role Menu Database][RoleMenuData]. This class contains @@ -21,7 +22,7 @@ class RoleMenuCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(RoleMenuData.name) /** * Using the provided [inputMessageId] the associated [RoleMenuData] will be returned from the database. @@ -32,7 +33,7 @@ class RoleMenuCollection : KordExKoinComponent { * @since 3.4.0 */ suspend inline fun getRoleData(inputMessageId: Snowflake): RoleMenuData? = - collection.findOne(RoleMenuData::messageId eq inputMessageId) + collection.findOne(eq(RoleMenuData::messageId.name, inputMessageId)) /** * Add the given [inputRoles] to the database entry for the role menu for the provided [inputMessageId], @@ -52,7 +53,7 @@ class RoleMenuCollection : KordExKoinComponent { inputRoles: MutableList ) { val newRoleMenu = RoleMenuData(inputMessageId, inputChannelId, inputGuildId, inputRoles) - collection.deleteOne(RoleMenuData::messageId eq inputMessageId) + collection.deleteOne(eq(RoleMenuData::messageId.name, inputMessageId)) collection.insertOne(newRoleMenu) } @@ -65,11 +66,11 @@ class RoleMenuCollection : KordExKoinComponent { * @since 3.4.0 */ suspend inline fun removeRoleFromMenu(inputMessageId: Snowflake, inputRoleId: Snowflake) { - val roleMenu = collection.findOne(RoleMenuData::messageId eq inputMessageId) ?: return + val roleMenu = collection.findOne(eq(RoleMenuData::messageId.name, inputMessageId)) ?: return roleMenu.roles.remove(inputRoleId) - collection.deleteOne(RoleMenuData::messageId eq inputMessageId) + collection.deleteOne(eq(RoleMenuData::messageId.name, inputMessageId)) collection.insertOne(roleMenu) } @@ -81,5 +82,5 @@ class RoleMenuCollection : KordExKoinComponent { * @since 4.0.0 */ suspend inline fun removeAllRoleMenus(inputGuildId: Snowflake) = - collection.deleteMany(RoleMenuData::guildId eq inputGuildId) + collection.deleteMany(eq(RoleMenuData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleSubscriptionCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleSubscriptionCollection.kt index 027e5272..17523a39 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleSubscriptionCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/RoleSubscriptionCollection.kt @@ -1,11 +1,13 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq +import com.mongodb.client.model.Updates import dev.kord.common.entity.Snowflake import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.RoleSubscriptionData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [Role Subscription database][RoleSubscriptionData]. This @@ -22,7 +24,7 @@ class RoleSubscriptionCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(RoleSubscriptionData.name) /** * Gets the roles that are subscribable for a given guild. @@ -34,7 +36,7 @@ class RoleSubscriptionCollection : KordExKoinComponent { * @since 4.9.0 */ suspend inline fun getSubscribableRoles(inputGuildId: Snowflake): RoleSubscriptionData? = - collection.findOne(RoleSubscriptionData::guildId eq inputGuildId) + collection.findOne(eq(RoleSubscriptionData::guildId.name, inputGuildId)) /** * Creates a subscribable role record in the database. This should only be used if a record does not already exist. @@ -58,12 +60,12 @@ class RoleSubscriptionCollection : KordExKoinComponent { * @since 4.9.0 */ suspend inline fun addSubscribableRole(inputGuildId: Snowflake, inputRoleId: Snowflake): Boolean? { - val col = collection.findOne(RoleSubscriptionData::guildId eq inputGuildId) ?: return null + val col = collection.findOne(eq(RoleSubscriptionData::guildId.name, inputGuildId)) ?: return null val newRoleList = col.subscribableRoles if (newRoleList.contains(inputRoleId)) return false else newRoleList.add(inputRoleId) collection.updateOne( - RoleSubscriptionData::guildId eq inputGuildId, - RoleSubscriptionData(inputGuildId, newRoleList) + eq(RoleSubscriptionData::guildId.name, inputGuildId), + Updates.set(RoleSubscriptionData::guildId.name, newRoleList) ) return true } @@ -79,7 +81,7 @@ class RoleSubscriptionCollection : KordExKoinComponent { * @since 4.9.0 */ suspend inline fun removeSubscribableRole(inputGuildId: Snowflake, inputRoleId: Snowflake): Boolean? { - val col = collection.findOne(RoleSubscriptionData::guildId eq inputGuildId) ?: return null + val col = collection.findOne(eq(RoleSubscriptionData::guildId.name, inputGuildId)) ?: return null val newRoleList = col.subscribableRoles if (!newRoleList.contains(inputRoleId)) { return false @@ -88,8 +90,8 @@ class RoleSubscriptionCollection : KordExKoinComponent { if (!removal) return false } collection.updateOne( - RoleSubscriptionData::guildId eq inputGuildId, - RoleSubscriptionData(inputGuildId, newRoleList) + eq(RoleSubscriptionData::guildId.name, inputGuildId), + Updates.set(RoleSubscriptionData::guildId.name, newRoleList) ) return true } @@ -103,5 +105,5 @@ class RoleSubscriptionCollection : KordExKoinComponent { * @since 4.9.0 */ suspend inline fun removeAllSubscribableRoles(inputGuildId: Snowflake) = - collection.deleteOne(RoleSubscriptionData::guildId eq inputGuildId) + collection.deleteOne(eq(RoleSubscriptionData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/StatusCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/StatusCollection.kt index aaccbba8..4a961feb 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/StatusCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/StatusCollection.kt @@ -2,7 +2,9 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import org.hyacinthbots.lilybot.database.Database +import org.hyacinthbots.lilybot.database.deleteOne import org.hyacinthbots.lilybot.database.entities.StatusData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject /** @@ -17,7 +19,7 @@ class StatusCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(StatusData.name) /** * Gets Lily's status from the database. diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TagsCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TagsCollection.kt index 89584046..21476ee5 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TagsCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/TagsCollection.kt @@ -1,11 +1,14 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.and +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake +import kotlinx.coroutines.flow.toList import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.TagsData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class contains the functions for interacting with the [Tags Database][TagsData]. This class has functions for @@ -22,7 +25,7 @@ class TagsCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(TagsData.name) /** * Gets the given tag using it's [name] and returns its [TagsData]. If the tag does not exist. @@ -35,7 +38,7 @@ class TagsCollection : KordExKoinComponent { * @since 3.1.0 */ suspend inline fun getTag(inputGuildId: Snowflake, name: String): TagsData? = - collection.findOne(TagsData::guildId eq inputGuildId, TagsData::name eq name) + collection.findOne(and(eq(TagsData::guildId.name, inputGuildId), eq(TagsData::name.name, name))) /** * Gets all tags in the given [inputGuildId]. @@ -46,7 +49,7 @@ class TagsCollection : KordExKoinComponent { * @since 3.1.0 */ suspend inline fun getAllTags(inputGuildId: Snowflake): List = - collection.find(TagsData::guildId eq inputGuildId).toList() + collection.find(eq(TagsData::guildId.name, inputGuildId)).toList() /** * Adds a tag to the database, using the provided parameters. @@ -59,12 +62,12 @@ class TagsCollection : KordExKoinComponent { * @since 3.1.0 */ suspend inline fun setTag( - inputGuildId: Snowflake, - name: String, - tagTitle: String, - tagValue: String, - tagAppearance: String - ) = + inputGuildId: Snowflake, + name: String, + tagTitle: String, + tagValue: String, + tagAppearance: String + ) = collection.insertOne(TagsData(inputGuildId, name, tagTitle, tagValue, tagAppearance)) /** @@ -76,7 +79,7 @@ class TagsCollection : KordExKoinComponent { * @since 3.1.0 */ suspend inline fun removeTag(inputGuildId: Snowflake, name: String) = - collection.deleteOne(TagsData::guildId eq inputGuildId, TagsData::name eq name) + collection.deleteOne(and(eq(TagsData::guildId.name, inputGuildId), eq(TagsData::name.name, name))) /** * Clears all tags for the provided [inputGuildId]. @@ -86,5 +89,5 @@ class TagsCollection : KordExKoinComponent { * @since 3.1.0 */ suspend inline fun clearTags(inputGuildId: Snowflake) = - collection.deleteMany(TagsData::guildId eq inputGuildId) + collection.deleteMany(eq(TagsData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ThreadsCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ThreadsCollection.kt index c65ad4ee..0b86be07 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ThreadsCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/ThreadsCollection.kt @@ -1,11 +1,13 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake +import kotlinx.coroutines.flow.toList import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.ThreadData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class stores all the functions for interacting with the [Threads Database][ThreadData]. This class contains @@ -23,7 +25,7 @@ class ThreadsCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(ThreadData.name) /** * Using the provided [inputThreadId] the thread is returned. @@ -35,7 +37,7 @@ class ThreadsCollection : KordExKoinComponent { * @since 3.2.0 */ suspend inline fun getThread(inputThreadId: Snowflake): ThreadData? = - collection.findOne(ThreadData::threadId eq inputThreadId) + collection.findOne(eq(ThreadData::threadId.name, inputThreadId)) /** * Gets all threads into a list and return them to the user. @@ -56,7 +58,7 @@ class ThreadsCollection : KordExKoinComponent { * @since 3.2.0 */ suspend inline fun getOwnerThreads(inputOwnerId: Snowflake): List = - collection.find(ThreadData::ownerId eq inputOwnerId).toList() + collection.find(eq(ThreadData::ownerId.name, inputOwnerId)).toList() /** * Add or update the ownership of the given [inputThreadId] to the given [newOwnerId]. @@ -78,7 +80,7 @@ class ThreadsCollection : KordExKoinComponent { parentChannelId: Snowflake?, preventArchiving: Boolean = false ) { - collection.deleteOne(ThreadData::threadId eq inputThreadId) + collection.deleteOne(eq(ThreadData::threadId.name, inputThreadId)) collection.insertOne(ThreadData(inputGuildId, inputThreadId, newOwnerId, parentChannelId, preventArchiving)) } @@ -91,7 +93,7 @@ class ThreadsCollection : KordExKoinComponent { * @since 3.2.2 */ suspend inline fun removeThread(inputThreadId: Snowflake) = - collection.deleteOne(ThreadData::threadId eq inputThreadId) + collection.deleteOne(eq(ThreadData::threadId.name, inputThreadId)) /** * This function deletes the ownership data stored in database for the given [inputGuildId]. @@ -102,5 +104,5 @@ class ThreadsCollection : KordExKoinComponent { * @since 4.1.0 */ suspend inline fun removeGuildThreads(inputGuildId: Snowflake) = - collection.deleteMany(ThreadData::guildId eq inputGuildId) + collection.deleteMany(eq(ThreadData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/UptimeCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/UptimeCollection.kt index f7f4057e..18d1a1e0 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/UptimeCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/UptimeCollection.kt @@ -4,6 +4,7 @@ import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import kotlinx.datetime.Instant import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.UptimeData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject /** @@ -18,7 +19,7 @@ class UptimeCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(UptimeData.name) /** * Gets the uptime data from the database. diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WarnCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WarnCollection.kt index cb64da4d..2f8da8bc 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WarnCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WarnCollection.kt @@ -1,11 +1,13 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters.and +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.WarnData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq /** * This class stores all the functions for interacting with the [Warn Database][WarnData]. The class contains the @@ -20,7 +22,7 @@ class WarnCollection : KordExKoinComponent { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(WarnData.name) /** * Gets the number of points the provided [inputUserId] has in the provided [inputGuildId] from the database. @@ -33,8 +35,10 @@ class WarnCollection : KordExKoinComponent { */ suspend inline fun getWarn(inputUserId: Snowflake, inputGuildId: Snowflake): WarnData? = collection.findOne( - WarnData::userId eq inputUserId, - WarnData::guildId eq inputGuildId + and( + eq(WarnData::userId.name, inputUserId), + eq(WarnData::guildId.name, inputGuildId) + ) ) /** @@ -48,7 +52,7 @@ class WarnCollection : KordExKoinComponent { */ suspend inline fun setWarn(inputUserId: Snowflake, inputGuildId: Snowflake, remove: Boolean) { val currentStrikes = getWarn(inputUserId, inputGuildId)?.strikes ?: 0 - collection.deleteOne(WarnData::userId eq inputUserId, WarnData::guildId eq inputGuildId) + collection.deleteOne(and(eq(WarnData::userId.name, inputUserId), eq(WarnData::guildId.name, inputGuildId))) collection.insertOne( WarnData( inputUserId, @@ -66,5 +70,5 @@ class WarnCollection : KordExKoinComponent { * @since 3.0.0 */ suspend inline fun clearWarns(inputGuildId: Snowflake) = - collection.deleteMany(WarnData::guildId eq inputGuildId) + collection.deleteMany(eq(WarnData::guildId.name, inputGuildId)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WelcomeChannelCollection.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WelcomeChannelCollection.kt index adc9e613..463bff04 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WelcomeChannelCollection.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/collections/WelcomeChannelCollection.kt @@ -8,12 +8,15 @@ package org.hyacinthbots.lilybot.database.collections import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Filters.eq import dev.kord.common.entity.Snowflake import dev.kord.core.Kord +import kotlinx.coroutines.flow.toList import org.hyacinthbots.lilybot.database.Database import org.hyacinthbots.lilybot.database.entities.WelcomeChannelData +import org.hyacinthbots.lilybot.database.findOne import org.koin.core.component.inject -import org.litote.kmongo.eq import org.quiltmc.community.cozy.modules.welcome.data.WelcomeChannelData as CozyWelcomeChannelData /** @@ -30,25 +33,25 @@ class WelcomeChannelCollection : KordExKoinComponent, CozyWelcomeChannelData { private val db: Database by inject() @PublishedApi - internal val collection = db.mainDatabase.getCollection() + internal val collection = db.mainDatabase.getCollection(WelcomeChannelData.name) override suspend fun getChannelURLs(): Map = collection.find() .toList().associate { it.channelId to it.url } override suspend fun getUrlForChannel(channelId: Snowflake): String? = - collection.findOne(WelcomeChannelData::channelId eq channelId) + collection.findOne(eq(WelcomeChannelData::channelId.name, channelId)) ?.url override suspend fun setUrlForChannel(channelId: Snowflake, url: String) { - collection.save(WelcomeChannelData(channelId, url)) + collection.replaceOne(Filters.empty(), WelcomeChannelData(channelId, url)) } override suspend fun removeChannel(channelId: Snowflake): String? { val url = getUrlForChannel(channelId) ?: return null - collection.deleteOne(WelcomeChannelData::channelId eq channelId) + collection.deleteOne(eq(WelcomeChannelData::channelId.name, channelId)) return url } @@ -56,7 +59,7 @@ class WelcomeChannelCollection : KordExKoinComponent, CozyWelcomeChannelData { suspend fun removeWelcomeChannelsForGuild(guildId: Snowflake, kord: Kord) { val guild = kord.getGuildOrNull(guildId) ?: return guild.channels.collect { - collection.deleteOne(WelcomeChannelData::channelId eq it.id) + collection.deleteOne(eq(WelcomeChannelData::channelId.name, it.id)) } } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AdaptedData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AdaptedData.kt index c41a4716..4eda9635 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AdaptedData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AdaptedData.kt @@ -10,8 +10,9 @@ import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable @Serializable -@Suppress("DataClassShouldBeImmutable") +@Suppress("DataClassShouldBeImmutable", "PropertyName", "ConstructorParameterNaming") data class AdaptedData( + val _id: String, val identifier: String, val type: StorageType? = null, diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AutoThreadingData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AutoThreadingData.kt index c736f76a..546b7743 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AutoThreadingData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/AutoThreadingData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for auto-threaded channels. @@ -29,4 +30,6 @@ data class AutoThreadingData( val mention: Boolean, val creationMessage: String?, val addModsAndRole: Boolean -) +) { + companion object : Collection("autoThreadingData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/Config.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/Config.kt index 84a256af..66ce1941 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/Config.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/Config.kt @@ -3,6 +3,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.datetime.DateTimePeriod import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for moderation configuration. The logging config stores where logs are sent to, and whether to enable or @@ -30,7 +31,9 @@ data class LoggingConfigData( val enablePublicMemberLogs: Boolean, val publicMemberLog: Snowflake?, val publicMemberLogData: PublicMemberLogData? -) +) { + companion object : Collection("loggingConfigData") +} /** * The data for moderation configuration. The moderation config is what stores the data for moderation actions. The @@ -56,7 +59,9 @@ data class ModerationConfigData( val autoPunishOnWarn: Boolean?, val publicLogging: Boolean?, val banDmMessage: String?, -) +) { + companion object : Collection("moderationConfigData") +} /** * The data for miscellaneous configuration. The miscellaneous config stores the data for enabling or disabling log @@ -70,4 +75,6 @@ data class ModerationConfigData( data class UtilityConfigData( val guildId: Snowflake, val utilityLogChannel: Snowflake? -) +) { + companion object : Collection("utilityConfigData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GalleryChannelData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GalleryChannelData.kt index 5c7944c8..1c7433da 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GalleryChannelData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GalleryChannelData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for image channels in a guild. @@ -14,4 +15,6 @@ import kotlinx.serialization.Serializable data class GalleryChannelData( val guildId: Snowflake, val channelId: Snowflake -) +) { + companion object : Collection("galleryChannelData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GithubData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GithubData.kt index c1c3d011..4438ee49 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GithubData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GithubData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for GitHub commands. @@ -15,4 +16,6 @@ import kotlinx.serialization.Serializable data class GithubData( val guildId: Snowflake, val defaultRepo: String -) +) { + companion object : Collection("githubData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GuildLeaveTimeData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GuildLeaveTimeData.kt index 53cf35c2..cdf1a197 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GuildLeaveTimeData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/GuildLeaveTimeData.kt @@ -3,6 +3,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.datetime.Instant import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for when Lily leaves a guild. @@ -15,4 +16,6 @@ import kotlinx.serialization.Serializable data class GuildLeaveTimeData( val guildId: Snowflake, val guildLeaveTime: Instant -) +) { + companion object : Collection("guildLeaveTimeData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/MetaData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/MetaData.kt index c7769389..53db865e 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/MetaData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/MetaData.kt @@ -1,6 +1,7 @@ package org.hyacinthbots.lilybot.database.entities import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for the metadata of the main database. @@ -14,7 +15,9 @@ import kotlinx.serialization.Serializable data class MainMetaData( val version: Int, val id: String = "mainMeta" -) +) { + companion object : Collection("mainMetaData") +} /** * The data for the metadata of the config database. @@ -28,4 +31,6 @@ data class MainMetaData( data class ConfigMetaData( val version: Int, val id: String = "configMeta" -) +) { + companion object : Collection("configMetaData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/NewsChannelPublishingData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/NewsChannelPublishingData.kt index 0bf01b37..258245ef 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/NewsChannelPublishingData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/NewsChannelPublishingData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for the news channel publishing database. @@ -15,4 +16,6 @@ import kotlinx.serialization.Serializable data class NewsChannelPublishingData( val guildId: Snowflake, val channelId: Snowflake -) +) { + companion object : Collection("newsChannelPublishingData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/PublicMemberLogData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/PublicMemberLogData.kt index 573d6b61..f3d6f1a1 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/PublicMemberLogData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/PublicMemberLogData.kt @@ -1,6 +1,7 @@ package org.hyacinthbots.lilybot.database.entities import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for public member logging. @@ -14,4 +15,6 @@ data class PublicMemberLogData( val pingNewUsers: Boolean, val joinMessage: String?, val leaveMessage: String? -) +) { + companion object : Collection("publicMemberLogData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ReminderData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ReminderData.kt index b7ca91c2..3bd03f56 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ReminderData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ReminderData.kt @@ -4,6 +4,7 @@ import dev.kord.common.entity.Snowflake import kotlinx.datetime.DateTimePeriod import kotlinx.datetime.Instant import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * THe data for reminders in a guild. @@ -35,4 +36,6 @@ data class ReminderData( val repeating: Boolean, val repeatingInterval: DateTimePeriod?, val id: Long -) +) { + companion object : Collection("reminderData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/RoleMenuData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/RoleMenuData.kt index 0673bd2e..de5a944b 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/RoleMenuData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/RoleMenuData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for role menus. @@ -18,4 +19,6 @@ data class RoleMenuData( val channelId: Snowflake, val guildId: Snowflake, val roles: MutableList -) +) { + companion object : Collection("roleMenuData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/RoleSubscriptionData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/RoleSubscriptionData.kt index 59290937..ed1e47b1 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/RoleSubscriptionData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/RoleSubscriptionData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for role subscriptions. @@ -14,4 +15,6 @@ import kotlinx.serialization.Serializable data class RoleSubscriptionData( val guildId: Snowflake, val subscribableRoles: MutableList -) +) { + companion object : Collection("roleSubscriptionData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/StatusData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/StatusData.kt index 090244d0..a199aa0d 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/StatusData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/StatusData.kt @@ -1,6 +1,7 @@ package org.hyacinthbots.lilybot.database.entities import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for the bot status. @@ -11,4 +12,6 @@ import kotlinx.serialization.Serializable @Serializable data class StatusData( val status: String? -) +) { + companion object : Collection("statusData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/TagsData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/TagsData.kt index 78b00ffb..4ee92e9c 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/TagsData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/TagsData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data of guild tags, which are stored in the database. @@ -20,4 +21,6 @@ data class TagsData( val tagTitle: String, val tagValue: String, val tagAppearance: String -) +) { + companion object : Collection("tagsData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ThreadData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ThreadData.kt index 1e60fc9e..2bd5fd1c 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ThreadData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/ThreadData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for threads. @@ -21,4 +22,6 @@ data class ThreadData( val ownerId: Snowflake, val parentChannelId: Snowflake?, var preventArchiving: Boolean = false -) +) { + companion object : Collection("threadData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/UptimeData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/UptimeData.kt index 45503f83..93c6d945 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/UptimeData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/UptimeData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import kotlinx.datetime.Instant import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data to help calculate bot uptime. @@ -12,4 +13,6 @@ import kotlinx.serialization.Serializable @Serializable data class UptimeData( val onTime: Instant -) +) { + companion object : Collection("uptimeData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/WarnData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/WarnData.kt index db5a79be..bac600c9 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/WarnData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/WarnData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for warnings in guilds. @@ -16,4 +17,6 @@ data class WarnData( val userId: Snowflake, val guildId: Snowflake, val strikes: Int -) +) { + companion object : Collection("warnData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/WelcomeChannelData.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/WelcomeChannelData.kt index 796c75ec..383c5d2c 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/WelcomeChannelData.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/entities/WelcomeChannelData.kt @@ -2,6 +2,7 @@ package org.hyacinthbots.lilybot.database.entities import dev.kord.common.entity.Snowflake import kotlinx.serialization.Serializable +import org.hyacinthbots.lilybot.database.Collection /** * The data for Welcome channels. @@ -14,4 +15,6 @@ import kotlinx.serialization.Serializable data class WelcomeChannelData( val channelId: Snowflake, val url: String -) +) { + companion object : Collection("welcomeChannelData") +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/Migrator.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/Migrator.kt index 3c8af692..8497b07d 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/Migrator.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/Migrator.kt @@ -24,6 +24,7 @@ import org.hyacinthbots.lilybot.database.migrations.config.configV3 import org.hyacinthbots.lilybot.database.migrations.config.configV4 import org.hyacinthbots.lilybot.database.migrations.config.configV5 import org.hyacinthbots.lilybot.database.migrations.config.configV6 +import org.hyacinthbots.lilybot.database.migrations.config.configV7 import org.hyacinthbots.lilybot.database.migrations.main.mainV1 import org.hyacinthbots.lilybot.database.migrations.main.mainV2 import org.hyacinthbots.lilybot.database.migrations.main.mainV3 @@ -121,6 +122,7 @@ object Migrator : KordExKoinComponent { 4 -> ::configV4 5 -> ::configV5 6 -> ::configV6 + 7 -> ::configV7 else -> break }(db.configDatabase) diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV1.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV1.kt index d63e0460..87f42e59 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV1.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV1.kt @@ -1,20 +1,20 @@ package org.hyacinthbots.lilybot.database.migrations.config +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Updates +import com.mongodb.kotlin.client.coroutine.MongoDatabase import org.hyacinthbots.lilybot.database.entities.LoggingConfigData -import org.litote.kmongo.coroutine.CoroutineDatabase -import org.litote.kmongo.exists -import org.litote.kmongo.setValue -suspend fun configV1(configDb: CoroutineDatabase) { - with(configDb.getCollection("loggingConfigData")) { +suspend fun configV1(configDb: MongoDatabase) { + with(configDb.getCollection(LoggingConfigData.name)) { updateMany( - LoggingConfigData::enableMessageEditLogs exists false, - setValue(LoggingConfigData::enableMessageEditLogs, false) + Filters.exists(LoggingConfigData::enableMessageEditLogs.name, false), + Updates.set(LoggingConfigData::enableMessageEditLogs.name, false) ) } - configDb.getCollection("loggingConfigData").updateMany( - "{}", - "{\$rename: {enableMessageLogs: \"enableMessageDeleteLogs\"}}" + configDb.getCollection(LoggingConfigData.name).updateMany( + Filters.empty(), + Updates.rename("enableMessageLogs", "enableMessageDeleteLogs") ) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV2.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV2.kt index fa6afca6..7fb69f2a 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV2.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV2.kt @@ -1,20 +1,20 @@ package org.hyacinthbots.lilybot.database.migrations.config +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Updates +import com.mongodb.kotlin.client.coroutine.MongoDatabase import org.hyacinthbots.lilybot.database.entities.ModerationConfigData -import org.litote.kmongo.coroutine.CoroutineDatabase -import org.litote.kmongo.exists -import org.litote.kmongo.setValue -suspend fun configV2(db: CoroutineDatabase) { - with(db.getCollection("moderationConfigData")) { +suspend fun configV2(db: MongoDatabase) { + with(db.getCollection(ModerationConfigData.name)) { updateMany( - ModerationConfigData::quickTimeoutLength exists false, - setValue(ModerationConfigData::quickTimeoutLength, null) + Filters.exists(ModerationConfigData::quickTimeoutLength.name, false), + Updates.set(ModerationConfigData::quickTimeoutLength.name, null) ) updateMany( - ModerationConfigData::autoPunishOnWarn exists false, - setValue(ModerationConfigData::autoPunishOnWarn, null) + Filters.exists(ModerationConfigData::autoPunishOnWarn.name, false), + Updates.set(ModerationConfigData::autoPunishOnWarn.name, null) ) } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV3.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV3.kt index 857d8022..5881fa76 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV3.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV3.kt @@ -1,23 +1,23 @@ package org.hyacinthbots.lilybot.database.migrations.config +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Updates +import com.mongodb.kotlin.client.coroutine.MongoDatabase import org.hyacinthbots.lilybot.database.entities.LoggingConfigData -import org.litote.kmongo.coroutine.CoroutineDatabase -import org.litote.kmongo.exists -import org.litote.kmongo.setValue -suspend fun configV3(db: CoroutineDatabase) { - with(db.getCollection("loggingConfigData")) { +suspend fun configV3(db: MongoDatabase) { + with(db.getCollection(LoggingConfigData.name)) { updateMany( - LoggingConfigData::enablePublicMemberLogs exists false, - setValue(LoggingConfigData::enablePublicMemberLogs, false) + Filters.exists(LoggingConfigData::enablePublicMemberLogs.name, false), + Updates.set(LoggingConfigData::enablePublicMemberLogs.name, false) ) updateMany( - LoggingConfigData::publicMemberLog exists false, - setValue(LoggingConfigData::publicMemberLog, null) + Filters.exists(LoggingConfigData::publicMemberLog.name, false), + Updates.set(LoggingConfigData::publicMemberLog.name, null) ) updateMany( - LoggingConfigData::publicMemberLogData exists false, - setValue(LoggingConfigData::publicMemberLogData, null) + Filters.exists(LoggingConfigData::publicMemberLogData.name, false), + Updates.set(LoggingConfigData::publicMemberLogData.name, null) ) } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV4.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV4.kt index 3c214b91..99ef32ce 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV4.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV4.kt @@ -1,9 +1,9 @@ package org.hyacinthbots.lilybot.database.migrations.config -import org.litote.kmongo.coroutine.CoroutineDatabase +import com.mongodb.kotlin.client.coroutine.MongoDatabase @Suppress("UnusedPrivateMember", "UNUSED_PARAMETER", "RedundantSuspendModifier") -suspend fun configV4(db: CoroutineDatabase) { +suspend fun configV4(db: MongoDatabase) { // Support config has been removed. // if (db.getCollection().find().toList().isEmpty()) { // db.dropCollection("supportConfigData") diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV5.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV5.kt index 1a264ed1..edd5e203 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV5.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV5.kt @@ -2,9 +2,9 @@ package org.hyacinthbots.lilybot.database.migrations.config -import org.litote.kmongo.coroutine.CoroutineDatabase +import com.mongodb.kotlin.client.coroutine.MongoDatabase -suspend fun configV5(db: CoroutineDatabase) { +suspend fun configV5(db: MongoDatabase) { // val collection = db.getCollection("utilityConfigData") // val oldConfigs = collection.find().toList() // val newConfigs = mutableListOf() diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV6.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV6.kt index da9c7cd0..073accdb 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV6.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV6.kt @@ -1,15 +1,15 @@ package org.hyacinthbots.lilybot.database.migrations.config +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Updates +import com.mongodb.kotlin.client.coroutine.MongoDatabase import org.hyacinthbots.lilybot.database.entities.ModerationConfigData -import org.litote.kmongo.coroutine.CoroutineDatabase -import org.litote.kmongo.exists -import org.litote.kmongo.setValue -suspend fun configV6(db: CoroutineDatabase) { - with(db.getCollection("moderationConfigData")) { +suspend fun configV6(db: MongoDatabase) { + with(db.getCollection(ModerationConfigData.name)) { updateMany( - ModerationConfigData::banDmMessage exists false, - setValue(ModerationConfigData::banDmMessage, null) + Filters.exists(ModerationConfigData::banDmMessage.name, false), + Updates.set(ModerationConfigData::banDmMessage.name, null) ) } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV7.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV7.kt new file mode 100644 index 00000000..ea4c3eeb --- /dev/null +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/config/configV7.kt @@ -0,0 +1,9 @@ +package org.hyacinthbots.lilybot.database.migrations.config + +import com.mongodb.kotlin.client.coroutine.MongoDatabase +import org.hyacinthbots.lilybot.database.entities.AdaptedData + +suspend fun configV7(db: MongoDatabase) { + db.getCollection("ext-pluralkit").drop() + db.getCollection("data-ext-pluralkit").drop() +} diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV1.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV1.kt index cbeb8940..bf774eb7 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV1.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV1.kt @@ -1,10 +1,10 @@ package org.hyacinthbots.lilybot.database.migrations.main +import com.mongodb.kotlin.client.coroutine.MongoDatabase import org.hyacinthbots.lilybot.database.entities.StatusData -import org.litote.kmongo.coroutine.CoroutineDatabase // This was commented out due to the remindme data class being removed -suspend fun mainV1(db: CoroutineDatabase) { +suspend fun mainV1(db: MongoDatabase) { // val reminders = db.getCollection("remindMeData") // // val repeating = mutableListOf>() @@ -63,7 +63,7 @@ suspend fun mainV1(db: CoroutineDatabase) { // reminders.bulkWrite(requests = nonRepeating, BulkWriteOptions().ordered(true)) // } - db.dropCollection("statusData") - db.createCollection("statusData") - db.getCollection("statusData").insertOne(StatusData(null)) + db.getCollection(StatusData.name).drop() + db.createCollection(StatusData.name) + db.getCollection(StatusData.name).insertOne(StatusData(null)) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV2.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV2.kt index cb8682fc..bfbf954d 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV2.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV2.kt @@ -1,12 +1,12 @@ package org.hyacinthbots.lilybot.database.migrations.main +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Updates +import com.mongodb.kotlin.client.coroutine.MongoDatabase import org.hyacinthbots.lilybot.database.entities.ThreadData -import org.litote.kmongo.coroutine.CoroutineDatabase -import org.litote.kmongo.exists -import org.litote.kmongo.setValue -suspend fun mainV2(db: CoroutineDatabase) { - with(db.getCollection()) { - updateMany(ThreadData::guildId exists false, setValue(ThreadData::guildId, null)) +suspend fun mainV2(db: MongoDatabase) { + with(db.getCollection(ThreadData.name)) { + updateMany(Filters.exists(ThreadData::guildId.name, false), Updates.set(ThreadData::guildId.name, null)) } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV3.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV3.kt index 3b775ed7..28b4815b 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV3.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV3.kt @@ -1,8 +1,9 @@ package org.hyacinthbots.lilybot.database.migrations.main -import org.litote.kmongo.coroutine.CoroutineDatabase +import com.mongodb.kotlin.client.coroutine.MongoDatabase +import org.hyacinthbots.lilybot.database.entities.ReminderData -suspend fun mainV3(db: CoroutineDatabase) { - db.dropCollection("remindMeData") - db.createCollection("reminderData") +suspend fun mainV3(db: MongoDatabase) { + // db.getCollection("remindMeData") + db.createCollection(ReminderData.name) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV4.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV4.kt index 4c5dc577..16b1c1a4 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV4.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV4.kt @@ -1,8 +1,10 @@ package org.hyacinthbots.lilybot.database.migrations.main -import org.litote.kmongo.coroutine.CoroutineDatabase +import com.mongodb.kotlin.client.coroutine.MongoDatabase +import org.hyacinthbots.lilybot.database.entities.GithubData +import org.hyacinthbots.lilybot.database.entities.WelcomeChannelData -suspend fun mainV4(db: CoroutineDatabase) { - db.createCollection("welcomeChannelData") - db.createCollection("githubData") +suspend fun mainV4(db: MongoDatabase) { + db.createCollection(WelcomeChannelData.name) + db.createCollection(GithubData.name) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV5.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV5.kt index 9d1f5d87..e27f2bf0 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV5.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV5.kt @@ -1,15 +1,18 @@ package org.hyacinthbots.lilybot.database.migrations.main +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Updates +import com.mongodb.kotlin.client.coroutine.MongoDatabase import org.hyacinthbots.lilybot.database.entities.ThreadData -import org.litote.kmongo.coroutine.CoroutineDatabase -import org.litote.kmongo.exists -import org.litote.kmongo.setValue -suspend fun mainV5(db: CoroutineDatabase) { +suspend fun mainV5(db: MongoDatabase) { // db.createCollection("autoThreadingData") - with(db.getCollection()) { - updateMany(ThreadData::parentChannelId exists false, setValue(ThreadData::parentChannelId, null)) + with(db.getCollection(ThreadData.name)) { + updateMany( + Filters.exists(ThreadData::parentChannelId.name, false), + Updates.set(ThreadData::parentChannelId.name, null) + ) } // with(configDb.getCollection()) { diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV6.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV6.kt index ac8b0e21..08b85186 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV6.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV6.kt @@ -1,12 +1,15 @@ package org.hyacinthbots.lilybot.database.migrations.main +import com.mongodb.client.model.Filters +import com.mongodb.client.model.Updates +import com.mongodb.kotlin.client.coroutine.MongoDatabase import org.hyacinthbots.lilybot.database.entities.AutoThreadingData -import org.litote.kmongo.coroutine.CoroutineDatabase -import org.litote.kmongo.exists -import org.litote.kmongo.setValue -suspend fun mainV6(db: CoroutineDatabase) { - with(db.getCollection()) { - updateMany(AutoThreadingData::addModsAndRole exists false, setValue(AutoThreadingData::addModsAndRole, false)) +suspend fun mainV6(db: MongoDatabase) { + with(db.getCollection(AutoThreadingData.name)) { + updateMany( + Filters.exists(AutoThreadingData::addModsAndRole.name, false), + Updates.set(AutoThreadingData::addModsAndRole.name, false) + ) } } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV7.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV7.kt index 6c14d1de..9bc111fe 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV7.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV7.kt @@ -1,7 +1,8 @@ package org.hyacinthbots.lilybot.database.migrations.main -import org.litote.kmongo.coroutine.CoroutineDatabase +import com.mongodb.kotlin.client.coroutine.MongoDatabase +import org.hyacinthbots.lilybot.database.entities.NewsChannelPublishingData -suspend fun mainV7(db: CoroutineDatabase) { - db.createCollection("newsChannelPublishingData") +suspend fun mainV7(db: MongoDatabase) { + db.createCollection(NewsChannelPublishingData.name) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV8.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV8.kt index 0995230d..311db03d 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV8.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV8.kt @@ -1,7 +1,8 @@ package org.hyacinthbots.lilybot.database.migrations.main -import org.litote.kmongo.coroutine.CoroutineDatabase +import com.mongodb.kotlin.client.coroutine.MongoDatabase +import org.hyacinthbots.lilybot.database.entities.RoleSubscriptionData -suspend fun mainV8(db: CoroutineDatabase) { - db.createCollection("roleSubscriptionData") +suspend fun mainV8(db: MongoDatabase) { + db.createCollection(RoleSubscriptionData.name) } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV9.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV9.kt index 0ac485e1..d2e99172 100644 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV9.kt +++ b/src/main/kotlin/org/hyacinthbots/lilybot/database/migrations/main/mainV9.kt @@ -1,7 +1,8 @@ package org.hyacinthbots.lilybot.database.migrations.main -import org.litote.kmongo.coroutine.CoroutineDatabase +import com.mongodb.kotlin.client.coroutine.MongoDatabase -suspend fun mainV9(db: CoroutineDatabase) { - db.dropCollection("logUploadingBlacklistData") +@Suppress("UnusedPrivateMember", "UNUSED_PARAMETER", "RedundantSuspendModifier") +suspend fun mainV9(db: MongoDatabase) { + // db.getCollection<>("logUploadingBlacklistData") } diff --git a/src/main/kotlin/org/hyacinthbots/lilybot/database/storage/MongoDBDataAdapter.kt b/src/main/kotlin/org/hyacinthbots/lilybot/database/storage/MongoDBDataAdapter.kt deleted file mode 100644 index 6790aa07..00000000 --- a/src/main/kotlin/org/hyacinthbots/lilybot/database/storage/MongoDBDataAdapter.kt +++ /dev/null @@ -1,146 +0,0 @@ -/* -* This code was utilized from [cozy](https://github.com/QuiltMC/cozy-discord) by QuiltMC -* and hence is subject to the terms of the Mozilla Public License V. 2.0 -* A copy of this license can be found at https://mozilla.org/MPL/2.0/. -*/ - -package org.hyacinthbots.lilybot.database.storage - -import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent -import com.kotlindiscord.kord.extensions.storage.Data -import com.kotlindiscord.kord.extensions.storage.DataAdapter -import com.kotlindiscord.kord.extensions.storage.StorageUnit -import com.mongodb.client.model.Filters.and -import kotlinx.serialization.InternalSerializationApi -import kotlinx.serialization.json.Json -import kotlinx.serialization.serializer -import org.bson.conversions.Bson -import org.hyacinthbots.lilybot.database.Database -import org.hyacinthbots.lilybot.database.entities.AdaptedData -import org.koin.core.component.inject -import org.litote.kmongo.coroutine.CoroutineCollection -import org.litote.kmongo.eq - -@OptIn(InternalSerializationApi::class) -class MongoDBDataAdapter : DataAdapter(), KordExKoinComponent { - private val database: Database by inject() - private val collectionCache: MutableMap> = mutableMapOf() - - private fun StorageUnit<*>.getIdentifier(): String = - buildString { - append("${storageType.type}/") - - if (guild != null) append("guild-$guild/") - if (channel != null) append("channel-$channel/") - if (user != null) append("user-$user/") - if (message != null) append("message-$message/") - - append(identifier) - } - - private fun getCollection(namespace: String): CoroutineCollection { - var collection = collectionCache[namespace] - - if (collection == null) { - collection = database.configDatabase.getCollection(namespace) - - collectionCache[namespace] = collection - } - - return collection - } - - private fun constructQuery(unit: StorageUnit<*>): Bson { - var query = AdaptedData::identifier eq unit.identifier - - query = and(query, AdaptedData::type eq unit.storageType) - - query = and(query, AdaptedData::channel eq unit.channel) - query = and(query, AdaptedData::guild eq unit.guild) - query = and(query, AdaptedData::message eq unit.message) - query = and(query, AdaptedData::user eq unit.user) - - return query - } - - override suspend fun delete(unit: StorageUnit): Boolean { - removeFromCache(unit) - - val result = getCollection(unit.namespace) - .deleteOne(constructQuery(unit)) - - return result.deletedCount > 0 - } - - override suspend fun get(unit: StorageUnit): R? { - val dataId = unitCache[unit] - - if (dataId != null) { - val data = dataCache[dataId] - - if (data != null) { - return data as R - } - } - - return reload(unit) - } - - override suspend fun reload(unit: StorageUnit): R? { - val dataId = unit.getIdentifier() - val result = getCollection(unit.namespace) - .findOne(constructQuery(unit))?.data - - if (result != null) { - dataCache[dataId] = Json.decodeFromString(unit.dataType.serializer(), result) - unitCache[unit] = dataId - } - - return dataCache[dataId] as R? - } - - override suspend fun save(unit: StorageUnit): R? { - val data = get(unit) ?: return null - - getCollection(unit.namespace).save( - AdaptedData( - identifier = unit.identifier, - - type = unit.storageType, - - channel = unit.channel, - guild = unit.guild, - message = unit.message, - user = unit.user, - - data = Json.encodeToString(unit.dataType.serializer(), data) - ) - ) - - return data - } - - override suspend fun save(unit: StorageUnit, data: R): R { - val dataId = unit.getIdentifier() - - dataCache[dataId] = data - unitCache[unit] = dataId - - getCollection(unit.namespace).save( - AdaptedData( - identifier = unit.identifier, - - type = unit.storageType, - - channel = unit.channel, - guild = unit.guild, - message = unit.message, - user = unit.user, - - data = Json.encodeToString(unit.dataType.serializer(), data) - ) - ) - - return data - } -}