Skip to content

Commit

Permalink
Use a constant to determine the collection name but do it better
Browse files Browse the repository at this point in the history
  • Loading branch information
NoComment1105 committed Sep 5, 2023
1 parent 472f1b2 commit 86f09cd
Show file tree
Hide file tree
Showing 46 changed files with 139 additions and 55 deletions.
22 changes: 22 additions & 0 deletions src/main/kotlin/org/hyacinthbots/lilybot/database/Collection.kt
Original file line number Diff line number Diff line change
@@ -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<MagicCollectionData>(name)
*
* suspend fun get(): List<MagicCollectionData> = collection.find().toList()
*
* companion object : Collection("magicCollection")
* }
* ```
*
* @property name The name of the collection
*/
abstract class Collection(val name: String)
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class AutoThreadingCollection : KordExKoinComponent {
private val db: Database by inject()

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

/**
* Gets all auto threads for a given [inputGuildId].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LoggingConfigCollection : KordExKoinComponent {
private val configDb: Database by inject()

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

/**
* Gets the logging config for the given guild using the [guildId][inputGuildId].
Expand Down Expand Up @@ -72,7 +72,7 @@ class ModerationConfigCollection : KordExKoinComponent {
private val configDb: Database by inject()

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

/**
* Gets the Moderation config for the given guild using the [guildId][inputGuildId].
Expand Down Expand Up @@ -121,7 +121,7 @@ class UtilityConfigCollection : KordExKoinComponent {
private val configDb: Database by inject()

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

/**
* Gets the Utility config for the given guild using the [guildId][inputGuildId].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class GalleryChannelCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<GalleryChannelData>("galleryChannelData")
internal val collection = db.mainDatabase.getCollection<GalleryChannelData>(GalleryChannelData.name)

/**
* Collects every gallery channel in the database into a [List].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GithubCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<GithubData>("githubData")
internal val collection = db.mainDatabase.getCollection<GithubData>(GithubData.name)

/**
* Gets the default repo for GitHub commands.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class GuildLeaveTimeCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<GuildLeaveTimeData>("guildLeaveTimeData")
internal val collection = db.mainDatabase.getCollection<GuildLeaveTimeData>(GuildLeaveTimeData.name)

/**
* Adds the time Lily bot left a guild with a config.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class MainMetaCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<MainMetaData>("mainMetaData")
internal val collection = db.mainDatabase.getCollection<MainMetaData>(MainMetaData.name)

/**
* Gets the main metadata from the database.
Expand Down Expand Up @@ -68,7 +68,7 @@ class ConfigMetaCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.configDatabase.getCollection<ConfigMetaData>("configMetaData")
internal val collection = db.configDatabase.getCollection<ConfigMetaData>(ConfigMetaData.name)

/**
* Gets the config metadata from the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class NewsChannelPublishingCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<NewsChannelPublishingData>("newsChannelPublishingData")
internal val collection = db.mainDatabase.getCollection<NewsChannelPublishingData>(NewsChannelPublishingData.name)

/**
* Adds a channel for auto-publishing.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ReminderCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<ReminderData>("reminderData")
internal val collection = db.mainDatabase.getCollection<ReminderData>(ReminderData.name)

/**
* Gets all the reminders currently in the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class RoleMenuCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<RoleMenuData>("roleMenuData")
internal val collection = db.mainDatabase.getCollection<RoleMenuData>(RoleMenuData.name)

/**
* Using the provided [inputMessageId] the associated [RoleMenuData] will be returned from the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class RoleSubscriptionCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<RoleSubscriptionData>("roleSubscriptionData")
internal val collection = db.mainDatabase.getCollection<RoleSubscriptionData>(RoleSubscriptionData.name)

/**
* Gets the roles that are subscribable for a given guild.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class StatusCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<StatusData>("statusData")
internal val collection = db.mainDatabase.getCollection<StatusData>(StatusData.name)

/**
* Gets Lily's status from the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class TagsCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<TagsData>("tagsData")
internal val collection = db.mainDatabase.getCollection<TagsData>(TagsData.name)

/**
* Gets the given tag using it's [name] and returns its [TagsData]. If the tag does not exist.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ThreadsCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<ThreadData>("threadsData")
internal val collection = db.mainDatabase.getCollection<ThreadData>(ThreadData.name)

/**
* Using the provided [inputThreadId] the thread is returned.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class UptimeCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<UptimeData>("uptimeData")
internal val collection = db.mainDatabase.getCollection<UptimeData>(UptimeData.name)

/**
* Gets the uptime data from the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class WarnCollection : KordExKoinComponent {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<WarnData>("warnData")
internal val collection = db.mainDatabase.getCollection<WarnData>(WarnData.name)

/**
* Gets the number of points the provided [inputUserId] has in the provided [inputGuildId] from the database.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class WelcomeChannelCollection : KordExKoinComponent, CozyWelcomeChannelData {
private val db: Database by inject()

@PublishedApi
internal val collection = db.mainDatabase.getCollection<WelcomeChannelData>("welcomeChannelData")
internal val collection = db.mainDatabase.getCollection<WelcomeChannelData>(WelcomeChannelData.name)

override suspend fun getChannelURLs(): Map<Snowflake, String> =
collection.find()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -29,4 +30,6 @@ data class AutoThreadingData(
val mention: Boolean,
val creationMessage: String?,
val addModsAndRole: Boolean
)
) {
companion object : Collection("autoThreadingData")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -70,4 +75,6 @@ data class ModerationConfigData(
data class UtilityConfigData(
val guildId: Snowflake,
val utilityLogChannel: Snowflake?
)
) {
companion object : Collection("utilityConfigData")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -14,4 +15,6 @@ import kotlinx.serialization.Serializable
data class GalleryChannelData(
val guildId: Snowflake,
val channelId: Snowflake
)
) {
companion object : Collection("galleryChannelData")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,4 +16,6 @@ import kotlinx.serialization.Serializable
data class GithubData(
val guildId: Snowflake,
val defaultRepo: String
)
) {
companion object : Collection("githubData")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,4 +16,6 @@ import kotlinx.serialization.Serializable
data class GuildLeaveTimeData(
val guildId: Snowflake,
val guildLeaveTime: Instant
)
) {
companion object : Collection("guildLeaveTimeData")
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand All @@ -28,4 +31,6 @@ data class MainMetaData(
data class ConfigMetaData(
val version: Int,
val id: String = "configMeta"
)
) {
companion object : Collection("configMetaData")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -15,4 +16,6 @@ import kotlinx.serialization.Serializable
data class NewsChannelPublishingData(
val guildId: Snowflake,
val channelId: Snowflake
)
) {
companion object : Collection("newsChannelPublishingData")
}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,4 +15,6 @@ data class PublicMemberLogData(
val pingNewUsers: Boolean,
val joinMessage: String?,
val leaveMessage: String?
)
) {
companion object : Collection("publicMemberLogData")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -35,4 +36,6 @@ data class ReminderData(
val repeating: Boolean,
val repeatingInterval: DateTimePeriod?,
val id: Long
)
) {
companion object : Collection("reminderData")
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -18,4 +19,6 @@ data class RoleMenuData(
val channelId: Snowflake,
val guildId: Snowflake,
val roles: MutableList<Snowflake>
)
) {
companion object : Collection("roleMenuData")
}
Loading

0 comments on commit 86f09cd

Please sign in to comment.