mirror of
https://github.com/LamGC/ScalaBot.git
synced 2025-04-29 22:27:31 +00:00
feat: 在启动机器人时调用 GetMe 接口, 以检查 Token 有效性.
通过这种方式, 可以帮助用户在启动阶段发现 Bot Token 无效的问题.
This commit is contained in:
parent
ba306e679f
commit
fd67eb99eb
@ -9,6 +9,8 @@ import net.lamgc.scalabot.util.registerShutdownHook
|
|||||||
import org.eclipse.aether.repository.LocalRepository
|
import org.eclipse.aether.repository.LocalRepository
|
||||||
import org.telegram.telegrambots.bots.DefaultBotOptions
|
import org.telegram.telegrambots.bots.DefaultBotOptions
|
||||||
import org.telegram.telegrambots.meta.TelegramBotsApi
|
import org.telegram.telegrambots.meta.TelegramBotsApi
|
||||||
|
import org.telegram.telegrambots.meta.api.methods.GetMe
|
||||||
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiRequestException
|
||||||
import org.telegram.telegrambots.meta.generics.BotSession
|
import org.telegram.telegrambots.meta.generics.BotSession
|
||||||
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession
|
import org.telegram.telegrambots.updatesreceivers.DefaultBotSession
|
||||||
import java.io.File
|
import java.io.File
|
||||||
@ -135,9 +137,13 @@ internal class Launcher(
|
|||||||
launchBot(botConfig)
|
launchBot(botConfig)
|
||||||
launchedCounts++
|
launchedCounts++
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
|
if (e is TelegramApiRequestException && e.errorCode == 401) {
|
||||||
|
log.error { "机器人 `${botConfig.account.name}` 的 Bot Token 无效, 请检查配置: [${e.errorCode}] ${e.apiResponse}" }
|
||||||
|
} else {
|
||||||
log.error(e) { "机器人 `${botConfig.account.name}` 启动时发生错误." }
|
log.error(e) { "机器人 `${botConfig.account.name}` 启动时发生错误." }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return if (launchedCounts != 0) {
|
return if (launchedCounts != 0) {
|
||||||
log.info { "已启动 $launchedCounts 个机器人." }
|
log.info { "已启动 $launchedCounts 个机器人." }
|
||||||
true
|
true
|
||||||
@ -201,6 +207,10 @@ internal class Launcher(
|
|||||||
extensionPackageFinders,
|
extensionPackageFinders,
|
||||||
botConfig
|
botConfig
|
||||||
)
|
)
|
||||||
|
|
||||||
|
val botUser = bot.execute(GetMe())
|
||||||
|
log.debug { "已验证 Bot Token 有效性, Bot Username: ${botUser.userName}" }
|
||||||
|
|
||||||
botSessionMap[bot] = botApi.registerBot(bot)
|
botSessionMap[bot] = botApi.registerBot(bot)
|
||||||
log.info { "机器人 `${bot.botUsername}` 已启动." }
|
log.info { "机器人 `${bot.botUsername}` 已启动." }
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user