diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt index bdcd498..500ab08 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt @@ -3,15 +3,15 @@ package org.kraftwerk28.spigot_tg_bridge import org.bukkit.configuration.file.YamlConfiguration class Commands(yamlCfg: YamlConfiguration) { - val time: String - val online: String - val chatID: String + val time: String? + val online: String? + val chatID: String? init { yamlCfg.run { - time = getString("commands.time", "time")!! - online = getString("commands.online", "online")!! - chatID = getString("commands.chat_id", "chat_id")!! + time = getString("commands.time") + online = getString("commands.online") + chatID = getString("commands.chat_id") } } } \ No newline at end of file diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt index 45492d1..02cec29 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt @@ -25,7 +25,7 @@ class EventHandler( @EventHandler fun onPlayerJoin(event: PlayerJoinEvent) { if (!config.logJoinLeave) return - val text = "${TgBot.escapeHTML(event.player.displayName)} " + + val text = "${TgBot.escape(event.player.displayName)} " + "${config.joinString}." plugin.tgBot.broadcastToTG(text) } @@ -33,7 +33,7 @@ class EventHandler( @EventHandler fun onPlayerLeave(event: PlayerQuitEvent) { if (!config.logJoinLeave) return - val text = "${TgBot.escapeHTML(event.player.displayName)} " + + val text = "${TgBot.escape(event.player.displayName)} " + "${config.leaveString}." plugin.tgBot.broadcastToTG(text) } @@ -43,7 +43,7 @@ class EventHandler( if (!config.logDeath) return event.deathMessage?.let { val plName = event.entity.displayName - val text = it.replace(plName, "$plName") + val text = it.replace(plName, "$plName") plugin.tgBot.broadcastToTG(text) } } @@ -52,7 +52,7 @@ class EventHandler( fun onPlayerAsleep(event: PlayerBedEnterEvent) { if (!config.logPlayerAsleep) return if (event.bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) return - val text = "${event.player.displayName} fell asleep." + val text = "${event.player.displayName} fell asleep." plugin.tgBot.broadcastToTG(text) } } \ No newline at end of file diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt index 50d53f9..d3ba557 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt @@ -35,7 +35,7 @@ class Plugin : JavaPlugin() { } override fun onDisable() { - if (!config.isEnabled) return + if (!config?.isEnabled) return config.serverStopMessage?.let { tgBot.broadcastToTG(it) } diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt index a6dd235..a95b89d 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt @@ -1,8 +1,6 @@ package org.kraftwerk28.spigot_tg_bridge -import com.github.kotlintelegrambot.Bot -import com.github.kotlintelegrambot.bot -import com.github.kotlintelegrambot.dispatch +import com.github.kotlintelegrambot.* import com.github.kotlintelegrambot.dispatcher.command import com.github.kotlintelegrambot.dispatcher.text import com.github.kotlintelegrambot.entities.BotCommand @@ -10,9 +8,19 @@ import com.github.kotlintelegrambot.entities.ParseMode import com.github.kotlintelegrambot.entities.Update import com.github.kotlintelegrambot.entities.User import okhttp3.logging.HttpLoggingInterceptor -import java.net.InetAddress +import kotlin.reflect.KProperty0 import org.kraftwerk28.spigot_tg_bridge.Constants as C +fun Bot.skipUpdates(lastUpdateID: Long = 0) { + val newUpdates = getUpdates(lastUpdateID) + + if (newUpdates.isNotEmpty()) { + val lastUpd = newUpdates.last() + if (lastUpd !is Update) return + return skipUpdates(lastUpd.updateId + 1) + } +} + class TgBot(private val plugin: Plugin, private val config: Configuration) { private lateinit var bot: Bot @@ -28,16 +36,25 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { bot = bot { token = config.botToken logLevel = HttpLoggingInterceptor.Level.NONE + + val cmdBinding = commands.let { + mapOf( + it.time to ::time, + it.online to ::online, + it.chatID to ::chatID + ) + }.filterKeys { it != null } + dispatch { - command(commands.time.replace(slashRegex, ""), ::time) - command(commands.online.replace(slashRegex, ""), ::online) - command(commands.chatID.replace(slashRegex, ""), ::chatID) + cmdBinding.forEach { text, handler -> + command(text!!, handler as HandleUpdate) + } text(null, ::onText) } } bot.setMyCommands(getBotCommands()) - skipUpdates() - plugin.logger.info("Server address: ${InetAddress.getLocalHost().hostAddress}.") + bot.skipUpdates() + config.webhookConfig?.let { _ -> plugin.logger.info("Running in webhook mode.") } ?: run { @@ -128,7 +145,7 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { config.allowedChats.forEach { chatID -> bot.sendMessage( chatID, - mcMessageStr(username, text), + messageFromMinecraft(username, text), parseMode = ParseMode.HTML ) } @@ -141,32 +158,24 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { plugin.sendMessageToMCFrom(rawUserMention(msg.from!!), msg.text!!) } - private fun mcMessageStr(username: String, text: String): String = - "${escapeHTML(username)}: $text" + private fun messageFromMinecraft(username: String, text: String): String = + "${escape(username)}: $text" private fun rawUserMention(user: User): String = (if (user.firstName.length < 2) null else user.firstName) ?: user.username ?: user.lastName!! - private fun skipUpdates(lastUpdateID: Long = 0) { - val newUpdates = bot.getUpdates(lastUpdateID) - - if (newUpdates.isNotEmpty()) { - val lastUpd = newUpdates.last() - if (lastUpd !is Update) return - return skipUpdates(lastUpd.updateId + 1) - } - } - private fun getBotCommands(): List { - val cmdList = config.commands.run { listOf(time, online, chatID) } + val cmdList = config.commands.run { listOfNotNull(time, online, chatID) } val descList = C.COMMAND_DESC.run { listOf(timeDesc, onlineDesc, chatIDDesc) } return cmdList.zip(descList).map { BotCommand(it.first, it.second) } } companion object { - fun escapeHTML(s: String): String = + fun escapeHTML(s: String) = s.replace("&", "&").replace(">", ">").replace("<", "<") + fun escapeColorCodes(s: String) = s.replace("\u00A7.".toRegex(), "") + fun escape(s: String) = escapeColorCodes(escapeHTML(s)) } } \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 537536b..2b24352 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: SpigotTGBridge -version: 0.0.11 +version: 0.0.12 api-version: '1.15' main: org.kraftwerk28.spigot_tg_bridge.Plugin description: Telegram <-> Minecraft communication plugin for Spigot.