Skip to content

Commit 0c80f4c

Browse files
committed
[+] Auto set bot command help
1 parent f507f80 commit 0c80f4c

1 file changed

Lines changed: 31 additions & 6 deletions

File tree

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

Lines changed: 31 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,31 @@ 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+
)
34+
35+
private val COMMAND_SPECS = listOf(
36+
CommandSpec("start", "Check if bot is alive", "/start", "检查机器人在线状态"),
37+
CommandSpec("help", "Show admin commands", "/help", "显示完整命令帮助"),
38+
CommandSpec("ban", "Ban an IP address", "/ban <ip> [reason]", "封禁 IP,可附带原因"),
39+
CommandSpec("unban", "Unban an IP address", "/unban <ip>", "解除 IP 封禁"),
40+
CommandSpec("listban", "List all banned IPs", "/listban", "列出当前所有封禁 IP"),
41+
CommandSpec("note", "Add note to a pending comment", "/note <note>", "回复待审评论消息后添加备注(内容为 clear 可清空)")
42+
)
43+
44+
private val TELEGRAM_COMMANDS = COMMAND_SPECS.map { BotCommand(it.name, it.menuDescription) }
45+
46+
private val HELP_TEXT = buildString {
47+
appendLine("可用命令:")
48+
COMMAND_SPECS.forEach { spec ->
49+
appendLine("${spec.usage} - ${spec.detail}")
50+
}
51+
}
52+
2753
/**
2854
* Command that can only be used in the telegram chats specified in the secrets
2955
*/
@@ -56,12 +82,7 @@ class PostConstruct(
5682
token = secrets.telegramBotToken
5783
dispatch {
5884
cmd("start") { "🐈 Running!" }
59-
secureCmd("help") { """
60-
/ban <ip> [reason]
61-
/unban <ip>
62-
/listban
63-
/note <note>""".trimIndent()
64-
}
85+
secureCmd("help") { HELP_TEXT }
6586
secureCmd("ban") {
6687
val args = (message.text ?: "").split(" ").slice(1)
6788
if (args.isEmpty()) return@secureCmd "Usage: /ban <ip> [reason]"
@@ -99,6 +120,10 @@ class PostConstruct(
99120
}
100121
}
101122
bot.sendMessage(ChatId.fromId(secrets.telegramChatID), getMorningMsg() + "\n\n(服务器已起床)")
123+
bot.setMyCommands(TELEGRAM_COMMANDS).fold(
124+
{ println("[Telegram] Command list synced (${TELEGRAM_COMMANDS.size} commands)") },
125+
{ println("[Telegram] Failed to sync command list: $it") }
126+
)
102127
GlobalScope.launch {
103128
println(geoIP.info("127.0.0.1"))
104129
}

0 commit comments

Comments
 (0)