Skip to content

Commit

Permalink
Remove deprecation from User.discriminator and User.tag (#901)
Browse files Browse the repository at this point in the history
Bot users still have discriminators, and it doesn't seem like this will
be changed any time soon [1].

[1] https://discord.com/developers/docs/change-log#unique-usernames-on-discord
  • Loading branch information
lukellmann authored Dec 24, 2023
1 parent 3297a27 commit 65101a4
Show file tree
Hide file tree
Showing 11 changed files with 13 additions and 55 deletions.
11 changes: 0 additions & 11 deletions common/src/commonMain/kotlin/entity/DiscordUser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import kotlinx.serialization.ExperimentalSerializationApi
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.json.JsonNames
import kotlin.DeprecationLevel.WARNING
import kotlin.contracts.InvocationKind
import kotlin.contracts.contract

Expand Down Expand Up @@ -80,11 +79,6 @@ import kotlin.contracts.contract
public data class DiscordUser(
val id: Snowflake,
val username: String,
@Deprecated(
"Discord's username system is changing and discriminators are being removed, see " +
"https://discord.com/developers/docs/change-log#unique-usernames-on-discord for details.",
level = WARNING,
)
val discriminator: Optional<String> = Optional.Missing(),
@SerialName("global_name")
val globalName: Optional<String?> = Optional.Missing(),
Expand Down Expand Up @@ -130,11 +124,6 @@ public data class DiscordUser(
public data class DiscordOptionallyMemberUser(
val id: Snowflake,
val username: String,
@Deprecated(
"Discord's username system is changing and discriminators are being removed, see " +
"https://discord.com/developers/docs/change-log#unique-usernames-on-discord for details.",
level = WARNING,
)
val discriminator: Optional<String> = Optional.Missing(),
@SerialName("global_name")
val globalName: Optional<String?> = Optional.Missing(),
Expand Down
3 changes: 0 additions & 3 deletions common/src/commonTest/kotlin/json/ChannelTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ class ChannelTest {
recipients.value!!.size shouldBe 1
with(recipients.value!!.first()) {
username shouldBe "test"
@Suppress("DEPRECATION")
discriminator shouldBe "9999"
globalName shouldBe null
id.toString() shouldBe "82198898841029460"
Expand Down Expand Up @@ -64,15 +63,13 @@ class ChannelTest {
recipients.value!!.size shouldBe 2
with(recipients.value!!.first()) {
username shouldBe "test"
@Suppress("DEPRECATION")
discriminator shouldBe "9999"
globalName shouldBe null
id.toString() shouldBe "82198898841029460"
avatar shouldBe "33ecab261d4681afa4d85a04691c4a01"
}
with(recipients.value!![1]) {
username shouldBe "test2"
@Suppress("DEPRECATION")
discriminator shouldBe "9999"
globalName shouldBe "amazing name"
id.toString() shouldBe "82198810841029460"
Expand Down
1 change: 0 additions & 1 deletion common/src/commonTest/kotlin/json/EmojiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class EmojiTest {
roles shouldBe listOf("41771983429993000", "41771983429993111").map { Snowflake(it) }
with(user.value!!) {
username shouldBe "Luigi"
@Suppress("DEPRECATION")
discriminator shouldBe "0002"
globalName shouldBe null
id shouldBe "96008815106887111"
Expand Down
2 changes: 0 additions & 2 deletions common/src/commonTest/kotlin/json/MessageTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ class MessageTest {
editedTimestamp shouldBe null
with(author) {
username shouldBe "Mason"
@Suppress("DEPRECATION")
discriminator shouldBe "9999"
globalName shouldBe null
id shouldBe "53908099506183680"
Expand Down Expand Up @@ -77,7 +76,6 @@ class MessageTest {
editedTimestamp shouldBe null
with(author) {
username shouldBe "Mason"
@Suppress("DEPRECATION")
discriminator shouldBe "9999"
globalName shouldBe "Mason!"
id.toString() shouldBe "53908099506183680"
Expand Down
1 change: 0 additions & 1 deletion common/src/commonTest/kotlin/json/UserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class UserTest {
with(user) {
id.toString() shouldBe "80351110224678912"
username shouldBe "Nelly"
@Suppress("DEPRECATION")
discriminator shouldBe "1337"
globalName shouldBe "Nelly"
avatar shouldBe "8342729096ea3675442027381ff50dfe"
Expand Down
10 changes: 2 additions & 8 deletions core/src/commonMain/kotlin/cache/data/UserData.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,13 @@ import dev.kord.common.entity.UserFlags
import dev.kord.common.entity.optional.Optional
import dev.kord.common.entity.optional.OptionalBoolean
import kotlinx.serialization.Serializable
import kotlin.DeprecationLevel.WARNING

private val WebhookData.nullableUserId get() = userId.value

@Serializable
public data class UserData(
val id: Snowflake,
val username: String,
@Deprecated(
"Discord's username system is changing and discriminators are being removed, see " +
"https://discord.com/developers/docs/change-log#unique-usernames-on-discord for details.",
level = WARNING,
)
val discriminator: Optional<String> = Optional.Missing(),
val globalName: Optional<String?> = Optional.Missing(),
val avatar: String? = null,
Expand All @@ -41,11 +35,11 @@ public data class UserData(
}

public fun from(entity: DiscordUser): UserData = with(entity) {
UserData(id, username, @Suppress("DEPRECATION") discriminator, globalName, avatar, bot, publicFlags, banner, accentColor, avatarDecoration)
UserData(id, username, discriminator, globalName, avatar, bot, publicFlags, banner, accentColor, avatarDecoration)
}

public fun from(entity: DiscordOptionallyMemberUser): UserData = with(entity) {
UserData(id, username, @Suppress("DEPRECATION") discriminator, globalName, avatar, bot, publicFlags)
UserData(id, username, discriminator, globalName, avatar, bot, publicFlags)
}

}
Expand Down
9 changes: 1 addition & 8 deletions core/src/commonMain/kotlin/entity/Asset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,8 @@ public class Asset private constructor(
public fun userBanner(userId: Snowflake, hash: String, kord: Kord): Asset =
Asset(hash.isAnimated, DiscordCdn.userBanner(userId, hash), kord)

@Suppress("DEPRECATION")
@Deprecated(
"Discord's username system is changing and discriminators are being removed, see " +
"https://discord.com/developers/docs/change-log#unique-usernames-on-discord for details.",
ReplaceWith("Asset.defaultUserAvatar(userId, kord)", imports = ["dev.kord.core.entity.Asset"]),
DeprecationLevel.WARNING,
)
public fun defaultUserAvatar(discriminator: Int, kord: Kord): Asset =
Asset(isAnimated = false, DiscordCdn.defaultAvatar(discriminator), kord, recommendedFormat = PNG)
Asset(isAnimated = false, DiscordCdn.defaultUserAvatar(discriminator), kord, recommendedFormat = PNG)

public fun defaultUserAvatar(userId: Snowflake, kord: Kord): Asset =
Asset(isAnimated = false, DiscordCdn.defaultUserAvatar(userId), kord, recommendedFormat = PNG)
Expand Down
20 changes: 4 additions & 16 deletions core/src/commonMain/kotlin/entity/User.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import dev.kord.core.behavior.UserBehavior
import dev.kord.core.cache.data.UserData
import dev.kord.core.supplier.EntitySupplier
import dev.kord.core.supplier.EntitySupplyStrategy
import kotlin.DeprecationLevel.WARNING

/**
* The user's effective name, prioritizing [globalName][User.globalName] over [username][User.username].
Expand Down Expand Up @@ -46,7 +45,7 @@ public open class User(
public val defaultAvatar: Asset
get() =
if (migratedToNewUsernameSystem) Asset.defaultUserAvatar(userId = id, kord)
else @Suppress("DEPRECATION") Asset.defaultUserAvatar(discriminator.toInt(), kord)
else Asset.defaultUserAvatar(discriminator.toInt(), kord)

public val avatarDecorationHash: String? get() = data.avatarDecoration.value

Expand All @@ -67,16 +66,9 @@ public open class User(
// "0" when data.discriminator is missing: if the field is missing, all users were migrated,
// see https://discord.com/developers/docs/change-log#identifying-migrated-users:
// "After migration of all users is complete, the `discriminator` field may be removed."
@Suppress("DEPRECATION", "DeprecatedCallableAddReplaceWith")
@Deprecated(
"Discord's username system is changing and discriminators are being removed, see " +
"https://discord.com/developers/docs/change-log#unique-usernames-on-discord for details.",
level = WARNING,
)
public val discriminator: String get() = data.discriminator.value ?: "0"

// see https://discord.com/developers/docs/change-log#identifying-migrated-users
@Suppress("DEPRECATION")
private val migratedToNewUsernameSystem get() = discriminator == "0"

/** The user's display name, if it is set. For bots, this is the application name. */
Expand All @@ -97,14 +89,10 @@ public open class User(

/**
* The complete user tag.
*
* If this user [has been migrated to the new username system][discriminator], this is the same as [username],
* otherwise a [String] of the form `"username#discriminator"` is returned.
*/
@Suppress("DEPRECATION")
@Deprecated(
"Discord's username system is changing and discriminators are being removed, see " +
"https://discord.com/developers/docs/change-log#unique-usernames-on-discord for details.",
ReplaceWith("this.username"),
level = WARNING,
)
public val tag: String get() = if (migratedToNewUsernameSystem) username else "$username#$discriminator"

/**
Expand Down
1 change: 0 additions & 1 deletion gateway/src/commonTest/kotlin/json/SerializationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class SerializationTest {
with(user) {
id.toString() shouldBe "80351110224678912"
username shouldBe "Nelly"
@Suppress("DEPRECATION")
discriminator shouldBe Optional("1337")
globalName shouldBe Optional(null)
avatar shouldBe "8342729096ea3675442027381ff50dfe"
Expand Down
1 change: 1 addition & 0 deletions rest/api/rest.api
Original file line number Diff line number Diff line change
Expand Up @@ -6297,6 +6297,7 @@ public final class dev/kord/rest/route/DiscordCdn {
public final fun applicationCover (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;)Ldev/kord/rest/route/CdnUrl;
public final fun applicationIcon (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;)Ldev/kord/rest/route/CdnUrl;
public final fun defaultAvatar (I)Ldev/kord/rest/route/CdnUrl;
public final fun defaultUserAvatar (I)Ldev/kord/rest/route/CdnUrl;
public final fun defaultUserAvatar (Ldev/kord/common/entity/Snowflake;)Ldev/kord/rest/route/CdnUrl;
public final fun emoji (Ldev/kord/common/entity/Snowflake;)Ldev/kord/rest/route/CdnUrl;
public final fun guildBanner (Ldev/kord/common/entity/Snowflake;Ljava/lang/String;)Ldev/kord/rest/route/CdnUrl;
Expand Down
9 changes: 5 additions & 4 deletions rest/src/commonMain/kotlin/route/DiscordCdn.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ public object DiscordCdn {
public fun userBanner(userId: Snowflake, hash: String): CdnUrl = CdnUrl("$BASE_URL/banners/$userId/$hash")

@Deprecated(
"Discord's username system is changing and discriminators are being removed, see " +
"https://discord.com/developers/docs/change-log#unique-usernames-on-discord for details.",
ReplaceWith("DiscordCdn.defaultUserAvatar(userId)", imports = ["dev.kord.rest.route.DiscordCdn"]),
"Renamed to 'defaultUserAvatar' to align name with documentation and overload taking Snowflake.",
ReplaceWith("DiscordCdn.defaultUserAvatar(discriminator)", imports = ["dev.kord.rest.route.DiscordCdn"]),
DeprecationLevel.WARNING,
)
public fun defaultAvatar(discriminator: Int): CdnUrl = CdnUrl("$BASE_URL/embed/avatars/${discriminator % 5}")
public fun defaultAvatar(discriminator: Int): CdnUrl = defaultUserAvatar(discriminator)

public fun defaultUserAvatar(discriminator: Int): CdnUrl = CdnUrl("$BASE_URL/embed/avatars/${discriminator % 5}")

public fun defaultUserAvatar(userId: Snowflake): CdnUrl =
CdnUrl("$BASE_URL/embed/avatars/${(userId.value shr 22) % 6u}")
Expand Down

0 comments on commit 65101a4

Please sign in to comment.