Skip to content

Commit 122da8d

Browse files
committed
[+] Auto set bot command help
1 parent f507f80 commit 122da8d

1 file changed

Lines changed: 32 additions & 6 deletions

File tree

src/main/kotlin/org/hydev/back/Application.kt

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import com.github.kotlintelegrambot.bot
55
import com.github.kotlintelegrambot.dispatch
66
import com.github.kotlintelegrambot.dispatcher.Dispatcher
77
import com.github.kotlintelegrambot.dispatcher.callbackQuery
8+
import com.github.kotlintelegrambot.entities.BotCommand
89
import com.github.kotlintelegrambot.entities.ChatId
910
import com.github.kotlintelegrambot.logging.LogLevel
1011
import kotlinx.coroutines.DelicateCoroutinesApi
@@ -24,6 +25,32 @@ val secrets = getSecrets()
2425
lateinit var bot: Bot
2526
private val COMMENT_ID_REGEX = Regex("^#(\\d+)\\b")
2627

28+
private data class CommandSpec(
29+
val name: String,
30+
val menuDescription: String,
31+
val usage: String,
32+
val detail: String,
33+
val secureOnly: Boolean = true
34+
)
35+
36+
private val COMMAND_SPECS = listOf(
37+
CommandSpec("start", "Check if bot is alive", "/start", "检查机器人在线状态", secureOnly = false),
38+
CommandSpec("help", "Show admin commands", "/help", "显示完整命令帮助"),
39+
CommandSpec("ban", "Ban an IP address", "/ban <ip> [reason]", "封禁 IP,可附带原因"),
40+
CommandSpec("unban", "Unban an IP address", "/unban <ip>", "解除 IP 封禁"),
41+
CommandSpec("listban", "List all banned IPs", "/listban", "列出当前所有封禁 IP"),
42+
CommandSpec("note", "Add note to a pending comment", "/note <note>", "回复待审评论消息后添加备注(内容为 clear 可清空)")
43+
)
44+
45+
private val TELEGRAM_COMMANDS = COMMAND_SPECS.map { BotCommand(it.name, it.menuDescription) }
46+
47+
private val HELP_TEXT = buildString {
48+
appendLine("可用命令:")
49+
COMMAND_SPECS.forEach { spec ->
50+
appendLine("${spec.usage} - ${spec.detail}")
51+
}
52+
}
53+
2754
/**
2855
* Command that can only be used in the telegram chats specified in the secrets
2956
*/
@@ -56,12 +83,7 @@ class PostConstruct(
5683
token = secrets.telegramBotToken
5784
dispatch {
5885
cmd("start") { "🐈 Running!" }
59-
secureCmd("help") { """
60-
/ban <ip> [reason]
61-
/unban <ip>
62-
/listban
63-
/note <note>""".trimIndent()
64-
}
86+
secureCmd("help") { HELP_TEXT }
6587
secureCmd("ban") {
6688
val args = (message.text ?: "").split(" ").slice(1)
6789
if (args.isEmpty()) return@secureCmd "Usage: /ban <ip> [reason]"
@@ -99,6 +121,10 @@ class PostConstruct(
99121
}
100122
}
101123
bot.sendMessage(ChatId.fromId(secrets.telegramChatID), getMorningMsg() + "\n\n(服务器已起床)")
124+
bot.setMyCommands(TELEGRAM_COMMANDS).fold(
125+
{ println("[Telegram] Command list synced (${TELEGRAM_COMMANDS.size} commands)") },
126+
{ println("[Telegram] Failed to sync command list: $it") }
127+
)
102128
GlobalScope.launch {
103129
println(geoIP.info("127.0.0.1"))
104130
}

0 commit comments

Comments
 (0)