diff --git a/scalabot-app/src/main/kotlin/AppConfigs.kt b/scalabot-app/src/main/kotlin/AppConfigs.kt index c4620c8..cb4a8ce 100644 --- a/scalabot-app/src/main/kotlin/AppConfigs.kt +++ b/scalabot-app/src/main/kotlin/AppConfigs.kt @@ -45,6 +45,7 @@ internal data class BotConfig( val enabled: Boolean = true, val account: BotAccount, val disableBuiltInAbility: Boolean = false, + val autoUpdateCommandList: Boolean = false, /* * 使用构件坐标来选择机器人所使用的扩展包. * 这么做的原因是我暂时没找到一个合适的方法来让开发者方便地设定自己的扩展 Id, diff --git a/scalabot-app/src/main/kotlin/AppMain.kt b/scalabot-app/src/main/kotlin/AppMain.kt index e4b8cdb..1cf0dc8 100644 --- a/scalabot-app/src/main/kotlin/AppMain.kt +++ b/scalabot-app/src/main/kotlin/AppMain.kt @@ -119,6 +119,20 @@ internal class Launcher : AutoCloseable { ) botSessionMap[bot] = botApi.registerBot(bot) log.info { "机器人 `${bot.botUsername}` 已启动." } + + if (botConfig.autoUpdateCommandList) { + log.debug { "[Bot ${botConfig.account.name}] 正在自动更新命令列表..." } + try { + val result = bot.updateCommandList() + if (result) { + log.info { "[Bot ${botConfig.account.name}] 已成功更新 Bot 命令列表." } + } else { + log.warn { "[Bot ${botConfig.account.name}] 自动更新 Bot 命令列表失败!" } + } + } catch (e: Exception) { + log.warn(e) { "命令列表自动更新失败." } + } + } } @Synchronized diff --git a/scalabot-app/src/main/kotlin/ScalaBot.kt b/scalabot-app/src/main/kotlin/ScalaBot.kt index f285504..3e24123 100644 --- a/scalabot-app/src/main/kotlin/ScalaBot.kt +++ b/scalabot-app/src/main/kotlin/ScalaBot.kt @@ -7,10 +7,14 @@ import mu.KotlinLogging import org.eclipse.aether.artifact.Artifact import org.telegram.abilitybots.api.bot.AbilityBot import org.telegram.abilitybots.api.db.DBContext +import org.telegram.abilitybots.api.objects.Ability import org.telegram.abilitybots.api.toggle.BareboneToggle import org.telegram.abilitybots.api.toggle.DefaultToggle import org.telegram.telegrambots.bots.DefaultBotOptions +import org.telegram.telegrambots.meta.api.methods.commands.DeleteMyCommands +import org.telegram.telegrambots.meta.api.methods.commands.SetMyCommands import org.telegram.telegrambots.meta.api.objects.Update +import org.telegram.telegrambots.meta.api.objects.commands.BotCommand /** * 可扩展 Bot. @@ -75,6 +79,34 @@ internal class ScalaBot( } } + /** + * 更新 Telegram Bot 的命令列表. + * + * 本方法将根据已注册的 [Ability] 更新 Telegram 中机器人的命令列表. + * + * 调用本方法前, 必须先调用一次 [registerAbilities], 否则无法获取 Ability 信息. + * @return 更新成功返回 `true`. + */ + fun updateCommandList(): Boolean { + if (abilities() == null) { + throw IllegalStateException("Abilities has not been initialized.") + } + + val botCommands = abilities().values.map { + val abilityInfo = if (it.info() == null || it.info().trim().isEmpty()) { + log.warn { "[Bot $botUsername] Ability `${it.name()}` 没有说明信息." } + "(The command has no description)" + } else { + log.debug { "[Bot $botUsername] Ability `${it.name()}` info `${it.info()}`" } + it.info().trim() + } + BotCommand(it.name(), abilityInfo) + } + val setMyCommands = SetMyCommands() + setMyCommands.commands = botCommands + return execute(DeleteMyCommands()) && execute(setMyCommands) + } + override fun onRegister() { super.onRegister() onlineBotGauge.inc()