diff --git a/build.gradle.kts b/build.gradle.kts index 1104651..6d3b4dc 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,10 +5,6 @@ import java.io.File import java.io.FileInputStream buildscript { - repositories { - mavenCentral() - maven("https://plugins.gradle.org/m2/") - } dependencies { classpath("org.yaml:snakeyaml:1.26") classpath("org.jlleitschuh.gradle:ktlint-gradle:10.1.0") @@ -16,26 +12,25 @@ buildscript { } plugins { - id("org.jetbrains.kotlin.jvm") version "1.6.10" - id("com.github.johnrengelman.shadow") version "5.2.0" + id("org.jetbrains.kotlin.jvm") version "1.8.20" + id("com.github.johnrengelman.shadow") version "8.1.1" id("org.jlleitschuh.gradle.ktlint") version "10.1.0" } -group = "org.kraftwerk28" - -val cfg: Map = Yaml() - .load(FileInputStream("$projectDir/src/main/resources/plugin.yml")) +val cfg: Map = Yaml().run { + val pluginFile = FileInputStream("$projectDir/src/main/resources/plugin.yml") + load(pluginFile) +} val pluginVersion = cfg.get("version") val spigotApiVersion = cfg.get("api-version") val retrofitVersion = "2.7.1" + +group = "org.kraftwerk28" version = pluginVersion as Any repositories { mavenCentral() - maven( - url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/" - ) - maven(url = "https://jitpack.io") + maven(url = "https://hub.spigotmc.org/nexus/content/repositories/snapshots/") maven(url = "https://oss.sonatype.org/content/repositories/snapshots/") } @@ -71,9 +66,10 @@ tasks { dependsOn("shadowJar") finalizedBy("copyArtifacts") } + withType { + sourceCompatibility = JavaVersion.VERSION_1_8.toString() + } withType { - kotlinOptions { - jvmTarget = "1.8" - } + kotlinOptions.jvmTarget = "1.8" } } 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 b6a9d54..f9dd87d 100644 --- a/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt +++ b/src/main/kotlin/org/kraftwerk28/spigot_tg_bridge/TgBot.kt @@ -89,7 +89,7 @@ class TgBot( } private fun initPolling() = plugin.launch { - loop@while (true) { + loop@ while (true) { try { api.getUpdates( offset = currentOffset, @@ -124,19 +124,21 @@ class TgBot( } private suspend fun handleUpdate(update: Update) { - // Ignore private message or channel post - if (listOf("private", "channel").contains(update.message?.chat?.type)) + // Accept only these chat types + if (!listOf("group", "supergroup").contains(update.message?.chat?.type)) return - val ctx = HandlerContext( - update, - update.message, - update.message?.chat, - ) + + // Check whether the chat is white-listed in config + if (!config.allowedChats.contains(update.message?.chat?.id)) + return + + val ctx = HandlerContext(update, update.message, update.message?.chat) update.message?.text?.let { commandRegex?.matchEntire(it)?.groupValues?.let { matchList -> - commandMap[matchList[1]]?.run { - val args = matchList[2].split("\\s+".toRegex()) - this(ctx.copy(commandArgs = args)) + commandMap[matchList[1]]?.let { handler -> + val commandArgs = matchList[2].split("""\s+""".toRegex()) + val handlerContext = ctx.copy(commandArgs = commandArgs) + handler(handlerContext) } } ?: run { onTextHandler(ctx) @@ -231,9 +233,9 @@ class TgBot( api.sendMessage(ctx.message!!.chat.id, "No linked users.") } else { val text = "Linked users:\n" + - linkedUsers.mapIndexed { i, dbUser -> - "${i + 1}. ${dbUser.fullName()}" - }.joinToString("\n") + linkedUsers.mapIndexed { i, dbUser -> + "${i + 1}. ${dbUser.fullName()}" + }.joinToString("\n") api.sendMessage(ctx.message!!.chat.id, text) } } diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 9b08d6c..fe4a254 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -1,5 +1,5 @@ name: SpigotTGBridge -version: "0.20.1" +version: "0.20.2" api-version: "1.15" main: org.kraftwerk28.spigot_tg_bridge.Plugin description: Telegram <-> Minecraft communication plugin for Spigot.