From 6df9f1b3c712463ee574987413885a78ea76a5ef Mon Sep 17 00:00:00 2001 From: LamGC Date: Sat, 26 Feb 2022 17:26:24 +0800 Subject: [PATCH] =?UTF-8?q?refactor(bot):=20=E6=8C=89=E7=85=A7=20Kotlin=20?= =?UTF-8?q?=E5=AE=98=E6=96=B9=E4=BB=A3=E7=A0=81=E8=A7=84=E8=8C=83,=20?= =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E4=BB=A3=E7=A0=81=E6=A0=BC=E5=BC=8F=E9=94=99?= =?UTF-8?q?=E8=AF=AF.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Kotlin 官方代码规范建议把 Companion object 放在类的最后面, 确实应该如此. --- scalabot-app/src/main/kotlin/ScalaBot.kt | 82 ++++++++++++------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/scalabot-app/src/main/kotlin/ScalaBot.kt b/scalabot-app/src/main/kotlin/ScalaBot.kt index dc58b59..42a6307 100644 --- a/scalabot-app/src/main/kotlin/ScalaBot.kt +++ b/scalabot-app/src/main/kotlin/ScalaBot.kt @@ -34,6 +34,47 @@ internal class ScalaBot( ) : AbilityBot(token, name, db, if (disableBuiltInAbility) BareboneToggle() else DefaultToggle(), options) { + private val extensionLoader = ExtensionLoader(this) + + init { + val extensionEntries = extensionLoader.getExtensions() + for (entry in extensionEntries) { + addExtension(entry.extension) + log.debug { + "[Bot ${botUsername}] 扩展包 `${entry.extensionArtifact}` 中的扩展 `${entry.extension::class.qualifiedName}` " + + "(由工厂类 `${entry.factoryClass.name}` 创建) 已注册." + } + } + } + + override fun creatorId(): Long = creatorId + + override fun onUpdateReceived(update: Update?) { + botUpdateCounter.labels(botUsername).inc() + botUpdateGauge.labels(botUsername).inc() + + val timer = updateProcessTime.labels(botUsername).startTimer() + try { + super.onUpdateReceived(update) + } catch (e: Exception) { + exceptionHandlingCounter.labels(botUsername).inc() + throw e + } finally { + timer.observeDuration() + botUpdateGauge.labels(botUsername).dec() + } + } + + override fun onRegister() { + super.onRegister() + onlineBotGauge.inc() + } + + override fun onClosing() { + super.onClosing() + onlineBotGauge.dec() + } + companion object { @JvmStatic private val log = KotlinLogging.logger { } @@ -83,45 +124,4 @@ internal class ScalaBot( .subsystem("telegrambots") .register() } - - private val extensionLoader = ExtensionLoader(this) - - init { - val extensionEntries = extensionLoader.getExtensions() - for (entry in extensionEntries) { - addExtension(entry.extension) - log.debug { - "[Bot ${botUsername}] 扩展包 `${entry.extensionArtifact}` 中的扩展 `${entry.extension::class.qualifiedName}` " + - "(由工厂类 `${entry.factoryClass.name}` 创建) 已注册." - } - } - } - - override fun creatorId(): Long = creatorId - - override fun onUpdateReceived(update: Update?) { - botUpdateCounter.labels(botUsername).inc() - botUpdateGauge.labels(botUsername).inc() - - val timer = updateProcessTime.labels(botUsername).startTimer() - try { - super.onUpdateReceived(update) - } catch (e: Exception) { - exceptionHandlingCounter.labels(botUsername).inc() - throw e - } finally { - timer.observeDuration() - botUpdateGauge.labels(botUsername).dec() - } - } - - override fun onRegister() { - super.onRegister() - onlineBotGauge.inc() - } - - override fun onClosing() { - super.onClosing() - onlineBotGauge.dec() - } }