Skip to content

Commit

Permalink
Merge pull request #7 from catgirlclient/experimental
Browse files Browse the repository at this point in the history
Fully migrate Database to its respective subdirectory
  • Loading branch information
shuuyu authored Oct 14, 2024
2 parents 7871ca2 + 39132f5 commit aea440c
Show file tree
Hide file tree
Showing 124 changed files with 1,031 additions and 873 deletions.
2 changes: 2 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
root = true

[*.ts]
indent_size = 4
File renamed without changes.
1 change: 0 additions & 1 deletion api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ dependencies {
implementation(project(":common"))
implementation(libs.bundles.kotlin)
implementation(libs.bundles.logger)
implementation(libs.bundles.parsers)
implementation(libs.bundles.ktor)
}
2 changes: 2 additions & 0 deletions api/src/main/kotlin/live/shuuyu/nabi/api/NabiAPIManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ class NabiAPIManager(private val config: NabiAPIConfig) {
install(Compression)

routing {
get("/servers/{id}/config") {

}
}
}

Expand Down
24 changes: 21 additions & 3 deletions bot/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import live.shuuyu.plugins.i18n.tasks.GenerateI18nFileTask
import live.shuuyu.plugins.i18n.utils.ParserType

plugins {
id("live.shuuyu.plugins.module")
id("live.shuuyu.plugins.i18n")
id("com.gradleup.shadow") version libs.versions.shadow
}

description = "Core packages that allow Nabi to function."

val generate = tasks.register<GenerateI18nFileTask>("generateI18nFileTasks") {
parserType.set(ParserType.Toml)
languageInputDirectory.set(file("../resources/locales/en/"))
languageOutputDirectory.set(file("${project.buildDir}/generated/locale/"))
generatedPackageName.set("live.shuuyu.nabi.i18n")
}

val shadowTest: Configuration by configurations.creating {
configurations.testImplementation.get().extendsFrom(this)
}

