Skip to content

Commit

Permalink
Merge pull request #18 from simple-robot/dev/fix-simbot-885
Browse files Browse the repository at this point in the history
使TelegramBot.id 返回 ticket.token 而不是 user.id, 并增加新的属性 `userId`; 以及修复bot启动时未初始化用户信息的问题
  • Loading branch information
ForteScarlet authored Jul 20, 2024
2 parents 260675d + 12cf60b commit 73000b9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,21 @@ public interface TelegramBot : Bot {
get() = userInfo.username ?: userInfo.firstName

/**
* The id of current bot.
* The id of [userInfo].
*
* @throws IllegalStateException The bot has not been started, or has never been used [queryUserInfo].
* @see userInfo
* @throws IllegalStateException The bot has not be started,
* or has never been used [queryUserInfo]
*/
public val userId: ID
get() = userInfo.id.ID

/**
* The bot token configured.
*
* If you want to get the `user.id` of this bot,
* use [userId].
*/
override val id: ID
get() = userInfo.id.ID

/**
* Query user info of the current bot.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import kotlinx.coroutines.sync.withLock
import love.forte.simbot.annotations.FragileSimbotAPI
import love.forte.simbot.bot.JobBasedBot
import love.forte.simbot.common.id.ID
import love.forte.simbot.common.id.literal
import love.forte.simbot.common.id.StringID.Companion.ID
import love.forte.simbot.common.id.toLongOrNull
import love.forte.simbot.component.telegram.core.bot.StdlibBot
import love.forte.simbot.component.telegram.core.bot.TelegramBot
import love.forte.simbot.component.telegram.core.bot.TelegramBotConfiguration
Expand Down Expand Up @@ -71,6 +72,8 @@ internal class TelegramBotImpl(
internal val logger = LoggerFactory.getLogger("love.forte.simbot.component.telegram.core.bot")
internal val eventLogger = LoggerFactory.getLogger("love.forte.simbot.component.telegram.stdlib.bot.event")

override val id: ID = source.ticket.token.ID

private var _userInfo: User? = null

override val userInfo: User
Expand All @@ -83,10 +86,10 @@ internal class TelegramBotImpl(
}

override fun isMe(id: ID): Boolean {
// TODO
return source.ticket.token == id.literal
}
val ui = _userInfo ?: return this.id == id

return this.id == id || ui.id == id.toLongOrNull()
}

private val startLock = Mutex()

Expand All @@ -99,6 +102,9 @@ internal class TelegramBotImpl(
}

source.start()
// init bot user info
queryUserInfo()

subscribeInternalProcessor(this, source, eventDispatcher)

// mark started
Expand Down

0 comments on commit 73000b9

Please sign in to comment.