InsanusMokrassar / ktgbotapi

Type-safe library for work with Telegram Bot API
https://docs.inmo.dev/tgbotapi/index.html
Apache License 2.0
345 stars 29 forks source link

override toString() method in Username for fix duplicating #816

Closed Slenkis closed 8 months ago

Slenkis commented 8 months ago

Problem: duplicating username field in

it.chat.userOrNull()?.username?.username ?: "null username"

After fix:

it.chat.userOrNull()?.username ?: "null username"

Where:

dev.inmo.tgbotapi.types.ChatIdentifier.kt ==>> data class Username

Fix:

@Serializable(ChatIdentifierSerializer::class)
data class Username(
   private val username: String
) : ChatIdentifier {
    override fun toString(): String = this.username

    val usernameWithoutAt
        get() = username.dropWhile { it == '@' }

    init {
        if (!username.startsWith("@")) {
            throw IllegalArgumentException("Username must starts with `@`")
        }
    }
}
github-actions[bot] commented 8 months ago

Welcome with your first issue