Skip to content

Commit

Permalink
Improve bot initialization and error handling (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholasgrose authored Aug 6, 2023
1 parent bce13c4 commit effebf1
Show file tree
Hide file tree
Showing 23 changed files with 279 additions and 217 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Gateway

A [Paper](https://papermc.io/) server plugin that adds [Discord](https://discord.com/)
-to-[Minecraft](https://www.minecraft.net/en-us) interactions and vice versa.
A [Paper](https://papermc.io/) server plugin that adds
[Discord](https://discord.com/)-to-[Minecraft](https://www.minecraft.net/en-us) interactions and vice versa.
It has a number of extensions that can be enabled or disabled in its configuration, so you don't need to use them all.

## Available extensions:
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
// https://github.com/jlleitschuh/ktlint-gradle
id("org.jlleitschuh.gradle.ktlint") version "11.5.0"
// https://detekt.dev/
id("io.gitlab.arturbosch.detekt") version "1.23.0"
id("io.gitlab.arturbosch.detekt") version "1.23.1"
}

val version: String by project
Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Project info
version=1.6.0
version=1.6.1
group=com.rose.gateway
# Plugin configuration
# https://github.com/pinterest/ktlint
Expand All @@ -11,13 +11,13 @@ paperApiRevision=R0.1
# https://github.com/sksamuel/hoplite
hopliteVersion=2.7.4
# https://github.com/charleskorn/kaml
kamlVersion=0.54.0
kamlVersion=0.55.0
# https://kordex.kotlindiscord.com/
kordexVersion=1.5.8-SNAPSHOT
# https://github.com/utybo/Lixy
lixyVersion=master-SNAPSHOT
# https://ktor.io/
ktorVersion=2.3.2
ktorVersion=2.3.3
# Compilation options for Kotlin
jvmVersion=17
kotlinLanguageVersion=1.9
Expand Down
7 changes: 4 additions & 3 deletions src/main/kotlin/com/rose/gateway/GatewayPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.rose.gateway

import com.rose.gateway.discord.bot.DiscordBot
import com.rose.gateway.discord.bot.DiscordBotController
import com.rose.gateway.minecraft.CommandRegistry
import com.rose.gateway.minecraft.EventListeners
import com.rose.gateway.minecraft.logging.Logger
import com.rose.gateway.shared.concurrency.PluginCoroutineScope
import com.rose.gateway.shared.koin.initializeKoin
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.datetime.Clock
import org.bukkit.plugin.java.JavaPlugin
Expand All @@ -32,11 +33,11 @@ class GatewayPlugin : JavaPlugin(), KoinComponent {
*/
val loader = classLoader

private val bot: DiscordBot by inject()
private val bot: DiscordBotController by inject()
private val coroutineScope: PluginCoroutineScope by inject()

override fun onEnable() {
runBlocking {
coroutineScope.launch {
bot.start()
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/com/rose/gateway/config/PluginConfig.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.rose.gateway.config

import com.rose.gateway.config.schema.Config
import com.rose.gateway.discord.bot.DiscordBot
import com.rose.gateway.discord.bot.DiscordBotController
import com.rose.gateway.shared.concurrency.PluginCoroutineScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
Expand All @@ -15,7 +15,7 @@ import kotlin.reflect.KType
* @constructor Creates plugin config
*/
class PluginConfig : KoinComponent {
private val bot: DiscordBot by inject()
private val bot: DiscordBotController by inject()
private val stringMap: ConfigStringMap by inject()
private val pluginCoroutineScope: PluginCoroutineScope by inject()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.rose.gateway.config.markers

import com.rose.gateway.discord.bot.DiscordBot
import kotlinx.coroutines.runBlocking
import com.rose.gateway.discord.bot.DiscordBotController
import com.rose.gateway.shared.concurrency.PluginCoroutineScope
import kotlinx.coroutines.launch
import kotlinx.serialization.Transient
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
Expand All @@ -18,7 +19,8 @@ open class CommonExtensionConfig(
enabled: Boolean,
@Transient val extensionName: String = "None",
) : KoinComponent, ConfigObject {
private val bot: DiscordBot by inject()
private val bot: DiscordBotController by inject()
private val pluginsScope: PluginCoroutineScope by inject()

/**
* Whether the extension is enabled
Expand All @@ -36,11 +38,11 @@ open class CommonExtensionConfig(
* @param enabled The extensions new status
*/
private fun modifyExtensionLoadedStatus(enabled: Boolean) {
runBlocking {
pluginsScope.launch {
if (enabled) {
bot.bot?.loadExtension(extensionName)
bot.discordBot.kordexBot.await()?.loadExtension(extensionName)
} else {
bot.bot?.unloadExtension(extensionName)
bot.discordBot.kordexBot.await()?.unloadExtension(extensionName)
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/com/rose/gateway/config/schema/BotConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.rose.gateway.config.schema

import com.rose.gateway.config.markers.ConfigItem
import com.rose.gateway.config.markers.ConfigObject
import com.rose.gateway.discord.bot.DiscordBot
import com.rose.gateway.discord.bot.DiscordBotController
import com.rose.gateway.shared.concurrency.PluginCoroutineScope
import com.rose.gateway.shared.serialization.SurrogateBasedSerializer
import com.rose.gateway.shared.serialization.SurrogateConverter
Expand Down Expand Up @@ -30,7 +30,7 @@ class BotConfig(
@ConfigItem val extensions: ExtensionsConfig,
) : KoinComponent, ConfigObject {
private val pluginCoroutineScope: PluginCoroutineScope by inject()
private val bot: DiscordBot by inject()
private val bot: DiscordBotController by inject()

/**
* The Discord bot's bot token
Expand All @@ -53,7 +53,7 @@ class BotConfig(
var botChannels = botChannels
set(value) {
field = value
runBlocking { bot.context.fillBotChannels() }
runBlocking { bot.state.fillBotChannels() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package com.rose.gateway.discord.bot
import com.rose.gateway.config.PluginConfig
import com.rose.gateway.config.access.botChannels
import com.rose.gateway.discord.bot.client.ClientInfo
import com.rose.gateway.shared.concurrency.PluginCoroutineScope
import dev.kord.core.entity.Guild
import dev.kord.core.entity.channel.TextChannel
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

Expand All @@ -13,9 +16,20 @@ import org.koin.core.component.inject
*
* @constructor Creates an empty bot context
*/
class BotContext : KoinComponent {
private val bot: DiscordBot by inject()
class BotState : KoinComponent {
private val bot: DiscordBotController by inject()
private val config: PluginConfig by inject()
private val pluginScope: PluginCoroutineScope by inject()

/**
* The bot's status
*/
var status = BotStatus.NOT_STARTED

/**
* The job running the bot
*/
var botJob: Job? = null

/**
* The text channels the bot operates in
Expand All @@ -27,6 +41,12 @@ class BotContext : KoinComponent {
*/
val botGuilds = mutableSetOf<Guild>()

init {
pluginScope.launch {
fillBotChannels()
}
}

/**
* Fills the valid bot channel set and the valid bot guild set
*/
Expand All @@ -36,7 +56,7 @@ class BotContext : KoinComponent {
botChannels.clear()
botGuilds.clear()

bot.kordClient()?.guilds?.collect { guild ->
bot.discordBot.kordClient()?.guilds?.collect { guild ->
guild.channels.collect { channel ->
if (ClientInfo.hasChannelPermissions(
channel,
Expand Down
Loading

0 comments on commit effebf1

Please sign in to comment.