mirror of
https://github.com/amalthea-mc/spigot-tg-bridge.git
synced 2024-11-08 19:51:04 +00:00
feature: realease 0.0.12
Changelog: - Disableable commands - player nickname escaping
This commit is contained in:
parent
441546a3e9
commit
8c7fe22e3d
@ -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")
|
||||
}
|
||||
}
|
||||
}
|
@ -25,7 +25,7 @@ class EventHandler(
|
||||
@EventHandler
|
||||
fun onPlayerJoin(event: PlayerJoinEvent) {
|
||||
if (!config.logJoinLeave) return
|
||||
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> " +
|
||||
val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
|
||||
"${config.joinString}."
|
||||
plugin.tgBot.broadcastToTG(text)
|
||||
}
|
||||
@ -33,7 +33,7 @@ class EventHandler(
|
||||
@EventHandler
|
||||
fun onPlayerLeave(event: PlayerQuitEvent) {
|
||||
if (!config.logJoinLeave) return
|
||||
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> " +
|
||||
val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
|
||||
"${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, "<b>$plName</b>")
|
||||
val text = it.replace(plName, "<i>$plName</i>")
|
||||
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 = "<b>${event.player.displayName}</b> fell asleep."
|
||||
val text = "<i>${event.player.displayName}</i> fell asleep."
|
||||
plugin.tgBot.broadcastToTG(text)
|
||||
}
|
||||
}
|
@ -35,7 +35,7 @@ class Plugin : JavaPlugin() {
|
||||
}
|
||||
|
||||
override fun onDisable() {
|
||||
if (!config.isEnabled) return
|
||||
if (!config?.isEnabled) return
|
||||
config.serverStopMessage?.let {
|
||||
tgBot.broadcastToTG(it)
|
||||
}
|
||||
|
@ -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 =
|
||||
"<b>${escapeHTML(username)}</b>: $text"
|
||||
private fun messageFromMinecraft(username: String, text: String): String =
|
||||
"<i>${escape(username)}</i>: $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<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) }
|
||||
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))
|
||||
}
|
||||
}
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user