From 2bc000ca6c7c2b3ef61c7ebc5064d9a56fa4c398 Mon Sep 17 00:00:00 2001 From: kraftwerk28 Date: Mon, 20 Jul 2020 19:39:08 +0300 Subject: [PATCH] add: /chat_id command --- .../spigot_tg_bridge/BotCommands.kt | 3 ++ .../kraftwerk28/spigot_tg_bridge/Constants.kt | 5 +++ .../org/kraftwerk28/spigot_tg_bridge/TgBot.kt | 36 ++++++++++++++++++- src/main/resources/plugin.yml | 2 +- 4 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt index de8b700..bdcd498 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/BotCommands.kt @@ -5,10 +5,13 @@ import org.bukkit.configuration.file.YamlConfiguration class Commands(yamlCfg: YamlConfiguration) { 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")!! } } } \ No newline at end of file diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt index 371c746..31b310c 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt @@ -22,4 +22,9 @@ object Constants { object COMMANDS { const val PLUGIN_RELOAD = "tgbridge_reload" } + object COMMAND_DESC { + const val timeDesc = "Get time on server" + const val onlineDesc = "Get players online" + const val chatIDDesc = "Get current chat id" + } } diff --git a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt index 9ef41c3..a6dd235 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt @@ -5,6 +5,7 @@ import com.github.kotlintelegrambot.bot import com.github.kotlintelegrambot.dispatch import com.github.kotlintelegrambot.dispatcher.command import com.github.kotlintelegrambot.dispatcher.text +import com.github.kotlintelegrambot.entities.BotCommand import com.github.kotlintelegrambot.entities.ParseMode import com.github.kotlintelegrambot.entities.Update import com.github.kotlintelegrambot.entities.User @@ -30,9 +31,11 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { dispatch { command(commands.time.replace(slashRegex, ""), ::time) command(commands.online.replace(slashRegex, ""), ::online) + command(commands.chatID.replace(slashRegex, ""), ::chatID) text(null, ::onText) } } + bot.setMyCommands(getBotCommands()) skipUpdates() plugin.logger.info("Server address: ${InetAddress.getLocalHost().hostAddress}.") config.webhookConfig?.let { _ -> @@ -48,6 +51,10 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { private fun time(bot: Bot, update: Update) { val msg = update.message!! + if (!config.allowedChats.contains(msg.chat.id)) { + return + } + if (plugin.server.worlds.isEmpty()) { bot.sendMessage( msg.chat.id, @@ -74,6 +81,11 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { } private fun online(bot: Bot, update: Update) { + val msg = update.message!! + if (!config.allowedChats.contains(msg.chat.id)) { + return + } + val playerList = plugin.server.onlinePlayers val playerStr = plugin.server .onlinePlayers @@ -82,7 +94,6 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { val text = if (playerList.isNotEmpty()) "${config.onlineString}:\n$playerStr" else config.nobodyOnlineString - val msg = update.message!! bot.sendMessage( msg.chat.id, text, replyToMessageId = msg.messageId, @@ -90,6 +101,23 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { ) } + private fun chatID(bot: Bot, update: Update) { + val msg = update.message!! + val chatID = msg.chat.id + val text = """ + Chat ID: + $chatID + paste this id to chats: section in you config.yml file so it will look like this: + """.trimIndent() + + "\n\nchats:\n # other ids...\n - ${chatID}" + bot.sendMessage( + chatID, + text, + parseMode = ParseMode.HTML, + replyToMessageId = msg.messageId + ) + } + fun broadcastToTG(text: String) { config.allowedChats.forEach { chatID -> bot.sendMessage(chatID, text, parseMode = ParseMode.HTML) @@ -131,6 +159,12 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) { } } + private fun getBotCommands(): List { + val cmdList = config.commands.run { listOf(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 = s.replace("&", "&").replace(">", ">").replace("<", "<") diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 46aba92..537536b 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: SpigotTGBridge -version: 0.0.10 +version: 0.0.11 api-version: '1.15' main: org.kraftwerk28.spigot_tg_bridge.Plugin description: Telegram <-> Minecraft communication plugin for Spigot.