Skip to content

Commit 8c7fe22

Browse files
committed
feature: realease 0.0.12
Changelog: - Disableable commands - player nickname escaping
1 parent 441546a commit 8c7fe22

File tree

5 files changed

+45
-36
lines changed

5 files changed

+45
-36
lines changed

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@ package org.kraftwerk28.spigot_tg_bridge
33
import org.bukkit.configuration.file.YamlConfiguration
44

55
class Commands(yamlCfg: YamlConfiguration) {
6-
val time: String
7-
val online: String
8-
val chatID: String
6+
val time: String?
7+
val online: String?
8+
val chatID: String?
99

1010
init {
1111
yamlCfg.run {
12-
time = getString("commands.time", "time")!!
13-
online = getString("commands.online", "online")!!
14-
chatID = getString("commands.chat_id", "chat_id")!!
12+
time = getString("commands.time")
13+
online = getString("commands.online")
14+
chatID = getString("commands.chat_id")
1515
}
1616
}
1717
}

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/EventHandler.kt

+4-4
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ class EventHandler(
2525
@EventHandler
2626
fun onPlayerJoin(event: PlayerJoinEvent) {
2727
if (!config.logJoinLeave) return
28-
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> " +
28+
val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
2929
"${config.joinString}."
3030
plugin.tgBot.broadcastToTG(text)
3131
}
3232

3333
@EventHandler
3434
fun onPlayerLeave(event: PlayerQuitEvent) {
3535
if (!config.logJoinLeave) return
36-
val text = "<b>${TgBot.escapeHTML(event.player.displayName)}</b> " +
36+
val text = "<i>${TgBot.escape(event.player.displayName)}</i> " +
3737
"${config.leaveString}."
3838
plugin.tgBot.broadcastToTG(text)
3939
}
@@ -43,7 +43,7 @@ class EventHandler(
4343
if (!config.logDeath) return
4444
event.deathMessage?.let {
4545
val plName = event.entity.displayName
46-
val text = it.replace(plName, "<b>$plName</b>")
46+
val text = it.replace(plName, "<i>$plName</i>")
4747
plugin.tgBot.broadcastToTG(text)
4848
}
4949
}
@@ -52,7 +52,7 @@ class EventHandler(
5252
fun onPlayerAsleep(event: PlayerBedEnterEvent) {
5353
if (!config.logPlayerAsleep) return
5454
if (event.bedEnterResult != PlayerBedEnterEvent.BedEnterResult.OK) return
55-
val text = "<b>${event.player.displayName}</b> fell asleep."
55+
val text = "<i>${event.player.displayName}</i> fell asleep."
5656
plugin.tgBot.broadcastToTG(text)
5757
}
5858
}

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class Plugin : JavaPlugin() {
3535
}
3636

3737
override fun onDisable() {
38-
if (!config.isEnabled) return
38+
if (!config?.isEnabled) return
3939
config.serverStopMessage?.let {
4040
tgBot.broadcastToTG(it)
4141
}

src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt

+33-24
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,26 @@
11
package org.kraftwerk28.spigot_tg_bridge
22

3-
import com.github.kotlintelegrambot.Bot
4-
import com.github.kotlintelegrambot.bot
5-
import com.github.kotlintelegrambot.dispatch
3+
import com.github.kotlintelegrambot.*
64
import com.github.kotlintelegrambot.dispatcher.command
75
import com.github.kotlintelegrambot.dispatcher.text
86
import com.github.kotlintelegrambot.entities.BotCommand
97
import com.github.kotlintelegrambot.entities.ParseMode
108
import com.github.kotlintelegrambot.entities.Update
119
import com.github.kotlintelegrambot.entities.User
1210
import okhttp3.logging.HttpLoggingInterceptor
13-
import java.net.InetAddress
11+
import kotlin.reflect.KProperty0
1412
import org.kraftwerk28.spigot_tg_bridge.Constants as C
1513

14+
fun Bot.skipUpdates(lastUpdateID: Long = 0) {
15+
val newUpdates = getUpdates(lastUpdateID)
16+
17+
if (newUpdates.isNotEmpty()) {
18+
val lastUpd = newUpdates.last()
19+
if (lastUpd !is Update) return
20+
return skipUpdates(lastUpd.updateId + 1)
21+
}
22+
}
23+
1624
class TgBot(private val plugin: Plugin, private val config: Configuration) {
1725

1826
private lateinit var bot: Bot
@@ -28,16 +36,25 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
2836
bot = bot {
2937
token = config.botToken
3038
logLevel = HttpLoggingInterceptor.Level.NONE
39+
40+
val cmdBinding = commands.let {
41+
mapOf(
42+
it.time to ::time,
43+
it.online to ::online,
44+
it.chatID to ::chatID
45+
)
46+
}.filterKeys { it != null }
47+
3148
dispatch {
32-
command(commands.time.replace(slashRegex, ""), ::time)
33-
command(commands.online.replace(slashRegex, ""), ::online)
34-
command(commands.chatID.replace(slashRegex, ""), ::chatID)
49+
cmdBinding.forEach { text, handler ->
50+
command(text!!, handler as HandleUpdate)
51+
}
3552
text(null, ::onText)
3653
}
3754
}
3855
bot.setMyCommands(getBotCommands())
39-
skipUpdates()
40-
plugin.logger.info("Server address: ${InetAddress.getLocalHost().hostAddress}.")
56+
bot.skipUpdates()
57+
4158
config.webhookConfig?.let { _ ->
4259
plugin.logger.info("Running in webhook mode.")
4360
} ?: run {
@@ -128,7 +145,7 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
128145
config.allowedChats.forEach { chatID ->
129146
bot.sendMessage(
130147
chatID,
131-
mcMessageStr(username, text),
148+
messageFromMinecraft(username, text),
132149
parseMode = ParseMode.HTML
133150
)
134151
}
@@ -141,32 +158,24 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
141158
plugin.sendMessageToMCFrom(rawUserMention(msg.from!!), msg.text!!)
142159
}
143160

144-
private fun mcMessageStr(username: String, text: String): String =
145-
"<b>${escapeHTML(username)}</b>: $text"
161+
private fun messageFromMinecraft(username: String, text: String): String =
162+
"<i>${escape(username)}</i>: $text"
146163

147164
private fun rawUserMention(user: User): String =
148165
(if (user.firstName.length < 2) null else user.firstName)
149166
?: user.username
150167
?: user.lastName!!
151168

152-
private fun skipUpdates(lastUpdateID: Long = 0) {
153-
val newUpdates = bot.getUpdates(lastUpdateID)
154-
155-
if (newUpdates.isNotEmpty()) {
156-
val lastUpd = newUpdates.last()
157-
if (lastUpd !is Update) return
158-
return skipUpdates(lastUpd.updateId + 1)
159-
}
160-
}
161-
162169
private fun getBotCommands(): List<BotCommand> {
163-
val cmdList = config.commands.run { listOf(time, online, chatID) }
170+
val cmdList = config.commands.run { listOfNotNull(time, online, chatID) }
164171
val descList = C.COMMAND_DESC.run { listOf(timeDesc, onlineDesc, chatIDDesc) }
165172
return cmdList.zip(descList).map { BotCommand(it.first, it.second) }
166173
}
167174

168175
companion object {
169-
fun escapeHTML(s: String): String =
176+
fun escapeHTML(s: String) =
170177
s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")
178+
fun escapeColorCodes(s: String) = s.replace("\u00A7.".toRegex(), "")
179+
fun escape(s: String) = escapeColorCodes(escapeHTML(s))
171180
}
172181
}

src/main/resources/plugin.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: SpigotTGBridge
2-
version: 0.0.11
2+
version: 0.0.12
33
api-version: '1.15'
44
main: org.kraftwerk28.spigot_tg_bridge.Plugin
55
description: Telegram <-> Minecraft communication plugin for Spigot.

0 commit comments

Comments
 (0)