mirror of
https://github.com/amalthea-mc/spigot-tg-bridge.git
synced 2024-11-23 10:52:26 +00:00
feature: release 0.0.9
Changelog: - message color format - plugin enable flag
This commit is contained in:
parent
6cffdf4b33
commit
3afb144104
@ -31,5 +31,10 @@
|
||||
<option name="name" value="maven2" />
|
||||
<option name="url" value="https://jitpack.io" />
|
||||
</remote-repository>
|
||||
<remote-repository>
|
||||
<option name="id" value="BintrayJCenter" />
|
||||
<option name="name" value="BintrayJCenter" />
|
||||
<option name="url" value="https://jcenter.bintray.com/" />
|
||||
</remote-repository>
|
||||
</component>
|
||||
</project>
|
30
README.md
30
README.md
@ -20,18 +20,20 @@
|
||||
#### config.yml:
|
||||
| Field | Description | Type | Required | Default |
|
||||
|:-----:|:------------|:----:|:--------:|:-------:|
|
||||
| botToken | Telegram bot token ([How to create bot](https://core.telegram.org/bots#3-how-do-i-create-a-bot)) | string | :heavy_check_mark: | - |
|
||||
| enable | If plugin should be enabled | `boolean` | :x: | `true` |
|
||||
| botToken | Telegram bot token ([How to create bot](https://core.telegram.org/bots#3-how-do-i-create-a-bot)) | `string` | :heavy_check_mark: | - |
|
||||
| botUsername | Telegram bot username | string | :heavy_check_mark: | - |
|
||||
| chats | Chats, where bot will work (to prevent using bot by unknown chats) | number[] | :heavy_check_mark: | [] |
|
||||
| serverStartMessage | What will be sent to chats when server starts | string | :x: | 'Server started.' |
|
||||
| serverStopMessage | What will be sent to chats when server stops | string | :x: | 'Server stopped.' |
|
||||
| logJoinLeave | If true, plugin will send corresponding messages to chats, when player joins or leaves server | boolean | :x: | true |
|
||||
| logFromMCtoTG | If true, plugin will send messages from players on server, to Telegram chats | boolean | :x: | true |
|
||||
| logFromTGtoMC | If true, plugin will send messages from chats, to Minecraft server | boolean | :x: | true |
|
||||
| logPlayerDeath | If true, plugin will send message to Telegram if player died | boolean | :x: | false |
|
||||
| logPlayerAsleep | If true, plugin will send message to Telegram if player fell asleep | boolean | :x: | false |
|
||||
| strings | Dictionary of tokens - strings for plugin i18n | Map<string, string> | :x: | See default config |
|
||||
| commands | Dictionary of command text used in Telegram bot | Map<string, string> | :x: | See default config |
|
||||
| chats | Chats, where bot will work (to prevent using bot by unknown chats) | `number[] or string[]` | :heavy_check_mark: | `[]` |
|
||||
| serverStartMessage | What will be sent to chats when server starts | `string` | :x: | `'Server started.'` |
|
||||
| serverStopMessage | What will be sent to chats when server stops | `string` | :x: | `'Server stopped.'` |
|
||||
| logJoinLeave | If true, plugin will send corresponding messages to chats, when player joins or leaves server | `boolean` | :x: | `true` |
|
||||
| logFromMCtoTG | If true, plugin will send messages from players on server, to Telegram chats | `boolean` | :x: | `true` |
|
||||
| logFromTGtoMC | If true, plugin will send messages from chats, to Minecraft server | `boolean` | :x: | `true` |
|
||||
| logPlayerDeath | If true, plugin will send message to Telegram if player died | `boolean` | :x: | `false` |
|
||||
| logPlayerAsleep | If true, plugin will send message to Telegram if player fell asleep | `boolean` | :x: | `false` |
|
||||
| strings | Dictionary of tokens - strings for plugin i18n | `Map<string, string>` | :x: | See default config |
|
||||
| commands | Dictionary of command text used in Telegram bot | `Map<string, string>` | :x: | See default config |
|
||||
| telegramMessageFormat | Format string for TGtoMC chat message | `string` | :x: | See default config |
|
||||
|
||||
## Commands:
|
||||
|
||||
@ -41,3 +43,9 @@ Commands are customizeable through config, but there are default values for them
|
||||
|:-------:|:------------|
|
||||
| `/online` | Get players, currently online |
|
||||
| `/time` | Get [time](https://minecraft.gamepedia.com/Day-night_cycle) on server |
|
||||
|
||||
## Format string:
|
||||
Must contain `%username%` and `%message` inside.
|
||||
You can customize message color with it. See [message color codes](https://www.digminecraft.com/lists/color_list_pc.php).
|
||||
|
||||
P. S.: related to [this issue](https://github.com/kraftwerk28/spigot-tg-bridge/issues/6)
|
@ -32,6 +32,7 @@ repositories {
|
||||
url "https://oss.sonatype.org/content/repositories/snapshots/"
|
||||
}
|
||||
mavenCentral()
|
||||
jcenter()
|
||||
}
|
||||
|
||||
def plugDir = "MinecraftServers/spigot_1.15.2/plugins/"
|
||||
@ -51,6 +52,8 @@ dependencies {
|
||||
|
||||
def tgBotVer = '5.0.0'
|
||||
implementation "io.github.kotlin-telegram-bot.kotlin-telegram-bot:telegram:$tgBotVer"
|
||||
// def ktorVersion = '1.3.2'
|
||||
// implementation "io.ktor:ktor-server-core:$ktorVersion"
|
||||
}
|
||||
|
||||
compileKotlin {
|
||||
|
@ -4,6 +4,7 @@ object Constants {
|
||||
const val configFilename = "config.yml"
|
||||
// Config field names
|
||||
object FIELDS {
|
||||
const val ENABLE = "enable"
|
||||
const val BOT_TOKEN = "botToken"
|
||||
const val BOT_USERNAME = "botUsername"
|
||||
const val ALLOWED_CHATS = "chats"
|
||||
@ -14,6 +15,8 @@ object Constants {
|
||||
const val LOG_JOIN_LEAVE = "logJoinLeave"
|
||||
const val LOG_PLAYER_DEATH = "logPlayerDeath"
|
||||
const val LOG_PLAYER_ASLEEP = "logPlayerAsleep"
|
||||
const val USE_WEBHOOK = "useWebhook"
|
||||
const val WEBHOOK_CONFIG = "webhookConfig"
|
||||
object STRINGS {
|
||||
const val ONLINE = "strings.online"
|
||||
const val OFFLINE = "strings.offline"
|
||||
@ -24,6 +27,7 @@ object Constants {
|
||||
const val TIME = "commands.time"
|
||||
const val ONLINE = "commands.online"
|
||||
}
|
||||
const val TELEGRAM_MESSAGE_FORMAT = "telegramMessageFormat"
|
||||
}
|
||||
object DEFS {
|
||||
const val logFromMCtoTG = false
|
||||
@ -39,10 +43,21 @@ object Constants {
|
||||
const val nobodyOnline = "Nobody online"
|
||||
const val playerJoined = "joined"
|
||||
const val playerLeft = "left"
|
||||
const val useWebhook = false
|
||||
const val enable = true
|
||||
const val telegramMessageFormat = "<%username%>: %message%"
|
||||
}
|
||||
object WARN {
|
||||
const val noConfigWarning = "No config file found! Writing default config to config.yml."
|
||||
const val noToken = "Bot token must be defined."
|
||||
const val noUsername = "Bot username must be defined."
|
||||
}
|
||||
object TIMES_OF_DAY {
|
||||
const val day = "\uD83C\uDFDE Day"
|
||||
const val sunset = "\uD83C\uDF06 Sunset"
|
||||
const val night = "\uD83C\uDF03 Night"
|
||||
const val sunrise = "\uD83C\uDF05 Sunrise"
|
||||
}
|
||||
const val USERNAME_PLACEHOLDER = "%username%"
|
||||
const val MESSAGE_TEXT_PLACEHOLDER = "%message%"
|
||||
}
|
||||
|
@ -25,7 +25,6 @@ class EventHandler(private val plugin: Plugin) : Listener {
|
||||
logDeathMessage = getBoolean(C.FIELDS.LOG_PLAYER_DEATH, C.DEFS.logPlayerDeath)
|
||||
logPlayerAsleep = getBoolean(C.FIELDS.LOG_PLAYER_ASLEEP, C.DEFS.logPlayerAsleep)
|
||||
}
|
||||
plugin.logger.info("Log death message: $logDeathMessage")
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
@ -1,6 +1,7 @@
|
||||
package org.kraftwerk28.spigot_tg_bridge
|
||||
|
||||
import com.vdurmont.emoji.EmojiParser
|
||||
import org.bukkit.ChatColor
|
||||
import org.bukkit.plugin.java.JavaPlugin
|
||||
import java.io.File
|
||||
import org.kraftwerk28.spigot_tg_bridge.Constants as C
|
||||
@ -8,7 +9,9 @@ import org.kraftwerk28.spigot_tg_bridge.Constants as C
|
||||
class Plugin : JavaPlugin() {
|
||||
|
||||
lateinit var tgBot: TgBot
|
||||
var chatToTG: Boolean = false
|
||||
val chatToTG: Boolean
|
||||
var _isEnabled: Boolean = false
|
||||
val telegramMessageFormat: String
|
||||
|
||||
init {
|
||||
config.run {
|
||||
@ -16,10 +19,16 @@ class Plugin : JavaPlugin() {
|
||||
C.FIELDS.LOG_FROM_MC_TO_TG,
|
||||
C.DEFS.logFromMCtoTG
|
||||
)
|
||||
_isEnabled = getBoolean(C.FIELDS.ENABLE, C.DEFS.enable)
|
||||
telegramMessageFormat = getString(
|
||||
C.FIELDS.TELEGRAM_MESSAGE_FORMAT,
|
||||
C.DEFS.telegramMessageFormat
|
||||
)!!
|
||||
}
|
||||
}
|
||||
|
||||
override fun onEnable() {
|
||||
if (!_isEnabled) return
|
||||
val configFile = File(
|
||||
server.pluginManager.getPlugin(name)!!.dataFolder,
|
||||
C.configFilename
|
||||
@ -41,6 +50,7 @@ class Plugin : JavaPlugin() {
|
||||
}
|
||||
|
||||
override fun onDisable() {
|
||||
if (!_isEnabled) return
|
||||
config.getString(C.FIELDS.SERVER_STOP_MSG, null)?.let {
|
||||
tgBot.broadcastToTG(it)
|
||||
}
|
||||
@ -53,10 +63,11 @@ class Plugin : JavaPlugin() {
|
||||
}
|
||||
|
||||
fun sendMessageToMCFrom(username: String, text: String) {
|
||||
server.broadcastMessage(
|
||||
EmojiParser.parseToAliases(
|
||||
"<${TgBot.escapeHTML(username)}> $text"
|
||||
)
|
||||
)
|
||||
val prepared = telegramMessageFormat
|
||||
.replace(C.USERNAME_PLACEHOLDER, emojiEsc(username))
|
||||
.replace(C.MESSAGE_TEXT_PLACEHOLDER, emojiEsc(text))
|
||||
server.broadcastMessage(prepared)
|
||||
}
|
||||
|
||||
fun emojiEsc(text: String) = EmojiParser.parseToAliases(text)
|
||||
}
|
||||
|
@ -9,6 +9,8 @@ import com.github.kotlintelegrambot.entities.ParseMode
|
||||
import com.github.kotlintelegrambot.entities.Update
|
||||
import com.github.kotlintelegrambot.entities.User
|
||||
import okhttp3.logging.HttpLoggingInterceptor
|
||||
import java.net.InetAddress
|
||||
import org.kraftwerk28.spigot_tg_bridge.Constants as C
|
||||
|
||||
class TgBot(val plugin: Plugin) {
|
||||
|
||||
@ -18,13 +20,22 @@ class TgBot(val plugin: Plugin) {
|
||||
private val chatToMC: Boolean
|
||||
private val botToken: String
|
||||
private val botUsername: String
|
||||
private val allowWebhook: Boolean
|
||||
private var webhookConfig: Map<String, Any>? = null
|
||||
|
||||
init {
|
||||
plugin.config.run {
|
||||
allowedChats = getLongList(Constants.FIELDS.ALLOWED_CHATS)
|
||||
chatToMC = getBoolean(Constants.FIELDS.LOG_FROM_TG_TO_MC, Constants.DEFS.logFromTGtoMC)
|
||||
botToken = getString(Constants.FIELDS.BOT_TOKEN) ?: throw Exception(Constants.WARN.noToken)
|
||||
botUsername = getString(Constants.FIELDS.BOT_USERNAME) ?: throw Exception(Constants.WARN.noUsername)
|
||||
allowedChats = getLongList(C.FIELDS.ALLOWED_CHATS)
|
||||
chatToMC = getBoolean(C.FIELDS.LOG_FROM_TG_TO_MC, C.DEFS.logFromTGtoMC)
|
||||
botToken = getString(C.FIELDS.BOT_TOKEN) ?: throw Exception(C.WARN.noToken)
|
||||
botUsername = getString(C.FIELDS.BOT_USERNAME) ?: throw Exception(C.WARN.noUsername)
|
||||
allowWebhook = getBoolean(C.FIELDS.USE_WEBHOOK, C.DEFS.useWebhook)
|
||||
|
||||
val whCfg = get(C.FIELDS.WEBHOOK_CONFIG)
|
||||
if (whCfg is Map<*, *>) {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
webhookConfig = whCfg as Map<String, Any>?
|
||||
}
|
||||
}
|
||||
val slashRegex = "^/+".toRegex()
|
||||
|
||||
@ -32,21 +43,26 @@ class TgBot(val plugin: Plugin) {
|
||||
token = botToken
|
||||
logLevel = HttpLoggingInterceptor.Level.NONE
|
||||
dispatch {
|
||||
text(null, ::onText)
|
||||
command(commands.time.replace(slashRegex, ""), ::time)
|
||||
command(commands.online.replace(slashRegex, ""), ::online)
|
||||
text(null, ::onText)
|
||||
}
|
||||
}
|
||||
bot.startPolling()
|
||||
plugin.logger.info("Server address: ${InetAddress.getLocalHost().hostAddress}.")
|
||||
webhookConfig?.let { config ->
|
||||
plugin.logger.info("Running in webhook mode.")
|
||||
} ?: run {
|
||||
bot.startPolling()
|
||||
}
|
||||
}
|
||||
|
||||
private fun time(bot: Bot, update: Update) {
|
||||
val t = plugin.server.worlds[0].time
|
||||
var text = when {
|
||||
t <= 12000 -> "\uD83C\uDFDE Day"
|
||||
t <= 13800 -> "\uD83C\uDF06 Sunset"
|
||||
t <= 22200 -> "\uD83C\uDF03 Night"
|
||||
t <= 24000 -> "\uD83C\uDF05 Sunrise"
|
||||
t <= 12000 -> C.TIMES_OF_DAY.day
|
||||
t <= 13800 -> C.TIMES_OF_DAY.sunset
|
||||
t <= 22200 -> C.TIMES_OF_DAY.night
|
||||
t <= 24000 -> C.TIMES_OF_DAY.sunrise
|
||||
else -> ""
|
||||
}
|
||||
text += " ($t)"
|
||||
@ -65,12 +81,12 @@ class TgBot(val plugin: Plugin) {
|
||||
.mapIndexed { i, s -> "${i + 1}. ${s.displayName}" }
|
||||
.joinToString("\n")
|
||||
val onlineStr = plugin.config.getString(
|
||||
Constants.FIELDS.STRINGS.ONLINE,
|
||||
Constants.DEFS.playersOnline
|
||||
C.FIELDS.STRINGS.ONLINE,
|
||||
C.DEFS.playersOnline
|
||||
)!!
|
||||
val offlineStr = plugin.config.getString(
|
||||
Constants.FIELDS.STRINGS.OFFLINE,
|
||||
Constants.DEFS.nobodyOnline
|
||||
C.FIELDS.STRINGS.OFFLINE,
|
||||
C.DEFS.nobodyOnline
|
||||
)!!
|
||||
val text =
|
||||
if (playerList.isNotEmpty()) "$onlineStr:\n$playerStr"
|
||||
@ -102,6 +118,7 @@ class TgBot(val plugin: Plugin) {
|
||||
private fun onText(bot: Bot, update: Update) {
|
||||
if (!chatToMC) return
|
||||
val msg = update.message!!
|
||||
if (msg.text!!.startsWith("/")) return // Suppress command forwarding
|
||||
plugin.sendMessageToMCFrom(rawUserMention(msg.from!!), msg.text!!)
|
||||
}
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
enable: true
|
||||
botToken: abcdef123456789
|
||||
botUsername: sample_username
|
||||
chats:
|
||||
@ -9,6 +10,7 @@ logFromMCtoTG: true
|
||||
logFromTGtoMC: true
|
||||
logPlayerDeath: false
|
||||
logPlayerAsleep: false
|
||||
telegramMessageFormat: '<%player%>: %message%'
|
||||
strings:
|
||||
online: '<b>Online</b>'
|
||||
nobodyOnline: '<b>Nobody online...</b>'
|
||||
|
@ -1,5 +1,5 @@
|
||||
name: SpigotTGBridge
|
||||
version: 0.0.8
|
||||
version: 0.0.9
|
||||
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