refactor: Move stuff around and replace println with Logger
This commit is contained in:
parent
acde0d3ee5
commit
135cbd31a5
3 changed files with 13 additions and 9 deletions
|
@ -1,5 +1,6 @@
|
||||||
package io.github.null2264.tsukumogami.bot.core.module
|
package io.github.null2264.tsukumogami.bot.core.module
|
||||||
|
|
||||||
|
import dev.kord.core.entity.effectiveName
|
||||||
import io.github.null2264.tsukumogami.core.module.annotation.Command
|
import io.github.null2264.tsukumogami.core.module.annotation.Command
|
||||||
import io.github.null2264.tsukumogami.core.Context
|
import io.github.null2264.tsukumogami.core.Context
|
||||||
import io.github.null2264.tsukumogami.core.module.BotModule
|
import io.github.null2264.tsukumogami.core.module.BotModule
|
||||||
|
@ -17,6 +18,6 @@ class GeneralModule : BotModule("General", "idk") {
|
||||||
|
|
||||||
@Command("test")
|
@Command("test")
|
||||||
private suspend fun differentName(ctx: Context) {
|
private suspend fun differentName(ctx: Context) {
|
||||||
ctx.send("Hello World! ${ctx.author}")
|
ctx.send("Hello World! ${ctx.author?.effectiveName}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -38,9 +38,7 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) {
|
||||||
|
|
||||||
client = runBlocking {
|
client = runBlocking {
|
||||||
Kord(currentConfig.token).apply {
|
Kord(currentConfig.token).apply {
|
||||||
on<ReadyEvent> {
|
on<ReadyEvent> { onReady() }
|
||||||
Logger.i { "Online! ${self.username}" }
|
|
||||||
}
|
|
||||||
|
|
||||||
on<MessageCreateEvent> { onMessage(this.message, this) }
|
on<MessageCreateEvent> { onMessage(this.message, this) }
|
||||||
}
|
}
|
||||||
|
@ -65,11 +63,11 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) {
|
||||||
return Context(this, message, candidate?.first, candidate?.second)
|
return Context(this, message, candidate?.first, candidate?.second)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun onCommandError(context: Context, error: CommandException) {
|
open suspend fun onCommandError(context: Context, error: CommandException) {
|
||||||
context.send(error.message!!)
|
context.send(error.message!!)
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun processCommand(message: Message) {
|
open suspend fun processCommand(message: Message) {
|
||||||
val ctx = getContext(message)
|
val ctx = getContext(message)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
@ -83,12 +81,16 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
suspend fun onMessage(message: Message, event: MessageCreateEvent) {
|
open suspend fun onMessage(message: Message, event: MessageCreateEvent) {
|
||||||
if (message.author?.isBot != false) return
|
if (message.author?.isBot != false) return
|
||||||
|
|
||||||
processCommand(message)
|
processCommand(message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
open suspend fun onReady() {
|
||||||
|
Logger.i { "Online! ${client.getSelf().username}" }
|
||||||
|
}
|
||||||
|
|
||||||
fun String.hasPrefix(): Pair<String, String>? {
|
fun String.hasPrefix(): Pair<String, String>? {
|
||||||
if (this.isBlank()) return null
|
if (this.isBlank()) return null
|
||||||
|
|
||||||
|
@ -96,7 +98,7 @@ abstract class AbstractBot(configurator: BotConfigurator.() -> Unit) {
|
||||||
|
|
||||||
prefixes.forEach {
|
prefixes.forEach {
|
||||||
if (this.substring(0, it.length) == it) {
|
if (this.substring(0, it.length) == it) {
|
||||||
ret = Pair(this.substring(0, it.length), this.substring(it.length))
|
ret = Pair(this.substring(0, it.length), this.substring(it.length).split(" ").first())
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package io.github.null2264.tsukumogami.core.module
|
package io.github.null2264.tsukumogami.core.module
|
||||||
|
|
||||||
|
import co.touchlab.kermit.Logger
|
||||||
import io.github.null2264.tsukumogami.core.AbstractBot
|
import io.github.null2264.tsukumogami.core.AbstractBot
|
||||||
import io.github.null2264.tsukumogami.core.module.annotation.Command as CommandAnnotation
|
import io.github.null2264.tsukumogami.core.module.annotation.Command as CommandAnnotation
|
||||||
import io.github.null2264.tsukumogami.core.BotConfigurator
|
import io.github.null2264.tsukumogami.core.BotConfigurator
|
||||||
|
@ -25,7 +26,7 @@ abstract class BotModule(val name: String, val description: String? = null) {
|
||||||
configurator.apply {
|
configurator.apply {
|
||||||
val kMethod = method.kotlinFunction
|
val kMethod = method.kotlinFunction
|
||||||
if (isExists(kMethod?.name)) {
|
if (isExists(kMethod?.name)) {
|
||||||
println("Command already exists")
|
Logger.e { "Command already exists" }
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
kMethod?.let {
|
kMethod?.let {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue