mirror of
https://github.com/amalthea-mc/spigot-tg-bridge.git
synced 2024-11-23 10:52:26 +00:00
add: /chat_id command
This commit is contained in:
parent
e98acd510c
commit
2bc000ca6c
@ -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")!!
|
||||
}
|
||||
}
|
||||
}
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
@ -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:
|
||||
<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) {
|
||||
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<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 {
|
||||
fun escapeHTML(s: String): String =
|
||||
s.replace("&", "&").replace(">", ">").replace("<", "<")
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user