bot runs and can ping/pong

This commit is contained in:
kageru 2019-06-08 11:07:52 +02:00
parent 573e32d7e0
commit effd106847
Signed by: kageru
GPG Key ID: 8282A2BEA4ADA3D2
7 changed files with 119 additions and 5 deletions

View File

@ -2,6 +2,15 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
plugins {
kotlin("jvm") version "1.3.31"
application
}
sourceSets {
getByName("main").resources.srcDirs("src/main/resources")
}
sourceSets["main"].resources.srcDir("src/main/resources")
application {
mainClassName = "moe.kageru.kagebot.MainKt"
}
group = "moe.kageru.kagebot"
@ -12,10 +21,11 @@ repositories {
}
dependencies {
implementation(kotlin("stdlib-jdk11"))
compile("com.electronwill.night-config:toml:3.6.0")
implementation(kotlin("stdlib-jdk8"))
implementation("org.javacord:javacord:3.0.4")
compile("com.moandjiezana.toml:toml4j:0.7.2")
}
tasks.withType<KotlinCompile> {
kotlinOptions.jvmTarget = "1.11"
kotlinOptions.jvmTarget = "1.8"
}

View File

@ -1,2 +1 @@
rootProject.name = 'kagebot'
rootProject.name = 'moe.kageru.kagebot'

View File

@ -0,0 +1,6 @@
package moe.kageru.kagebot
class Command(private val input: String, private val output: String) {
fun matches(msg: String) = msg.startsWith(this.input)
fun respond() = this.output
}

View File

@ -0,0 +1,29 @@
package moe.kageru.kagebot
import com.moandjiezana.toml.Toml
import java.io.File
class Config(val system: System, val commands: Commands) {
companion object {
val config: Config by lazy { read("config.toml") }
val secret = File("secret").readText().replace("\n", "")
private fun read(path: String): Config {
val rawConfig: Toml = Toml().read(run {
val file = File(path)
if (file.isFile) {
return@run file
}
println("Config not found, falling back to defaults...")
File(this::class.java.classLoader.getResource(path)!!.toURI())
})
return rawConfig.to(Config::class.java)
}
}
}
data class System(val serverId: String, val admins: List<String>)
// wrapper for toml deserialization
data class Commands(val commands: List<Command>)

View File

@ -0,0 +1,25 @@
package moe.kageru.kagebot
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.logging.*
object Log {
val log: Logger by lazy {
val log = Logger.getGlobal()
val fh = FileHandler("kagebot.log", true)
val formatter = LogFormatter()
fh.formatter = formatter
log.addHandler(fh)
return@lazy log
}
}
private class LogFormatter : Formatter() {
private val timeFormatter: DateTimeFormatter =
DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").withZone(ZoneId.systemDefault())
override fun format(record: LogRecord): String {
return "[${record.level}] ${timeFormatter.format(record.instant)}: ${record.message}\n"
}
}

View File

@ -0,0 +1,32 @@
package moe.kageru.kagebot
import moe.kageru.kagebot.Config.Companion.config
import moe.kageru.kagebot.Log.log
import org.javacord.api.DiscordApiBuilder
import java.lang.System
fun main() {
try {
createBot()
} catch (e: Exception) {
log.warning("An exception occurred in the main thread, exiting. ${e.stackTrace.joinToString("\n")}")
System.exit(1)
}
}
fun createBot() {
val api = DiscordApiBuilder().setToken(Config.secret).login().join()
println(config.system.admins)
Runtime.getRuntime().addShutdownHook(Thread {
log.info("Bot has been interrupted. Shutting down.")
api.disconnect()
})
log.info("kagebot Mk II running")
api.addMessageCreateListener { event ->
if (config.commands.commands[0].matches(event.messageContent)) {
event.channel.sendMessage(config.commands.commands[0].respond())
println("message was created")
}
}
}

View File

@ -0,0 +1,13 @@
[system]
serverId = "356414885292277771"
admins = [
"137780880344088576"
]
[commands]
commands = [
{
input = "!ping",
output = "pong"
}
]