dependencies {
// TODO: Possibly fatjar this?
implementation(project(":subprojects:cache"))
implementation(project(":subprojects:database"))
implementation(project(":subprojects:metrics"))
Expand All @@ -24,18 +31,29 @@ dependencies {
implementation(libs.bundles.discord)
implementation(libs.bundles.database)
implementation(libs.bundles.jackson)
implementation(libs.bundles.parsers)
implementation(libs.bundles.logger)
implementation(libs.bundles.caching)
implementation(libs.kotlin.protobuf)
implementation(libs.bundles.i18n)
implementation(libs.bundles.ktor)

shadowTest(libs.bundles.test) // Shadow all tests in order to avoid conflicts
}

sourceSets.main {
kotlin.srcDirs(generate)
}

tasks {
withType(ShadowJar::class.java) {
archiveBaseName.set("Nabi")
archiveClassifier.set("")
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
configurations = listOf(shadowTest)

exclude("**/LICENSE.txt", "**/LICENSE", "**/LICENSE.md")
mergeServiceFiles()
}

processResources {
from("../resources/locales/en/")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,19 @@ import dev.kord.gateway.Event
import dev.kord.gateway.Gateway
import dev.kord.gateway.GatewayConfiguration
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.withContext
import kotlin.coroutines.CoroutineContext
import kotlin.time.Duration

class NabiGateway(
override val coroutineContext: CoroutineContext,
override val ping: StateFlow<Duration?>
) : Gateway {
override val events = MutableSharedFlow<Event>()
class NabiCustomGateway : Gateway {
override val coroutineContext: CoroutineContext = SupervisorJob() + Dispatchers.Default
override val events: SharedFlow<Event> = MutableSharedFlow()
override val ping: StateFlow<Duration?> = MutableStateFlow(null)

override suspend fun start(configuration: GatewayConfiguration) = withContext(Dispatchers.Default) {

Expand All @@ -28,6 +30,10 @@ class NabiGateway(
override suspend fun send(command: Command) {
TODO("Not yet implemented")
}

suspend fun restart() {

}

override suspend fun stop() {
TODO("Not yet implemented")
Expand Down
20 changes: 16 additions & 4 deletions bot/src/main/kotlin/live/shuuyu/nabi/NabiCore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,22 @@ import live.shuuyu.discordinteraktions.common.commands.MessageCommandDeclaration
import live.shuuyu.discordinteraktions.common.commands.SlashCommandDeclaration
import live.shuuyu.discordinteraktions.platforms.kord.installDiscordInteraKTions
import live.shuuyu.nabi.cache.NabiCacheManager
import live.shuuyu.nabi.database.NabiDatabaseCore
import live.shuuyu.nabi.database.NabiDatabaseManager
import live.shuuyu.nabi.events.EventContext
import live.shuuyu.nabi.events.EventResult
import live.shuuyu.nabi.events.impl.*
import live.shuuyu.nabi.interactions.InteractionsManager
import live.shuuyu.nabi.metrics.NabiMetricsManager
import live.shuuyu.nabi.utils.config.NabiConfig
import live.shuuyu.nabi.utils.i18n.LanguageManager
import kotlin.concurrent.thread
import kotlin.time.measureTimedValue

class NabiCore(
val gatewayManager: NabiGatewayManager,
val config: NabiConfig,
val cache: NabiCacheManager,
val database: NabiDatabaseCore,
val database: NabiDatabaseManager,
val metrics: NabiMetricsManager
) {
companion object {
Expand Down Expand Up @@ -65,27 +66,38 @@ class NabiCore(
UserModule(this)
)

val language = LanguageManager.load()

/*
We need to do this for 2 main reasons.
1. We cannot register more than 100 slash commands, 5 user commands, and 5 message commands
2. It's better if we do this before initializing to prevent issues from coming up.
*/
fun preInitialization() = runBlocking {
private fun preInitialization() = runBlocking {
val slashCommandCount = interaktions.manager.applicationCommandsDeclarations.filterIsInstance<SlashCommandDeclaration>().size
val messageCommandCount = interaktions.manager.applicationCommandsDeclarations.filterIsInstance<MessageCommandDeclaration>().size

logger.info { "Running pre-initialization tasks, this may take a while..." }

require(slashCommandCount > 100) {
"Registered slash command count is more than 100! Exiting the process.... "
}
}

@OptIn(PrivilegedIntent::class)
fun initialize() = runBlocking {
// Initialize all of our microservices before the bot starts to prevent issues from arrising
preInitialization()
database.initialize()
database.createMissingSchemaAndColumns()
database.createMissingTablesAndColums()
manager.registerGlobalApplicationCommands()
manager.registerGuildApplicationCommands(config.discord.defaultGuildId)
cache.initialize(kord)
metrics.start()

logger.info { "Initializing all Gateway instances of Nabi..." }

gatewayManager.gateways.forEach { (shardId, gateway) ->
gateway.installDiscordInteraKTions(interaktions)

Expand Down
15 changes: 8 additions & 7 deletions bot/src/main/kotlin/live/shuuyu/nabi/NabiLauncher.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import dev.kord.gateway.DefaultGateway
import live.shuuyu.common.utils.ParserUtils
import live.shuuyu.nabi.cache.NabiCacheConfig
import live.shuuyu.nabi.cache.NabiCacheManager
import live.shuuyu.nabi.database.NabiDatabaseCore
import live.shuuyu.nabi.database.NabiDatabaseConfig
import live.shuuyu.nabi.database.NabiDatabaseManager
import live.shuuyu.nabi.metrics.NabiMetricsConfig
import live.shuuyu.nabi.metrics.NabiMetricsManager
import live.shuuyu.nabi.utils.config.DatabaseConfig
import live.shuuyu.nabi.utils.config.NabiConfig
import live.shuuyu.nabi.utils.config.NabiDiscordConfig
import java.io.File
Expand All @@ -28,10 +28,11 @@ object NabiLauncher {
result.discord.publicKey,
result.discord.port
),
DatabaseConfig(
result.database.address,
result.database.username,
result.database.password
NabiDatabaseConfig(
result.database.jdbcUrl,
result.database.jdbcUsername,
result.database.jdbcPassword,
result.database.port
),
NabiCacheConfig(
result.cache.addresses,
Expand All @@ -49,7 +50,7 @@ object NabiLauncher {

val gatewayManager = NabiGatewayManager(config.discord.shards, gateways)
val cacheManager = NabiCacheManager(config.cache)
val databaseManager = NabiDatabaseCore(config.database)
val databaseManager = NabiDatabaseManager(config.database)
val metricsManager = NabiMetricsManager(config.metrics)

val nabi = NabiCore(gatewayManager, config, cacheManager, databaseManager, metricsManager)
Expand Down
70 changes: 0 additions & 70 deletions bot/src/main/kotlin/live/shuuyu/nabi/database/NabiDatabaseCore.kt

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit aea440c

Please sign in to comment.