feature: realease 0.0.12

Changelog:
  - Disableable commands
  - player nickname escaping
This commit is contained in:
kraftwerk28 2020-07-24 09:43:45 +03:00
parent 441546a3e9
commit 8c7fe22e3d
5 changed files with 45 additions and 36 deletions

View File

@ -3,15 +3,15 @@ package org.kraftwerk28.spigot_tg_bridge
import org.bukkit.configuration.file.YamlConfiguration import org.bukkit.configuration.file.YamlConfiguration
class Commands(yamlCfg: YamlConfiguration) { class Commands(yamlCfg: YamlConfiguration) {
val time: String val time: String?
val online: String val online: String?
val chatID: String val chatID: String?
init { init {
yamlCfg.run { yamlCfg.run {
time = getString("commands.time", "time")!! time = getString("commands.time")
online = getString("commands.online", "online")!! online = getString("commands.online")
chatID = getString("commands.chat_id", "chat_id")!! chatID = getString("commands.chat_id")
} }
} }
} }

View File

@ -25,7 +25,7 @@ class EventHandler(
@EventHandler @EventHandler
fun onPlayerJoin(event: PlayerJoinEvent) { fun onPlayerJoin(event: PlayerJoinEvent) {
if (!config.logJoinLeave) return if (!config.logJoinLeave) return
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> " + val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
"${config.joinString}." "${config.joinString}."
plugin.tgBot.broadcastToTG(text) plugin.tgBot.broadcastToTG(text)
} }
@ -33,7 +33,7 @@ class EventHandler(
@EventHandler @EventHandler
fun onPlayerLeave(event: PlayerQuitEvent) { fun onPlayerLeave(event: PlayerQuitEvent) {
if (!config.logJoinLeave) return if (!config.logJoinLeave) return
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> " + val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
"${config.leaveString}." "${config.leaveString}."
plugin.tgBot.broadcastToTG(text) plugin.tgBot.broadcastToTG(text)
} }
@ -43,7 +43,7 @@ class EventHandler(
if (!config.logDeath) return if (!config.logDeath) return
event.deathMessage?.let { event.deathMessage?.let {
val plName = event.entity.displayName val plName = event.entity.displayName
val text = it.replace(plName, "<b>$plName</b>") val text = it.replace(plName, "<i>$plName</i>")
plugin.tgBot.broadcastToTG(text) plugin.tgBot.broadcastToTG(text)
} }
} }
@ -52,7 +52,7 @@ class EventHandler(
fun onPlayerAsleep(event: PlayerBedEnterEvent) { fun onPlayerAsleep(event: PlayerBedEnterEvent) {
if (!config.logPlayerAsleep) return if (!config.logPlayerAsleep) return
if (event.bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) return if (event.bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) return
val text = "<b>${event.player.displayName}</b> fell asleep." val text = "<i>${event.player.displayName}</i> fell asleep."
plugin.tgBot.broadcastToTG(text) plugin.tgBot.broadcastToTG(text)
} }
} }

View File

@ -35,7 +35,7 @@ class Plugin : JavaPlugin() {
} }
override fun onDisable() { override fun onDisable() {
if (!config.isEnabled) return if (!config?.isEnabled) return
config.serverStopMessage?.let { config.serverStopMessage?.let {
tgBot.broadcastToTG(it) tgBot.broadcastToTG(it)
} }

View File

@ -1,8 +1,6 @@
package org.kraftwerk28.spigot_tg_bridge package org.kraftwerk28.spigot_tg_bridge
import com.github.kotlintelegrambot.Bot import com.github.kotlintelegrambot.*
import com.github.kotlintelegrambot.bot
import com.github.kotlintelegrambot.dispatch
import com.github.kotlintelegrambot.dispatcher.command import com.github.kotlintelegrambot.dispatcher.command
import com.github.kotlintelegrambot.dispatcher.text import com.github.kotlintelegrambot.dispatcher.text
import com.github.kotlintelegrambot.entities.BotCommand 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.Update
import com.github.kotlintelegrambot.entities.User import com.github.kotlintelegrambot.entities.User
import okhttp3.logging.HttpLoggingInterceptor import okhttp3.logging.HttpLoggingInterceptor
import java.net.InetAddress import kotlin.reflect.KProperty0
import org.kraftwerk28.spigot_tg_bridge.Constants as C 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) { class TgBot(private val plugin: Plugin, private val config: Configuration) {
private lateinit var bot: Bot private lateinit var bot: Bot
@ -28,16 +36,25 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
bot = bot { bot = bot {
token = config.botToken token = config.botToken
logLevel = HttpLoggingInterceptor.Level.NONE 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 { dispatch {
command(commands.time.replace(slashRegex, ""), ::time) cmdBinding.forEach { text, handler ->
command(commands.online.replace(slashRegex, ""), ::online) command(text!!, handler as HandleUpdate)
command(commands.chatID.replace(slashRegex, ""), ::chatID) }
text(null, ::onText) text(null, ::onText)
} }
} }
bot.setMyCommands(getBotCommands()) bot.setMyCommands(getBotCommands())
skipUpdates() bot.skipUpdates()
plugin.logger.info("Server address: ${InetAddress.getLocalHost().hostAddress}.")
config.webhookConfig?.let { _ -> config.webhookConfig?.let { _ ->
plugin.logger.info("Running in webhook mode.") plugin.logger.info("Running in webhook mode.")
} ?: run { } ?: run {
@ -128,7 +145,7 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
config.allowedChats.forEach { chatID -> config.allowedChats.forEach { chatID ->
bot.sendMessage( bot.sendMessage(
chatID, chatID,
mcMessageStr(username, text), messageFromMinecraft(username, text),
parseMode = ParseMode.HTML parseMode = ParseMode.HTML
) )
} }
@ -141,32 +158,24 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
plugin.sendMessageToMCFrom(rawUserMention(msg.from!!), msg.text!!) plugin.sendMessageToMCFrom(rawUserMention(msg.from!!), msg.text!!)
} }
private fun mcMessageStr(username: String, text: String): String = private fun messageFromMinecraft(username: String, text: String): String =
"<b>${escapeHTML(username)}</b>: $text" "<i>${escape(username)}</i>: $text"
private fun rawUserMention(user: User): String = private fun rawUserMention(user: User): String =
(if (user.firstName.length < 2) null else user.firstName) (if (user.firstName.length < 2) null else user.firstName)
?: user.username ?: user.username
?: user.lastName!! ?: 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<BotCommand> { private fun getBotCommands(): List<BotCommand> {
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) } val descList = C.COMMAND_DESC.run { listOf(timeDesc, onlineDesc, chatIDDesc) }
return cmdList.zip(descList).map { BotCommand(it.first, it.second) } return cmdList.zip(descList).map { BotCommand(it.first, it.second) }
} }
companion object { companion object {
fun escapeHTML(s: String): String = fun escapeHTML(s: String) =
s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;") s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
fun escapeColorCodes(s: String) = s.replace("\u00A7.".toRegex(), "")
fun escape(s: String) = escapeColorCodes(escapeHTML(s))
} }
} }

View File

@ -1,5 +1,5 @@
name: SpigotTGBridge name: SpigotTGBridge
version: 0.0.11 version: 0.0.12
api-version: '1.15' api-version: '1.15'
main: org.kraftwerk28.spigot_tg_bridge.Plugin main: org.kraftwerk28.spigot_tg_bridge.Plugin
description: Telegram <-> Minecraft communication plugin for Spigot. description: Telegram <-> Minecraft communication plugin for Spigot.