-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Member Avatars & Avatar->Icon refactor (#401)
* Member Avatars & Avatar->Icon refactor * Default to webp instead * Make user avatar optional, add defaultAvatar * Move CDN urls to DiscordCDN * Stuff * Naming * This naming fits better imo
- Loading branch information
Showing
9 changed files
with
133 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package dev.kord.core.entity | ||
|
||
import dev.kord.common.entity.Snowflake | ||
import dev.kord.core.Kord | ||
import dev.kord.core.KordObject | ||
import dev.kord.rest.Image | ||
import dev.kord.rest.route.CdnUrl | ||
import dev.kord.rest.route.DiscordCdn | ||
|
||
sealed class Icon(val animated: Boolean, val cdnUrl: CdnUrl, override val kord: Kord) : KordObject { | ||
|
||
val format: Image.Format | ||
get() = when { | ||
animated -> Image.Format.GIF | ||
else -> Image.Format.WEBP | ||
} | ||
|
||
val url: String | ||
get() = cdnUrl.toUrl { | ||
this.format = this@Icon.format | ||
} | ||
|
||
suspend fun getImage(): Image = Image.fromUrl(kord.resources.httpClient, cdnUrl.toUrl()) | ||
|
||
suspend fun getImage(size: Image.Size): Image = | ||
Image.fromUrl(kord.resources.httpClient, cdnUrl.toUrl { | ||
this.size = size | ||
}) | ||
|
||
suspend fun getImage(format: Image.Format): Image = | ||
Image.fromUrl(kord.resources.httpClient, cdnUrl.toUrl { | ||
this.format = format | ||
}) | ||
|
||
suspend fun getImage(format: Image.Format, size: Image.Size): Image = | ||
Image.fromUrl(kord.resources.httpClient, cdnUrl.toUrl { | ||
this.format = format | ||
this.size = size | ||
}) | ||
|
||
override fun toString(): String { | ||
return "Icon(type=${javaClass.name},animated=$animated,cdnUrl=$cdnUrl,kord=$kord)" | ||
} | ||
|
||
class EmojiIcon(animated: Boolean, emojiId: Snowflake, kord: Kord) : Icon(animated, DiscordCdn.emoji(emojiId), kord) | ||
|
||
class DefaultUserAvatar(discriminator: Int, kord: Kord) : Icon(false, DiscordCdn.defaultAvatar(discriminator), kord) | ||
|
||
class UserAvatar(userId: Snowflake, avatarHash: String, kord: Kord) : | ||
Icon(avatarHash.startsWith("a_"), DiscordCdn.userAvatar(userId, avatarHash), kord) | ||
|
||
class MemberAvatar(guildId: Snowflake, userId: Snowflake, avatarHash: String, kord: Kord) : | ||
Icon(avatarHash.startsWith("a_"), DiscordCdn.memberAvatar(guildId, userId, avatarHash), kord) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package dev.kord.rest.route | ||
|
||
import dev.kord.rest.Image | ||
|
||
class CdnUrl(private val rawAssetUri: String) { | ||
|
||
fun toUrl(): String { | ||
return toUrl(UrlFormatBuilder()) | ||
} | ||
|
||
inline fun toUrl(format: UrlFormatBuilder.() -> Unit): String { | ||
val config = UrlFormatBuilder().apply(format) | ||
return toUrl(config) | ||
} | ||
|
||
fun toUrl(config: UrlFormatBuilder): String { | ||
val urlBuilder = StringBuilder(rawAssetUri).append(".").append(config.format.extension) | ||
config.size?.let { urlBuilder.append("?size=").append(it.maxRes) } | ||
return urlBuilder.toString() | ||
} | ||
|
||
override fun toString(): String { | ||
return "CdnUrl(rawAssetUri=$rawAssetUri)" | ||
} | ||
|
||
data class UrlFormatBuilder(var format: Image.Format = Image.Format.WEBP, var size: Image.Size? = null) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dev.kord.rest.route | ||
|
||
import dev.kord.common.entity.Snowflake | ||
|
||
object DiscordCdn { | ||
|
||
private const val BASE_URL = "https://cdn.discordapp.com" | ||
|
||
fun emoji(emojiId: Snowflake): CdnUrl = CdnUrl("$BASE_URL/emojis/${emojiId.asString}") | ||
|
||
fun defaultAvatar(discriminator: Int): CdnUrl = CdnUrl("$BASE_URL/embed/avatars/${discriminator % 5}") | ||
|
||
fun userAvatar(userId: Snowflake, hash: String): CdnUrl = CdnUrl("$BASE_URL/avatars/${userId.asString}/$hash") | ||
|
||
fun memberAvatar(guildId: Snowflake, userId: Snowflake, hash: String) = | ||
CdnUrl("$BASE_URL/guilds/${guildId.asString}/users/${userId.asString}/avatars/$hash") | ||
|
||
} |