Skip to content

Commit

Permalink
Revert "Switch to official mongo driver (#351)"
Browse files Browse the repository at this point in the history
This reverts commit 8557d4f
  • Loading branch information
NoComment1105 committed Sep 13, 2023
1 parent 2b0e6f7 commit d76bc5c
Show file tree
Hide file tree
Showing 60 changed files with 368 additions and 389 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/)'s [Kotlin Driver](https://www.mongodb.com/docs/drivers/kotlin/)
* [MongoDB](https://www.mongodb.com/) and [KMongo](https://litote.org/kmongo/)
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
Expand Down
3 changes: 1 addition & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ 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)

Expand All @@ -65,7 +64,7 @@ dependencies {
implementation(libs.github.api)

// KMongo
implementation(libs.mongo.driver)
implementation(libs.kmongo)

// Cozy's welcome module
implementation(libs.cozy.welcome)
Expand Down
8 changes: 3 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ grgit = "5.2.0"
blossom = "2.0.1"

# Libraries
#kord-extensions = "1.5.9-20230820.204324-8"
kord-extensions = "1.5.9-SNAPSHOT"
kord-extensions = "1.5.9-20230809.104126-1"
logging = "5.1.0"
logback = "1.4.11"
github-api = "1.316"
mongo-driver = "4.10.2"
kmongo = "4.10.0"
cozy-welcome = "1.0.1-SNAPSHOT"
dma = "v0.2.1"
docgenerator = "0.1.2-SNAPSHOT"
Expand All @@ -23,12 +22,11 @@ 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" }
mongo-driver = { module = "org.mongodb:mongodb-driver-kotlin-coroutine", version.ref = "mongo-driver" }
kmongo = { module = "org.litote.kmongo:kmongo-coroutine-serialization", version.ref = "kmongo" }
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"}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/org/hyacinthbots/lilybot/LilyBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
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
Expand All @@ -16,6 +15,7 @@ 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
Expand Down Expand Up @@ -60,7 +60,7 @@ val docFile = Path("./docs/commands.md")
suspend fun main() {
val bot = ExtensibleBot(BOT_TOKEN) {
database(true)
mongoDB()
dataAdapter(::MongoDBDataAdapter)

members {
lockMemberRequests = true // Collect members one at a time to avoid hitting rate limits
Expand Down
9 changes: 4 additions & 5 deletions src/main/kotlin/org/hyacinthbots/lilybot/database/Cleanups.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
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
Expand All @@ -26,6 +24,7 @@ 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
Expand All @@ -39,10 +38,10 @@ object Cleanups : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val guildLeaveTimeCollection = db.mainDatabase.getCollection<GuildLeaveTimeData>(GuildLeaveTimeData.name)
internal val guildLeaveTimeCollection = db.mainDatabase.getCollection<GuildLeaveTimeData>()

@PublishedApi
internal val threadDataCollection = db.mainDatabase.getCollection<ThreadData>(ThreadData.name)
internal val threadDataCollection = db.mainDatabase.getCollection<ThreadData>()

@PublishedApi
internal val cleanupsLogger = KotlinLogging.logger("Database Cleanups")
Expand Down Expand Up @@ -76,7 +75,7 @@ object Cleanups : KordExKoinComponent {
UtilityConfigCollection().clearConfig(it.guildId)
WarnCollection().clearWarns(it.guildId)
WelcomeChannelCollection().removeWelcomeChannelsForGuild(it.guildId, kord)
guildLeaveTimeCollection.deleteOne(eq(GuildLeaveTimeData::guildId.name, it.guildId))
guildLeaveTimeCollection.deleteOne(GuildLeaveTimeData::guildId eq it.guildId)
deletedGuildData += 1 // Increment the counter for logging
}
}
Expand Down
22 changes: 0 additions & 22 deletions src/main/kotlin/org/hyacinthbots/lilybot/database/Collection.kt

This file was deleted.

15 changes: 3 additions & 12 deletions src/main/kotlin/org/hyacinthbots/lilybot/database/Database.kt
Original file line number Diff line number Diff line change
@@ -1,38 +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 = MongoClient.create(settings)
private val client = KMongo.createClient(settings).coroutine

/** The main database for storing data. */
val mainDatabase get() = client.getDatabase("LilyBot")

/** 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.
*
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
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
Expand All @@ -24,7 +22,7 @@ class AutoThreadingCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<AutoThreadingData>(AutoThreadingData.name)
internal val collection = db.mainDatabase.getCollection<AutoThreadingData>()

/**
* Gets all auto threads for a given [inputGuildId].
Expand All @@ -35,7 +33,7 @@ class AutoThreadingCollection : KordExKoinComponent {
* @since 4.6.0
*/
suspend inline fun getAllAutoThreads(inputGuildId: Snowflake): List<AutoThreadingData> =
collection.find(eq(AutoThreadingData::guildId.name, inputGuildId)).toList()
collection.find(AutoThreadingData::guildId eq inputGuildId).toList()

/**
* Gets a single auto thread based off the channel ID.
Expand All @@ -46,7 +44,7 @@ class AutoThreadingCollection : KordExKoinComponent {
* @since 4.6.0
*/
suspend inline fun getSingleAutoThread(inputChannelId: Snowflake): AutoThreadingData? =
collection.findOne(eq(AutoThreadingData::channelId.name, inputChannelId))
collection.findOne(AutoThreadingData::channelId eq inputChannelId)

/**
* Sets a new auto thread.
Expand All @@ -56,7 +54,7 @@ class AutoThreadingCollection : KordExKoinComponent {
* @since 4.6.0
*/
suspend inline fun setAutoThread(inputAutoThreadData: AutoThreadingData) {
collection.deleteOne(eq(AutoThreadingData::channelId.name, inputAutoThreadData.channelId))
collection.deleteOne(AutoThreadingData::channelId eq inputAutoThreadData.channelId)
collection.insertOne(inputAutoThreadData)
}

Expand All @@ -68,7 +66,7 @@ class AutoThreadingCollection : KordExKoinComponent {
* @since 4.6.0
*/
suspend inline fun deleteAutoThread(inputChannelId: Snowflake) =
collection.deleteOne(eq(AutoThreadingData::channelId.name, inputChannelId))
collection.deleteOne(AutoThreadingData::channelId eq inputChannelId)

/**
* Deletes auto threads for a given guild.
Expand All @@ -78,5 +76,5 @@ class AutoThreadingCollection : KordExKoinComponent {
* @since 4.6.0
*/
suspend inline fun deleteGuildAutoThreads(inputGuildId: Snowflake) =
collection.deleteMany(eq(AutoThreadingData::guildId.name, inputGuildId))
collection.deleteMany(AutoThreadingData::guildId eq inputGuildId)
}
Original file line number Diff line number Diff line change
@@ -1,14 +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 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
Expand All @@ -23,7 +22,7 @@ class LoggingConfigCollection : KordExKoinComponent {
private val configDb: Database by inject()

@PublishedApi
internal val collection = configDb.configDatabase.getCollection<LoggingConfigData>(LoggingConfigData.name)
internal val collection = configDb.configDatabase.getCollection<LoggingConfigData>()

/**
* Gets the logging config for the given guild using the [guildId][inputGuildId].
Expand All @@ -34,7 +33,7 @@ class LoggingConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun getConfig(inputGuildId: Snowflake): LoggingConfigData? =
collection.findOne(eq(LoggingConfigData::guildId.name, inputGuildId))
collection.findOne(LoggingConfigData::guildId eq inputGuildId)

/**
* Adds the given [loggingConfig] to the database.
Expand All @@ -44,7 +43,7 @@ class LoggingConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun setConfig(loggingConfig: LoggingConfigData) {
collection.deleteOne(eq(LoggingConfigData::guildId.name, loggingConfig.guildId))
collection.deleteOne(LoggingConfigData::guildId eq loggingConfig.guildId)
collection.insertOne(loggingConfig)
}

Expand All @@ -56,7 +55,7 @@ class LoggingConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun clearConfig(inputGuildId: Snowflake) =
collection.deleteOne(eq(LoggingConfigData::guildId.name, inputGuildId))
collection.deleteOne(LoggingConfigData::guildId eq inputGuildId)
}

/**
Expand All @@ -72,7 +71,7 @@ class ModerationConfigCollection : KordExKoinComponent {
private val configDb: Database by inject()

@PublishedApi
internal val collection = configDb.configDatabase.getCollection<ModerationConfigData>(ModerationConfigData.name)
internal val collection = configDb.configDatabase.getCollection<ModerationConfigData>()

/**
* Gets the Moderation config for the given guild using the [guildId][inputGuildId].
Expand All @@ -83,7 +82,7 @@ class ModerationConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun getConfig(inputGuildId: Snowflake): ModerationConfigData? =
collection.findOne(eq(ModerationConfigData::guildId.name, inputGuildId))
collection.findOne(ModerationConfigData::guildId eq inputGuildId)

/**
* Adds the given [moderationConfig] to the database.
Expand All @@ -93,7 +92,7 @@ class ModerationConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun setConfig(moderationConfig: ModerationConfigData) {
collection.deleteOne(eq(ModerationConfigData::guildId.name, moderationConfig.guildId))
collection.deleteOne(ModerationConfigData::guildId eq moderationConfig.guildId)
collection.insertOne(moderationConfig)
}

Expand All @@ -105,7 +104,7 @@ class ModerationConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun clearConfig(inputGuildId: Snowflake) =
collection.deleteOne(eq(ModerationConfigData::guildId.name, inputGuildId))
collection.deleteOne(ModerationConfigData::guildId eq inputGuildId)
}

/**
Expand All @@ -121,7 +120,7 @@ class UtilityConfigCollection : KordExKoinComponent {
private val configDb: Database by inject()

@PublishedApi
internal val collection = configDb.configDatabase.getCollection<UtilityConfigData>(UtilityConfigData.name)
internal val collection = configDb.configDatabase.getCollection<UtilityConfigData>()

/**
* Gets the Utility config for the given guild using the [guildId][inputGuildId].
Expand All @@ -132,7 +131,7 @@ class UtilityConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun getConfig(inputGuildId: Snowflake): UtilityConfigData? =
collection.findOne(eq(UtilityConfigData::guildId.name, inputGuildId))
collection.findOne(UtilityConfigData::guildId eq inputGuildId)

/**
* Adds the given [utilityConfig] to the database.
Expand All @@ -142,7 +141,7 @@ class UtilityConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun setConfig(utilityConfig: UtilityConfigData) {
collection.deleteOne(eq(UtilityConfigData::guildId.name, utilityConfig.guildId))
collection.deleteOne(UtilityConfigData::guildId eq utilityConfig.guildId)
collection.insertOne(utilityConfig)
}

Expand All @@ -154,5 +153,5 @@ class UtilityConfigCollection : KordExKoinComponent {
* @since 4.0.0
*/
suspend inline fun clearConfig(inputGuildId: Snowflake) =
collection.deleteOne(eq(UtilityConfigData::guildId.name, inputGuildId))
collection.deleteOne(UtilityConfigData::guildId eq inputGuildId)
}
Loading

0 comments on commit d76bc5c

Please sign in to comment.