add: /chat_id command

This commit is contained in:
kraftwerk28 2020-07-20 19:39:08 +03:00
parent e98acd510c
commit 2bc000ca6c
4 changed files with 44 additions and 2 deletions

View File

@ -5,10 +5,13 @@ import org.bukkit.configuration.file.YamlConfiguration
class Commands(yamlCfg: YamlConfiguration) { class Commands(yamlCfg: YamlConfiguration) {
val time: String val time: String
val online: String val online: String
val chatID: String
init { init {
yamlCfg.run { yamlCfg.run {
time = getString("commands.time", "time")!! time = getString("commands.time", "time")!!
online = getString("commands.online", "online")!! online = getString("commands.online", "online")!!
chatID = getString("commands.chat_id", "chat_id")!!
} }
} }
} }

View File

@ -22,4 +22,9 @@ object Constants {
object COMMANDS { object COMMANDS {
const val PLUGIN_RELOAD = "tgbridge_reload" 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"
}
} }

View File

@ -5,6 +5,7 @@ import com.github.kotlintelegrambot.bot
import com.github.kotlintelegrambot.dispatch import com.github.kotlintelegrambot.dispatch
import com.github.kotlintelegrambot.dispatcher.command import com.github.kotlintelegrambot.dispatcher.command
import com.github.kotlintelegrambot.dispatcher.text import com.github.kotlintelegrambot.dispatcher.text
import com.github.kotlintelegrambot.entities.BotCommand
import com.github.kotlintelegrambot.entities.ParseMode import com.github.kotlintelegrambot.entities.ParseMode
import com.github.kotlintelegrambot.entities.Update import com.github.kotlintelegrambot.entities.Update
import com.github.kotlintelegrambot.entities.User import com.github.kotlintelegrambot.entities.User
@ -30,9 +31,11 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
dispatch { dispatch {
command(commands.time.replace(slashRegex, ""), ::time) command(commands.time.replace(slashRegex, ""), ::time)
command(commands.online.replace(slashRegex, ""), ::online) command(commands.online.replace(slashRegex, ""), ::online)
command(commands.chatID.replace(slashRegex, ""), ::chatID)
text(null, ::onText) text(null, ::onText)
} }
} }
bot.setMyCommands(getBotCommands())
skipUpdates() skipUpdates()
plugin.logger.info("Server address: ${InetAddress.getLocalHost().hostAddress}.") plugin.logger.info("Server address: ${InetAddress.getLocalHost().hostAddress}.")
config.webhookConfig?.let { _ -> config.webhookConfig?.let { _ ->
@ -48,6 +51,10 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
private fun time(bot: Bot, update: Update) { private fun time(bot: Bot, update: Update) {
val msg = update.message!! val msg = update.message!!
if (!config.allowedChats.contains(msg.chat.id)) {
return
}
if (plugin.server.worlds.isEmpty()) { if (plugin.server.worlds.isEmpty()) {
bot.sendMessage( bot.sendMessage(
msg.chat.id, msg.chat.id,
@ -74,6 +81,11 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
} }
private fun online(bot: Bot, update: Update) { 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 playerList = plugin.server.onlinePlayers
val playerStr = plugin.server val playerStr = plugin.server
.onlinePlayers .onlinePlayers
@ -82,7 +94,6 @@ class TgBot(private val plugin: Plugin, private val config: Configuration) {
val text = val text =
if (playerList.isNotEmpty()) "${config.onlineString}:\n$playerStr" if (playerList.isNotEmpty()) "${config.onlineString}:\n$playerStr"
else config.nobodyOnlineString else config.nobodyOnlineString
val msg = update.message!!
bot.sendMessage( bot.sendMessage(
msg.chat.id, text, msg.chat.id, text,
replyToMessageId = msg.messageId, 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:
<code>$chatID</code>
paste this id to <code>chats:</code> section in you config.yml file so it will look like this:
""".trimIndent() +
"\n\n<code>chats:\n # other ids...\n - ${chatID}</code>"
bot.sendMessage(
chatID,
text,
parseMode = ParseMode.HTML,
replyToMessageId = msg.messageId
)
}
fun broadcastToTG(text: String) { fun broadcastToTG(text: String) {
config.allowedChats.forEach { chatID -> config.allowedChats.forEach { chatID ->
bot.sendMessage(chatID, text, parseMode = ParseMode.HTML) 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<BotCommand> {
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 { companion object {
fun escapeHTML(s: String): String = fun escapeHTML(s: String): String =
s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;") s.replace("&", "&amp;").replace(">", "&gt;").replace("<", "&lt;")

View File

@ -1,5 +1,5 @@
name: SpigotTGBridge name: SpigotTGBridge
version: 0.0.10 version: 0.0.11
api-version: '1.15' api-version: '1.15'
main: org.kraftwerk28.spigot_tg_bridge.Plugin main: org.kraftwerk28.spigot_tg_bridge.Plugin
description: Telegram <-> Minecraft communication plugin for Spigot. description: Telegram <-> Minecraft communication plugin for Spigot.