Skip to content
This repository has been archived by the owner on Apr 16, 2023. It is now read-only.

Deprecate current auto logging methods for building bot, and create new which have optional login parameter and returns CommandProcessor. #24

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,10 @@ If you have a suggestion for more targets, you can create an issue (or go to our
```kotlin
@file:Autowired

suspend fun main() = bot("token") {
configure()
suspend fun main() {
bot("token", login = true) {
configure()
}
}

val prefix = prefix {
Expand Down
2 changes: 1 addition & 1 deletion kordx-commands-processor/src/test/kotlin/ProcessFile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ val kordPrefix = prefix {
kord { literal("+") }
}

suspend fun testReference() = bot("sample") {
suspend fun testReference() = bot("sample", login = true) {
configure()
}

Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
package com.gitlab.kordlib.kordx.commands.kord

import com.gitlab.kordlib.core.Kord
import com.gitlab.kordlib.kordx.commands.kord.model.processor.*
import com.gitlab.kordlib.kordx.commands.kord.model.processor.KordContext
import com.gitlab.kordlib.kordx.commands.kord.model.processor.KordContextConverter
import com.gitlab.kordlib.kordx.commands.kord.model.processor.KordErrorHandler
import com.gitlab.kordlib.kordx.commands.kord.model.processor.KordEventSource
import com.gitlab.kordlib.kordx.commands.kord.model.processor.KordProcessorBuilder
import com.gitlab.kordlib.kordx.commands.kord.model.processor.eventFilter
import com.gitlab.kordlib.kordx.commands.kord.plug.KordPlugSocket
import com.gitlab.kordlib.kordx.commands.model.context.CommonContext
import com.gitlab.kordlib.kordx.commands.model.processor.BaseEventHandler
import com.gitlab.kordlib.kordx.commands.model.processor.CommandProcessor
import com.gitlab.kordlib.kordx.commands.model.processor.EventHandler
import com.gitlab.kordlib.kordx.commands.model.processor.ProcessorBuilder
import mu.KotlinLogging
import org.koin.dsl.module
import com.gitlab.kordlib.kordx.commands.model.processor.EventHandler

private val logger = KotlinLogging.logger {}

Expand Down Expand Up @@ -95,22 +100,58 @@ class BotBuilder(
}
}.build()


}

/**
* Creates a bot with the given [token] as discord bot token, applying [configure] to the bot's configuration.
* Once created, the bot will log in to the gateway and suspend until the gateway until logged out.
*/
suspend inline fun bot(token: String, configure: KordProcessorBuilder.() -> Unit) = bot(Kord(token), configure)
@Deprecated(
message = "Use bot(token, login, configure) instead.",
replaceWith = ReplaceWith("bot(token, login = true, configure)"),
level = DeprecationLevel.WARNING
)
suspend inline fun bot(token: String, configure: KordProcessorBuilder.() -> Unit) {
bot(Kord(token), login = true, configure)
}

/**
* Creates a bot with the given [kord] instance, applying [configure] to the bot's configuration.
* Once created, the bot will log in to the gateway and suspend until the gateway until logged out.
*/
@Deprecated(
message = "Use bot(kord, login, configure) instead.",
replaceWith = ReplaceWith("bot(kord, login = true, configure)"),
level = DeprecationLevel.WARNING
)
suspend inline fun bot(kord: Kord, configure: KordProcessorBuilder.() -> Unit) {
val builder = BotBuilder(kord)
builder.processorBuilder.configure()
builder.build()
kord.login()
bot(kord, login = true, configure)
}

/**
* Creates a bot with the given [token] as discord bot token, applying [configure] to the bot's configuration.
* If [login] is true, the bot will log in to the gateway and suspend until the gateway until logged out.
*/
suspend inline fun bot(
token: String,
login: Boolean = false,
configure: KordProcessorBuilder.() -> Unit
): Pair<Kord, CommandProcessor> {
val kord = Kord(token)
val commandProcessor = bot(kord, login, configure)
return Pair(kord, commandProcessor)
}

/**
* Creates a bot with the given [kord] instance, applying [configure] to the bot's configuration.
* If [login] is true, the bot will log in to the gateway and suspend until the gateway logged out.
*/
suspend inline fun bot(
kord: Kord,
login: Boolean,
configure: KordProcessorBuilder.() -> Unit
): CommandProcessor =
BotBuilder(kord)
.apply { processorBuilder.configure() }
.build()
.also { if (login) kord.login() }
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,42 @@ import kapt.kotlin.generated.configure
import kotlinx.coroutines.Dispatchers

/**
* entry method, `configure` is generated on build.
* Entry method, `configure` is generated on build.
*/
suspend fun main() = bot(buildKord()) {
configure()
suspend fun main() {
Examples.way1()
// Examples.way2()
}

@Suppress("UNUSED_VARIABLE")
object Examples {
/**
* Way 1: Usage with token and default settings of [com.gitlab.kordlib.core.builder.kord.KordBuilder].
*/
suspend fun way1() {
// If login is passed as true, there is no need of manually invoking Kord.login()
// and it will suspend this line until the gateway until logged out.
val (kord, commandProcessor) = bot("token", login = false) {
configure()
}
// ...
kord.login()
}

/**
* Way 2: Create with a [Kord] instance.
*/
suspend fun way2() {
val kord = buildKord()

// If login is passed as true, there is no need of manually invoking Kord.login()
// and it will suspend this line until the gateway until logged out.
val commandsProcessor = bot(kord, login = false) {
configure()
}
// ...
kord.login()
}
}

suspend fun buildKord() = Kord(System.getenv("token")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import com.gitlab.kordlib.kordx.commands.model.command.invoke
import com.gitlab.kordlib.kordx.commands.kord.module.module
import com.gitlab.kordlib.kordx.emoji.Emojis
import org.koin.core.get
import org.koin.dsl.module

/**
* register our CommandSwitch dependency
*/
val commandSwitchDependencies = org.koin.dsl.module {
val commandSwitchDependencies = module {
single { CommandSwitch() }
}

Expand All @@ -39,7 +40,7 @@ fun Command<*>.enable() = get<CommandSwitch>().set(this, true)
val Command<*>.isEnabled get() = get<CommandSwitch>()[this]

/**
* Cancel commands that we've disabled with the [switch].
* Cancel commands that we've disabled with the [Command.disable].
*/
fun ignoreDisabledCommands() = precondition {
command.isEnabled.also {
Expand Down