Skip to content

Commit 2bc000c

Browse files
committed
add: /chat_id command
1 parent e98acd5 commit 2bc000c

File tree

4 files changed

+44
-2
lines changed

4 files changed

+44
-2
lines changed

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

+3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import org.bukkit.configuration.file.YamlConfiguration
55
class Commands(yamlCfg: YamlConfiguration) {
66
val time: String
77
val online: String
8+
val chatID: String
9+
810
init {
911
yamlCfg.run {
1012
time = getString("commands.time", "time")!!
1113
online = getString("commands.online", "online")!!
14+
chatID = getString("commands.chat_id", "chat_id")!!
1215
}
1316
}
1417
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,9 @@ object Constants {
2222
object COMMANDS {
2323
const val PLUGIN_RELOAD = "tgbridge_reload"
2424
}
25+
object COMMAND_DESC {
26+
const val timeDesc = "Get time on server"
27+
const val onlineDesc = "Get players online"
28+
const val chatIDDesc = "Get current chat id"
29+
}
2530
}

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

+35-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.github.kotlintelegrambot.bot
55
import com.github.kotlintelegrambot.dispatch
66
import com.github.kotlintelegrambot.dispatcher.command
77
import com.github.kotlintelegrambot.dispatcher.text
8+
import com.github.kotlintelegrambot.entities.BotCommand
89
import com.github.kotlintelegrambot.entities.ParseMode
910
import com.github.kotlintelegrambot.entities.Update
1011
import com.github.kotlintelegrambot.entities.User
@@ -30,9 +31,11 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
3031
dispatch {
3132
command(commands.time.replace(slashRegex, ""), ::time)
3233
command(commands.online.replace(slashRegex, ""), ::online)
34+
command(commands.chatID.replace(slashRegex, ""), ::chatID)
3335
text(null, ::onText)
3436
}
3537
}
38+
bot.setMyCommands(getBotCommands())
3639
skipUpdates()
3740
plugin.logger.info("Server address: ${InetAddress.getLocalHost().hostAddress}.")
3841
config.webhookConfig?.let { _ ->
@@ -48,6 +51,10 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
4851

4952
private fun time(bot: Bot, update: Update) {
5053
val msg = update.message!!
54+
if (!config.allowedChats.contains(msg.chat.id)) {
55+
return
56+
}
57+
5158
if (plugin.server.worlds.isEmpty()) {
5259
bot.sendMessage(
5360
msg.chat.id,
@@ -74,6 +81,11 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
7481
}
7582

7683
private fun online(bot: Bot, update: Update) {
84+
val msg = update.message!!
85+
if (!config.allowedChats.contains(msg.chat.id)) {
86+
return
87+
}
88+
7789
val playerList = plugin.server.onlinePlayers
7890
val playerStr = plugin.server
7991
.onlinePlayers
@@ -82,14 +94,30 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
8294
val text =
8395
if (playerList.isNotEmpty()) "${config.onlineString}:\n$playerStr"
8496
else config.nobodyOnlineString
85-
val msg = update.message!!
8697
bot.sendMessage(
8798
msg.chat.id, text,
8899
replyToMessageId = msg.messageId,
89100
parseMode = ParseMode.HTML
90101
)
91102
}
92103

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+
93121
fun broadcastToTG(text: String) {
94122
config.allowedChats.forEach { chatID ->
95123
bot.sendMessage(chatID, text, parseMode = ParseMode.HTML)
@@ -131,6 +159,12 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
131159
}
132160
}
133161

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+
134168
companion object {
135169
fun escapeHTML(s: String): String =
136170
s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")

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.10
2+
version: 0.0.11
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)