mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-29 22:27:31 +00:00
feat(bot): 新增命令列表更新功能.
增加`命令列表更新`功能, 该功能可根据已加载的 Ability, 调用 Telegram Bot API 更新命令列表. 后续会尝试支持 BotCommandScope.
This commit is contained in:
parent
de83d2c3d3
commit
4b20b0cc59
@ -45,6 +45,7 @@ internal data class BotConfig(
|
|||||||
val enabled: Boolean = true,
|
val enabled: Boolean = true,
|
||||||
val account: BotAccount,
|
val account: BotAccount,
|
||||||
val disableBuiltInAbility: Boolean = false,
|
val disableBuiltInAbility: Boolean = false,
|
||||||
|
val autoUpdateCommandList: Boolean = false,
|
||||||
/*
|
/*
|
||||||
* 使用构件坐标来选择机器人所使用的扩展包.
|
* 使用构件坐标来选择机器人所使用的扩展包.
|
||||||
* 这么做的原因是我暂时没找到一个合适的方法来让开发者方便地设定自己的扩展 Id,
|
* 这么做的原因是我暂时没找到一个合适的方法来让开发者方便地设定自己的扩展 Id,
|
||||||
|
@ -119,6 +119,20 @@ internal class Launcher : AutoCloseable {
|
|||||||
)
|
)
|
||||||
botSessionMap[bot] = botApi.registerBot(bot)
|
botSessionMap[bot] = botApi.registerBot(bot)
|
||||||
log.info { "机器人 `${bot.botUsername}` 已启动." }
|
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
|
@Synchronized
|
||||||
|
@ -7,10 +7,14 @@ import mu.KotlinLogging
|
|||||||
import org.eclipse.aether.artifact.Artifact
|
import org.eclipse.aether.artifact.Artifact
|
||||||
import org.telegram.abilitybots.api.bot.AbilityBot
|
import org.telegram.abilitybots.api.bot.AbilityBot
|
||||||
import org.telegram.abilitybots.api.db.DBContext
|
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.BareboneToggle
|
||||||
import org.telegram.abilitybots.api.toggle.DefaultToggle
|
import org.telegram.abilitybots.api.toggle.DefaultToggle
|
||||||
import org.telegram.telegrambots.bots.DefaultBotOptions
|
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.Update
|
||||||
|
import org.telegram.telegrambots.meta.api.objects.commands.BotCommand
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 可扩展 Bot.
|
* 可扩展 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() {
|
override fun onRegister() {
|
||||||
super.onRegister()
|
super.onRegister()
|
||||||
onlineBotGauge.inc()
|
onlineBotGauge.inc()
|
||||||
|
Loading…
Reference in New Issue
Block a user