From 032bc641304f9bb234aafb01f0e808276baedf87 Mon Sep 17 00:00:00 2001 From: HopeBaron Date: Sat, 20 Jun 2020 12:58:10 +0300 Subject: [PATCH] Add mention property for GuildEmoji and ReactionEmoji --- .../kordlib/core/behavior/MessageBehavior.kt | 10 +++++----- .../com/gitlab/kordlib/core/entity/GuildEmoji.kt | 5 ++++- .../gitlab/kordlib/core/entity/ReactionEmoji.kt | 14 +++++++++++--- .../kordlib/core/supplier/RestEntitySupplier.kt | 2 +- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/MessageBehavior.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/MessageBehavior.kt index 0ed7216f4e8..a19b17f59c7 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/MessageBehavior.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/behavior/MessageBehavior.kt @@ -70,7 +70,7 @@ interface MessageBehavior : Entity, Strategizable { kord.rest.channel.getReactions( channelId = channelId.value, messageId = id.value, - emoji = emoji.formatted, + emoji = emoji.urlFormat, limit = 100, position = position ) @@ -82,7 +82,7 @@ interface MessageBehavior : Entity, Strategizable { * @throws [RestRequestException] if something went wrong during the request. */ suspend fun addReaction(emoji: ReactionEmoji) { - kord.rest.channel.createReaction(channelId = channelId.value, messageId = id.value, emoji = emoji.formatted) + kord.rest.channel.createReaction(channelId = channelId.value, messageId = id.value, emoji = emoji.urlFormat) } /** @@ -100,7 +100,7 @@ interface MessageBehavior : Entity, Strategizable { * @throws [RestRequestException] if something went wrong during the request. */ suspend fun deleteReaction(userId: Snowflake, emoji: ReactionEmoji) { - kord.rest.channel.deleteReaction(channelId = channelId.value, messageId = id.value, userId = userId.value, emoji = emoji.formatted) + kord.rest.channel.deleteReaction(channelId = channelId.value, messageId = id.value, userId = userId.value, emoji = emoji.urlFormat) } /** @@ -109,7 +109,7 @@ interface MessageBehavior : Entity, Strategizable { * @throws [RestRequestException] if something went wrong during the request. */ suspend fun deleteOwnReaction(emoji: ReactionEmoji) { - kord.rest.channel.deleteOwnReaction(channelId = channelId.value, messageId = id.value, emoji = emoji.formatted) + kord.rest.channel.deleteOwnReaction(channelId = channelId.value, messageId = id.value, emoji = emoji.urlFormat) } /** @@ -127,7 +127,7 @@ interface MessageBehavior : Entity, Strategizable { * @throws [RestRequestException] if something went wrong during the request. */ suspend fun deleteReaction(emoji: ReactionEmoji) { - kord.rest.channel.deleteAllReactionsForEmoji(channelId = channelId.value, messageId = id.value, emoji = emoji.formatted) + kord.rest.channel.deleteAllReactionsForEmoji(channelId = channelId.value, messageId = id.value, emoji = emoji.urlFormat) } /** diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/entity/GuildEmoji.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/entity/GuildEmoji.kt index 77231a24977..6221de3bcab 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/entity/GuildEmoji.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/entity/GuildEmoji.kt @@ -30,6 +30,9 @@ class GuildEmoji( val guildId: Snowflake get() = Snowflake(data.guildId) + val mention: String + get() = if (isAnimated) "" else "<:$name:${id.value}>" + /** * Whether this emoji can be used, may be false due to loss of Server Boosts. */ @@ -118,7 +121,7 @@ class GuildEmoji( override fun hashCode(): Int = Objects.hash(id, guildId) - override fun equals(other: Any?): Boolean = when(other) { + override fun equals(other: Any?): Boolean = when (other) { is GuildEmoji -> other.id == id && other.guildId == guildId else -> super.equals(other) } diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/entity/ReactionEmoji.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/entity/ReactionEmoji.kt index bbe1daf9a86..c89bf3ee5d3 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/entity/ReactionEmoji.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/entity/ReactionEmoji.kt @@ -4,19 +4,27 @@ import com.gitlab.kordlib.common.entity.Snowflake import com.gitlab.kordlib.core.cache.data.RemovedReactionData sealed class ReactionEmoji { - abstract val formatted: String + abstract val urlFormat: String abstract val name: String data class Custom(val id: Snowflake, override val name: String, val isAnimated: Boolean) : ReactionEmoji() { - override val formatted: String + /** + * + * Format used in HTTP queries. + * + */ + override val urlFormat: String get() = "$name:${id.value}" + val mention: String + get() = if (isAnimated) "" else "<:$name:${id.value}>" + override fun toString() = "Custom(id=$id, name=$name, isAnimated=$isAnimated)" } class Unicode(override val name: String) : ReactionEmoji() { - override val formatted: String get() = name + override val urlFormat: String get() = name } companion object { diff --git a/core/src/main/kotlin/com/gitlab/kordlib/core/supplier/RestEntitySupplier.kt b/core/src/main/kotlin/com/gitlab/kordlib/core/supplier/RestEntitySupplier.kt index f53ff9088ff..a237454e029 100644 --- a/core/src/main/kotlin/com/gitlab/kordlib/core/supplier/RestEntitySupplier.kt +++ b/core/src/main/kotlin/com/gitlab/kordlib/core/supplier/RestEntitySupplier.kt @@ -175,7 +175,7 @@ class RestEntitySupplier(val kord: Kord) : EntitySupplier { kord.rest.channel.getReactions( channelId = channelId.value, messageId = messageId.value, - emoji = emoji.formatted, + emoji = emoji.urlFormat, limit = 100, position = position )