1
1
package org.kraftwerk28.spigot_tg_bridge
2
2
3
- import com.github.kotlintelegrambot.Bot
4
- import com.github.kotlintelegrambot.bot
5
- import com.github.kotlintelegrambot.dispatch
3
+ import com.github.kotlintelegrambot.*
6
4
import com.github.kotlintelegrambot.dispatcher.command
7
5
import com.github.kotlintelegrambot.dispatcher.text
8
6
import com.github.kotlintelegrambot.entities.BotCommand
9
7
import com.github.kotlintelegrambot.entities.ParseMode
10
8
import com.github.kotlintelegrambot.entities.Update
11
9
import com.github.kotlintelegrambot.entities.User
12
10
import okhttp3.logging.HttpLoggingInterceptor
13
- import java.net.InetAddress
11
+ import kotlin.reflect.KProperty0
14
12
import org.kraftwerk28.spigot_tg_bridge.Constants as C
15
13
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
+
16
24
class TgBot (private val plugin : Plugin , private val config : Configuration ) {
17
25
18
26
private lateinit var bot: Bot
@@ -28,16 +36,25 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
28
36
bot = bot {
29
37
token = config.botToken
30
38
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
+
31
48
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
+ }
35
52
text(null , ::onText)
36
53
}
37
54
}
38
55
bot.setMyCommands(getBotCommands())
39
- skipUpdates()
40
- plugin.logger.info( " Server address: ${ InetAddress .getLocalHost().hostAddress} . " )
56
+ bot. skipUpdates()
57
+
41
58
config.webhookConfig?.let { _ ->
42
59
plugin.logger.info(" Running in webhook mode." )
43
60
} ? : run {
@@ -128,7 +145,7 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
128
145
config.allowedChats.forEach { chatID ->
129
146
bot.sendMessage(
130
147
chatID,
131
- mcMessageStr (username, text),
148
+ messageFromMinecraft (username, text),
132
149
parseMode = ParseMode .HTML
133
150
)
134
151
}
@@ -141,32 +158,24 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
141
158
plugin.sendMessageToMCFrom(rawUserMention(msg.from!! ), msg.text!! )
142
159
}
143
160
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 "
146
163
147
164
private fun rawUserMention (user : User ): String =
148
165
(if (user.firstName.length < 2 ) null else user.firstName)
149
166
? : user.username
150
167
? : user.lastName!!
151
168
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
-
162
169
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) }
164
171
val descList = C .COMMAND_DESC .run { listOf (timeDesc, onlineDesc, chatIDDesc) }
165
172
return cmdList.zip(descList).map { BotCommand (it.first, it.second) }
166
173
}
167
174
168
175
companion object {
169
- fun escapeHTML (s : String ): String =
176
+ fun escapeHTML (s : String ) =
170
177
s.replace(" &" , " &" ).replace(" >" , " >" ).replace(" <" , " <" )
178
+ fun escapeColorCodes (s : String ) = s.replace(" \u00A7 ." .toRegex(), " " )
179
+ fun escape (s : String ) = escapeColorCodes(escapeHTML(s))
171
180
}
172
181
}
0 commit comments