bot runs and can ping/pong
parent
573e32d7e0
commit
effd106847
@ -1,2 +1 @@
|
||||
rootProject.name = 'kagebot'
|
||||
|
||||
rootProject.name = 'moe.kageru.kagebot'
|
||||
|
@ -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
|
||||
}
|
@ -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>)
|
@ -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"
|
||||
}
|
||||
}
|
@ -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")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
[system]
|
||||
serverId = "356414885292277771"
|
||||
admins = [
|
||||
"137780880344088576"
|
||||
]
|
||||
|
||||
[commands]
|
||||
commands = [
|
||||
{
|
||||
input = "!ping",
|
||||
output = "pong"
|
||||
}
|
||||
]
|
Loading…
Reference in new issue