YeetBot/src/main/kotlin/nl/voidcorp/yeetbot/Yeet.kt

95 lines
2.6 KiB
Kotlin
Raw Normal View History

2018-09-13 20:14:31 +02:00
package nl.voidcorp.yeetbot
import com.github.salomonbrys.kotson.fromJson
import net.dv8tion.jda.core.AccountType
import net.dv8tion.jda.core.JDABuilder
import net.dv8tion.jda.core.OnlineStatus
2018-09-13 20:14:31 +02:00
import net.dv8tion.jda.core.entities.Game
import org.apache.logging.log4j.LogManager
import java.io.File
import java.io.FileReader
import java.io.FileWriter
import java.util.*
import kotlin.concurrent.fixedRateTimer
import kotlin.concurrent.schedule
lateinit var config: Config
2018-09-18 11:42:57 +02:00
val logger = LogManager.getLogger("YeetBot")!!
2018-09-13 20:14:31 +02:00
fun main(args: Array<String>) {
config = if (File("conf.json").exists()) {
gson.fromJson(FileReader("conf.json"))
} else {
Config()
}
val token = if (args.isEmpty()) {
throw IllegalArgumentException("missing the token string my dude!")
} else {
args[0]
}
val jda = JDABuilder(AccountType.BOT).setToken(token).build()
jda.addEventListener(MessageListener())
jda.presence.setPresence(OnlineStatus.ONLINE, Game.playing("a banjo"))
fixedRateTimer("ConfigThread", period = 1000 * 60 * 5, initialDelay = 1000 * 10) {
logger.info("Writing config!")
try {
with(FileWriter("conf.json")) {
gson.toJson(config, this)
close()
}
} catch (e: Exception) {
logger.fatal("Could not write config!", e)
}
}
fixedRateTimer("StopTimer", period = 1000) {
if (File("new").exists()) {
2018-09-15 00:28:17 +02:00
File("new").delete()
var tick = false
val t = fixedRateTimer("ColorTick", period = 1000) {
if (tick) {
jda.presence.setPresence(OnlineStatus.DO_NOT_DISTURB, Game.watching("a restart timer!"))
} else {
jda.presence.setPresence(OnlineStatus.IDLE, Game.watching("a restart timer!"))
2018-09-13 20:14:31 +02:00
}
2018-09-15 00:28:17 +02:00
tick = !tick
2018-09-13 20:14:31 +02:00
}
2018-09-15 00:28:17 +02:00
Timer().schedule(1000 * 30) {
t.cancel()
jda.presence.setPresence(OnlineStatus.DO_NOT_DISTURB, Game.watching("myself restart!"))
logger.info("Writing config!")
try {
with(FileWriter("conf.json")) {
gson.toJson(config, this)
close()
2018-09-13 20:14:31 +02:00
}
2018-09-15 00:28:17 +02:00
} catch (e: Exception) {
logger.fatal("Could not write config!", e)
}
Timer().schedule(1000) {
Runtime.getRuntime().exec("sudo service jservice-yeetbot restart")
2018-09-13 20:14:31 +02:00
}
}
2018-09-15 00:28:17 +02:00
}
2018-09-13 20:14:31 +02:00
}
2018-09-18 11:42:57 +02:00
org.eclipse.jetty.util.log.Log.setLog(CustomLogging())
2018-09-18 11:42:57 +02:00
server()
2018-09-15 00:28:17 +02:00
2018-09-13 20:14:31 +02:00
}