mirror of
https://github.com/amalthea-mc/spigot-tg-bridge.git
synced 2024-11-23 19:02:21 +00:00
Close connection in job cancellation
This commit is contained in:
parent
655ab4b002
commit
a8bf75a977
@ -37,7 +37,7 @@ repositories {
|
|||||||
|
|
||||||
val tgBotVersion = "6.0.4"
|
val tgBotVersion = "6.0.4"
|
||||||
val retrofitVersion = "2.7.1"
|
val retrofitVersion = "2.7.1"
|
||||||
val plugDir = "MinecraftServers/spigot_1.17/plugins/"
|
val plugDir = "MinecraftServers/spigot_1.16.5/plugins/"
|
||||||
val homeDir = System.getProperty("user.home")
|
val homeDir = System.getProperty("user.home")
|
||||||
|
|
||||||
tasks {
|
tasks {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package org.kraftwerk28.spigot_tg_bridge
|
package org.kraftwerk28.spigot_tg_bridge
|
||||||
|
|
||||||
import com.google.gson.annotations.SerializedName as Name
|
import com.google.gson.annotations.SerializedName as Name
|
||||||
|
import retrofit2.Call
|
||||||
import retrofit2.http.*
|
import retrofit2.http.*
|
||||||
|
|
||||||
interface TgApiService {
|
interface TgApiService {
|
||||||
@ -56,11 +57,11 @@ interface TgApiService {
|
|||||||
): TgResponse<Message>
|
): TgResponse<Message>
|
||||||
|
|
||||||
@GET("getUpdates")
|
@GET("getUpdates")
|
||||||
suspend fun getUpdates(
|
fun getUpdates(
|
||||||
@Query("offset") offset: Long,
|
@Query("offset") offset: Long,
|
||||||
@Query("limit") limit: Int = 100,
|
@Query("limit") limit: Int = 100,
|
||||||
@Query("timeout") timeout: Int = 0,
|
@Query("timeout") timeout: Int = 0,
|
||||||
): TgResponse<List<Update>>
|
): Call<TgResponse<List<Update>>>
|
||||||
|
|
||||||
@GET("getMe")
|
@GET("getMe")
|
||||||
suspend fun getMe(): TgResponse<User>
|
suspend fun getMe(): TgResponse<User>
|
||||||
|
@ -22,10 +22,12 @@ class Plugin : JavaPlugin() {
|
|||||||
return
|
return
|
||||||
|
|
||||||
val cmdHandler = CommandHandler(this)
|
val cmdHandler = CommandHandler(this)
|
||||||
tgBot = TgBot(this, config)
|
loadBot()
|
||||||
getCommand(C.COMMANDS.PLUGIN_RELOAD)?.setExecutor(cmdHandler)
|
tgBot?.let { bot ->
|
||||||
val eventHandler = EventHandler(tgBot!!, config)
|
val eventHandler = EventHandler(bot, config)
|
||||||
server.pluginManager.registerEvents(eventHandler, this)
|
server.pluginManager.registerEvents(eventHandler, this)
|
||||||
|
}
|
||||||
|
getCommand(C.COMMANDS.PLUGIN_RELOAD)?.setExecutor(cmdHandler)
|
||||||
|
|
||||||
// Notify Telegram groups about server start
|
// Notify Telegram groups about server start
|
||||||
config.serverStartMessage?.let { message ->
|
config.serverStartMessage?.let { message ->
|
||||||
@ -34,6 +36,11 @@ class Plugin : JavaPlugin() {
|
|||||||
logger.info("Plugin started.")
|
logger.info("Plugin started.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun loadBot() {
|
||||||
|
tgBot?.let { it.stop() }
|
||||||
|
tgBot = TgBot(this, config)
|
||||||
|
}
|
||||||
|
|
||||||
override fun onDisable() {
|
override fun onDisable() {
|
||||||
if (!config.isEnabled) return
|
if (!config.isEnabled) return
|
||||||
config.serverStopMessage?.let {
|
config.serverStopMessage?.let {
|
||||||
@ -55,8 +62,7 @@ class Plugin : JavaPlugin() {
|
|||||||
fun reload() {
|
fun reload() {
|
||||||
logger.info(C.INFO.reloading)
|
logger.info(C.INFO.reloading)
|
||||||
config.reload(this)
|
config.reload(this)
|
||||||
tgBot?.stop()
|
loadBot()
|
||||||
tgBot = TgBot(this, config)
|
|
||||||
logger.info(C.INFO.reloadComplete)
|
logger.info(C.INFO.reloadComplete)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,6 +6,7 @@ import kotlinx.coroutines.channels.*
|
|||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.logging.HttpLoggingInterceptor
|
import okhttp3.logging.HttpLoggingInterceptor
|
||||||
import retrofit2.Retrofit
|
import retrofit2.Retrofit
|
||||||
|
import retrofit2.Call
|
||||||
import retrofit2.converter.gson.GsonConverterFactory
|
import retrofit2.converter.gson.GsonConverterFactory
|
||||||
import java.time.Duration
|
import java.time.Duration
|
||||||
|
|
||||||
@ -32,13 +33,8 @@ class TgBot(
|
|||||||
}
|
}
|
||||||
|
|
||||||
init {
|
init {
|
||||||
val interceptor = HttpLoggingInterceptor().apply {
|
|
||||||
level = HttpLoggingInterceptor.Level.NONE;
|
|
||||||
}
|
|
||||||
|
|
||||||
client = OkHttpClient
|
client = OkHttpClient
|
||||||
.Builder()
|
.Builder()
|
||||||
.addInterceptor(interceptor)
|
|
||||||
.readTimeout(Duration.ZERO)
|
.readTimeout(Duration.ZERO)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
@ -65,40 +61,45 @@ class TgBot(
|
|||||||
api.setMyCommands(commands)
|
api.setMyCommands(commands)
|
||||||
}
|
}
|
||||||
|
|
||||||
pollJob = scope.launch {
|
pollJob = initPolling()
|
||||||
|
handlerJob = initHandler()
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun initPolling() = scope.launch {
|
||||||
|
var request:
|
||||||
|
Call<TgApiService.TgResponse<List<TgApiService.Update>>>? = null
|
||||||
try {
|
try {
|
||||||
while (true) {
|
while (true) {
|
||||||
try {
|
try {
|
||||||
pollUpdates()
|
request = api.getUpdates(
|
||||||
} catch (e: Exception) {
|
offset = currentOffset,
|
||||||
e.printStackTrace()
|
timeout = pollTimeout,
|
||||||
}
|
)
|
||||||
}
|
val response = request.execute().body()
|
||||||
} catch (e: CancellationException) {}
|
response?.result?.let { updates ->
|
||||||
}
|
|
||||||
|
|
||||||
handlerJob = scope.launch {
|
|
||||||
try {
|
|
||||||
while (true) {
|
|
||||||
handleUpdate()
|
|
||||||
}
|
|
||||||
} catch (e: CancellationException) {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private suspend fun pollUpdates() {
|
|
||||||
val updatesResponse = api
|
|
||||||
.getUpdates(offset = currentOffset, timeout = pollTimeout)
|
|
||||||
updatesResponse.result?.let { updates ->
|
|
||||||
if (!updates.isEmpty()) {
|
if (!updates.isEmpty()) {
|
||||||
updates.forEach { updateChan.send(it) }
|
updates.forEach { updateChan.send(it) }
|
||||||
currentOffset = updates.last().updateId + 1
|
currentOffset = updates.last().updateId + 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (e: Exception) {
|
||||||
|
e.printStackTrace()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e: CancellationException) {
|
||||||
|
request?.cancel()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private suspend fun handleUpdate() {
|
private fun initHandler() = scope.launch {
|
||||||
val update = updateChan.receive()
|
try {
|
||||||
|
while (true) {
|
||||||
|
handleUpdate(updateChan.receive())
|
||||||
|
}
|
||||||
|
} catch (e: CancellationException) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
suspend fun handleUpdate(update: TgApiService.Update) {
|
||||||
update.message?.text?.let {
|
update.message?.text?.let {
|
||||||
commandRegex.matchEntire(it)?.groupValues?.let {
|
commandRegex.matchEntire(it)?.groupValues?.let {
|
||||||
commandMap[it[1]]?.let { it(update) }
|
commandMap[it[1]]?.let { it(update) }
|
||||||
|
Loading…
Reference in New Issue
Block a user