Utility IRC bot
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package com.chameth.hexbot
  2. import com.chameth.hexbot.modules.Hue
  3. import com.chameth.hexbot.modules.Module
  4. import com.dmdirc.ktirc.IrcClient
  5. import com.dmdirc.ktirc.events.IrcEvent
  6. import com.dmdirc.ktirc.events.ServerDisconnected
  7. import com.dmdirc.ktirc.events.ServerReady
  8. import com.dmdirc.ktirc.messages.sendJoin
  9. import com.ufoscout.properlty.Properlty
  10. import com.ufoscout.properlty.reader.EnvironmentVariablesReader
  11. import com.ufoscout.properlty.reader.PropertiesResourceReader
  12. import kotlinx.coroutines.Dispatchers
  13. import kotlinx.coroutines.GlobalScope
  14. import kotlinx.coroutines.launch
  15. import kotlinx.coroutines.runBlocking
  16. import org.jetbrains.exposed.sql.Database
  17. import java.util.logging.Level
  18. import java.util.logging.LogManager
  19. class HexBot(private val config: Properlty, availableModules: List<Module>) {
  20. private var client: IrcClient? = null
  21. private val channels
  22. get() = config.getArray("irc.channels")
  23. private val modules = availableModules.filter { it.init(config) }
  24. fun connect() {
  25. with (IrcClient {
  26. server {
  27. host = config["irc.server"].orElseThrow { IllegalStateException("irc.server not configured") }
  28. port = config.getInt("irc.port").orElseThrow { IllegalStateException("irc.port not configured") }
  29. useTls = config.getBoolean("irc.tls", false)
  30. password = config["irc.password", null]
  31. }
  32. profile {
  33. nickname = config["irc.nickname", "Hex"]
  34. realName = config["irc.realname", "+++Divide By Cucumber Error+++"]
  35. username = config["irc.ident", "hexbot"]
  36. }
  37. sasl {
  38. username = config["irc.nickserv.user", null]
  39. password = config["irc.nickserv.password", null]
  40. }
  41. }) {
  42. client = this
  43. onEvent {
  44. handleEvent(this, it)
  45. }
  46. connect()
  47. }
  48. }
  49. private fun handleEvent(client: IrcClient, event: IrcEvent) {
  50. when (event) {
  51. is ServerReady -> joinChannels()
  52. is ServerDisconnected -> client.connect()
  53. }
  54. for (module in modules) {
  55. GlobalScope.launch(Dispatchers.IO) {
  56. module.processEvent(client, event)
  57. }
  58. }
  59. }
  60. private fun joinChannels() {
  61. channels.forEach { client?.sendJoin(it) }
  62. }
  63. }
  64. internal fun loadConfig(): Properlty = Properlty
  65. .builder()
  66. .caseSensitive(false)
  67. .add(PropertiesResourceReader.build("./hexbot.properties").ignoreNotFound(true))
  68. .add(EnvironmentVariablesReader().replace("HEXBOT_", "").replace("_", "."))
  69. .build()
  70. fun main() = runBlocking<Unit> {
  71. val rootLogger = LogManager.getLogManager().getLogger("")
  72. rootLogger.level = Level.FINEST
  73. for (h in rootLogger.handlers) {
  74. h.level = Level.FINEST
  75. }
  76. Database.connect("jdbc:sqlite:data.db", "org.sqlite.JDBC")
  77. val bot = HexBot(loadConfig(), listOf(Hue()))
  78. bot.connect()
  79. readLine()
  80. }