refactor: Turn prefixes getter to a getter function
So it can be overriden
This commit is contained in:
parent
1a3e8a3335
commit
ab5c116ced
1 changed files with 7 additions and 11 deletions
|
@ -3,10 +3,7 @@ package io.github.null2264.tsukumogami.core
|
||||||
import co.touchlab.kermit.Logger
|
import co.touchlab.kermit.Logger
|
||||||
import dev.kord.core.Kord
|
import dev.kord.core.Kord
|
||||||
import dev.kord.core.entity.Message
|
import dev.kord.core.entity.Message
|
||||||
import dev.kord.core.entity.User
|
|
||||||
import dev.kord.core.event.gateway.ReadyEvent
|
|
||||||
import dev.kord.core.event.message.MessageCreateEvent
|
import dev.kord.core.event.message.MessageCreateEvent
|
||||||
import dev.kord.core.on
|
|
||||||
import dev.kord.gateway.Intent
|
import dev.kord.gateway.Intent
|
||||||
import dev.kord.gateway.PrivilegedIntent
|
import dev.kord.gateway.PrivilegedIntent
|
||||||
import io.github.null2264.tsukumogami.core.commands.Command
|
import io.github.null2264.tsukumogami.core.commands.Command
|
||||||
|
@ -14,10 +11,7 @@ import io.github.null2264.tsukumogami.core.exceptions.CommandException
|
||||||
import io.github.null2264.tsukumogami.core.exceptions.CommandNotFound
|
import io.github.null2264.tsukumogami.core.exceptions.CommandNotFound
|
||||||
import io.github.null2264.tsukumogami.core.module.BotModule
|
import io.github.null2264.tsukumogami.core.module.BotModule
|
||||||
import io.github.null2264.tsukumogami.core.commands.IGroup
|
import io.github.null2264.tsukumogami.core.commands.IGroup
|
||||||
import io.github.null2264.tsukumogami.core.ext.parseCommandAndArguments
|
|
||||||
import io.github.null2264.tsukumogami.core.koin.TsukumogamiKoinComponent
|
import io.github.null2264.tsukumogami.core.koin.TsukumogamiKoinComponent
|
||||||
import kotlin.reflect.full.callSuspend
|
|
||||||
import kotlinx.coroutines.runBlocking
|
|
||||||
import org.koin.core.component.inject
|
import org.koin.core.component.inject
|
||||||
|
|
||||||
open class Bot internal constructor(): IGroup, TsukumogamiKoinComponent {
|
open class Bot internal constructor(): IGroup, TsukumogamiKoinComponent {
|
||||||
|
@ -25,8 +19,6 @@ open class Bot internal constructor(): IGroup, TsukumogamiKoinComponent {
|
||||||
val client: Kord by inject()
|
val client: Kord by inject()
|
||||||
private val modules = mutableMapOf<String, BotModule>()
|
private val modules = mutableMapOf<String, BotModule>()
|
||||||
private val _prefixes = mutableListOf<String>()
|
private val _prefixes = mutableListOf<String>()
|
||||||
val prefixes: List<String> get() = _prefixes.toList()
|
|
||||||
internal lateinit var token: String
|
|
||||||
override val allCommands: MutableMap<String, Command> = mutableMapOf()
|
override val allCommands: MutableMap<String, Command> = mutableMapOf()
|
||||||
|
|
||||||
suspend fun self() = client.getSelf().asUser()
|
suspend fun self() = client.getSelf().asUser()
|
||||||
|
@ -76,7 +68,7 @@ open class Bot internal constructor(): IGroup, TsukumogamiKoinComponent {
|
||||||
|
|
||||||
fun getCommand(name: String?) = allCommands[name]
|
fun getCommand(name: String?) = allCommands[name]
|
||||||
|
|
||||||
private fun getContext(message: Message): Context {
|
private suspend fun getContext(message: Message): Context {
|
||||||
val parsed = message.content.parsePrefixAndCommand()
|
val parsed = message.content.parsePrefixAndCommand()
|
||||||
val context = Context(message, parsed?.first)
|
val context = Context(message, parsed?.first)
|
||||||
context.command = getCommand(parsed?.second)
|
context.command = getCommand(parsed?.second)
|
||||||
|
@ -110,13 +102,17 @@ open class Bot internal constructor(): IGroup, TsukumogamiKoinComponent {
|
||||||
Logger.i { "Online! ${client.getSelf().username}" }
|
Logger.i { "Online! ${client.getSelf().username}" }
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String.parsePrefixAndCommand(): Pair<String, String>? {
|
open suspend fun getPrefixes(): List<String> {
|
||||||
|
return _prefixes.toList()
|
||||||
|
}
|
||||||
|
|
||||||
|
private suspend fun String.parsePrefixAndCommand(): Pair<String, String>? {
|
||||||
if (this.isBlank()) return null
|
if (this.isBlank()) return null
|
||||||
|
|
||||||
var ret: Pair<String, String>? = null
|
var ret: Pair<String, String>? = null
|
||||||
|
|
||||||
run {
|
run {
|
||||||
prefixes.forEach { candidate ->
|
getPrefixes().forEach { candidate ->
|
||||||
val prefix = this.substring(0, candidate.length)
|
val prefix = this.substring(0, candidate.length)
|
||||||
if (prefix != candidate) { return@forEach }
|
if (prefix != candidate) { return@forEach }
|
||||||
val command = this.drop(candidate.length).substringBefore(' ')
|
val command = this.drop(candidate.length).substringBefore(' ')
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue