@@ -5,6 +5,7 @@ import com.github.kotlintelegrambot.bot
5
5
import com.github.kotlintelegrambot.dispatch
6
6
import com.github.kotlintelegrambot.dispatcher.command
7
7
import com.github.kotlintelegrambot.dispatcher.text
8
+ import com.github.kotlintelegrambot.entities.BotCommand
8
9
import com.github.kotlintelegrambot.entities.ParseMode
9
10
import com.github.kotlintelegrambot.entities.Update
10
11
import com.github.kotlintelegrambot.entities.User
@@ -30,9 +31,11 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
30
31
dispatch {
31
32
command(commands.time.replace(slashRegex, " " ), ::time)
32
33
command(commands.online.replace(slashRegex, " " ), ::online)
34
+ command(commands.chatID.replace(slashRegex, " " ), ::chatID)
33
35
text(null , ::onText)
34
36
}
35
37
}
38
+ bot.setMyCommands(getBotCommands())
36
39
skipUpdates()
37
40
plugin.logger.info(" Server address: ${InetAddress .getLocalHost().hostAddress} ." )
38
41
config.webhookConfig?.let { _ ->
@@ -48,6 +51,10 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
48
51
49
52
private fun time (bot : Bot , update : Update ) {
50
53
val msg = update.message!!
54
+ if (! config.allowedChats.contains(msg.chat.id)) {
55
+ return
56
+ }
57
+
51
58
if (plugin.server.worlds.isEmpty()) {
52
59
bot.sendMessage(
53
60
msg.chat.id,
@@ -74,6 +81,11 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
74
81
}
75
82
76
83
private fun online (bot : Bot , update : Update ) {
84
+ val msg = update.message!!
85
+ if (! config.allowedChats.contains(msg.chat.id)) {
86
+ return
87
+ }
88
+
77
89
val playerList = plugin.server.onlinePlayers
78
90
val playerStr = plugin.server
79
91
.onlinePlayers
@@ -82,14 +94,30 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
82
94
val text =
83
95
if (playerList.isNotEmpty()) " ${config.onlineString} :\n $playerStr "
84
96
else config.nobodyOnlineString
85
- val msg = update.message!!
86
97
bot.sendMessage(
87
98
msg.chat.id, text,
88
99
replyToMessageId = msg.messageId,
89
100
parseMode = ParseMode .HTML
90
101
)
91
102
}
92
103
104
+ private fun chatID (bot : Bot , update : Update ) {
105
+ val msg = update.message!!
106
+ val chatID = msg.chat.id
107
+ val text = """
108
+ Chat ID:
109
+ <code>$chatID </code>
110
+ paste this id to <code>chats:</code> section in you config.yml file so it will look like this:
111
+ """ .trimIndent() +
112
+ " \n\n <code>chats:\n # other ids...\n - ${chatID} </code>"
113
+ bot.sendMessage(
114
+ chatID,
115
+ text,
116
+ parseMode = ParseMode .HTML ,
117
+ replyToMessageId = msg.messageId
118
+ )
119
+ }
120
+
93
121
fun broadcastToTG (text : String ) {
94
122
config.allowedChats.forEach { chatID ->
95
123
bot.sendMessage(chatID, text, parseMode = ParseMode .HTML )
@@ -131,6 +159,12 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
131
159
}
132
160
}
133
161
162
+ private fun getBotCommands (): List <BotCommand > {
163
+ val cmdList = config.commands.run { listOf (time, online, chatID) }
164
+ val descList = C .COMMAND_DESC .run { listOf (timeDesc, onlineDesc, chatIDDesc) }
165
+ return cmdList.zip(descList).map { BotCommand (it.first, it.second) }
166
+ }
167
+
134
168
companion object {
135
169
fun escapeHTML (s : String ): String =
136
170
s.replace(" &" , " &" ).replace(" >" , " >" ).replace(" <" , " <" )
0 commit comments