Skip to content

Commit

Permalink
feature: release 0.0.9
Browse files Browse the repository at this point in the history
Changelog:
- message color format
- plugin enable flag
  • Loading branch information
kraftwerk28 committed Jul 18, 2020
1 parent 6cffdf4 commit 3afb144
Show file tree
Hide file tree
Showing 9 changed files with 94 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .idea/jarRepositories.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 19 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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)
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ repositories {
url "https://oss.sonatype.org/content/repositories/snapshots/"
}
mavenCentral()
jcenter()
}

def plugDir = "MinecraftServers/spigot_1.15.2/plugins/"
Expand All @@ -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 {
Expand Down
15 changes: 15 additions & 0 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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
Expand All @@ -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%"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 17 additions & 6 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/Plugin.kt
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
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

class Plugin : JavaPlugin() {

lateinit var tgBot: TgBot
var chatToTG: Boolean = false
val chatToTG: Boolean
var _isEnabled: Boolean = false
val telegramMessageFormat: String

init {
config.run {
chatToTG = getBoolean(
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
Expand All @@ -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)
}
Expand All @@ -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)
}
45 changes: 31 additions & 14 deletions src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -18,35 +20,49 @@ 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()

bot = bot {
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)"
Expand All @@ -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"
Expand Down Expand Up @@ -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!!)
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
enable: true
botToken: abcdef123456789
botUsername: sample_username
chats:
Expand All @@ -9,11 +10,12 @@ logFromMCtoTG: true
logFromTGtoMC: true
logPlayerDeath: false
logPlayerAsleep: false
telegramMessageFormat: '<%player%>: %message%'
strings:
online: '<b>Online</b>'
nobodyOnline: '<b>Nobody online...</b>'
joined: 'joined'
left: 'left'
commands:
time: 'time'
online: 'online'
online: 'online'
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -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.
Expand Down

0 comments on commit 3afb144

Please sign in to comment.