From 6d65862d53a73c9773329c2d7799ee30f75714f4 Mon Sep 17 00:00:00 2001 From: Taubsie Date: Sat, 8 Jun 2024 10:56:54 +0200 Subject: [PATCH 1/7] Added a method to check if a user is the same as the bot. --- core/src/commonMain/kotlin/entity/User.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core/src/commonMain/kotlin/entity/User.kt b/core/src/commonMain/kotlin/entity/User.kt index 86821c79fe2..5961e7489a3 100644 --- a/core/src/commonMain/kotlin/entity/User.kt +++ b/core/src/commonMain/kotlin/entity/User.kt @@ -37,6 +37,13 @@ public open class User( override val id: Snowflake get() = data.id + /** + * Returns true if the user is the same as the bot. + */ + public val isSelf: Boolean by lazy { + id == kord.selfId + } + public val avatarHash: String? get() = data.avatar /** The avatar of this user as an [Asset]. */ From e7315bfd44000cd89d77a06cb16f7b4eea1470a3 Mon Sep 17 00:00:00 2001 From: Taubsie <50213229+Taubsie@users.noreply.github.com> Date: Sat, 8 Jun 2024 13:44:11 +0200 Subject: [PATCH 2/7] Made the property not lazy anymore Co-authored-by: Michael Rittmeister --- core/src/commonMain/kotlin/entity/User.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/core/src/commonMain/kotlin/entity/User.kt b/core/src/commonMain/kotlin/entity/User.kt index 5961e7489a3..e050f56d053 100644 --- a/core/src/commonMain/kotlin/entity/User.kt +++ b/core/src/commonMain/kotlin/entity/User.kt @@ -40,10 +40,7 @@ public open class User( /** * Returns true if the user is the same as the bot. */ - public val isSelf: Boolean by lazy { - id == kord.selfId - } - +public val isSelf: Boolean get() = id == kord.selfId public val avatarHash: String? get() = data.avatar /** The avatar of this user as an [Asset]. */ From bf3c9ce4b7232f032cba81e3a9becdda6558474a Mon Sep 17 00:00:00 2001 From: Taubsie <50213229+Taubsie@users.noreply.github.com> Date: Sat, 8 Jun 2024 13:46:35 +0200 Subject: [PATCH 3/7] Removed funny indentation Co-authored-by: NoComment <67918617+NoComment1105@users.noreply.github.com> --- core/src/commonMain/kotlin/entity/User.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core/src/commonMain/kotlin/entity/User.kt b/core/src/commonMain/kotlin/entity/User.kt index e050f56d053..05fcc86cfe0 100644 --- a/core/src/commonMain/kotlin/entity/User.kt +++ b/core/src/commonMain/kotlin/entity/User.kt @@ -40,7 +40,8 @@ public open class User( /** * Returns true if the user is the same as the bot. */ -public val isSelf: Boolean get() = id == kord.selfId + public val isSelf: Boolean get() = id == kord.selfId + public val avatarHash: String? get() = data.avatar /** The avatar of this user as an [Asset]. */ From 45280d3c275566631c67bc204e15adf0f8361955 Mon Sep 17 00:00:00 2001 From: Taubsie Date: Sat, 8 Jun 2024 13:47:41 +0200 Subject: [PATCH 4/7] Fixed weird indentation again --- core/src/commonMain/kotlin/entity/User.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/src/commonMain/kotlin/entity/User.kt b/core/src/commonMain/kotlin/entity/User.kt index 05fcc86cfe0..bbcc37af6f8 100644 --- a/core/src/commonMain/kotlin/entity/User.kt +++ b/core/src/commonMain/kotlin/entity/User.kt @@ -40,7 +40,7 @@ public open class User( /** * Returns true if the user is the same as the bot. */ - public val isSelf: Boolean get() = id == kord.selfId + public val isSelf: Boolean get() = id == kord.selfId public val avatarHash: String? get() = data.avatar From 3078eee0861dfa438ba045c61fff6092a19c8ff6 Mon Sep 17 00:00:00 2001 From: Taubsie Date: Sat, 8 Jun 2024 13:54:53 +0200 Subject: [PATCH 5/7] Added missing API dump --- core/api/core.api | 1 + core/api/core.klib.api | 2 ++ 2 files changed, 3 insertions(+) diff --git a/core/api/core.api b/core/api/core.api index 18cd0fb4b20..19c163b67c2 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -7113,6 +7113,7 @@ public class dev/kord/core/entity/User : dev/kord/core/behavior/UserBehavior { public final fun getUsername ()Ljava/lang/String; public fun hashCode ()I public final fun isBot ()Z + public final fun isSelf ()Z public fun toString ()Ljava/lang/String; public synthetic fun withStrategy (Ldev/kord/core/supplier/EntitySupplyStrategy;)Ldev/kord/core/behavior/UserBehavior; public synthetic fun withStrategy (Ldev/kord/core/supplier/EntitySupplyStrategy;)Ldev/kord/core/entity/Strategizable; diff --git a/core/api/core.klib.api b/core/api/core.klib.api index 51b03a3144f..d7aa07aa4db 100644 --- a/core/api/core.klib.api +++ b/core/api/core.klib.api @@ -9761,6 +9761,8 @@ open class dev.kord.core.entity/User : dev.kord.core.behavior/UserBehavior { // final fun (): kotlin/String? // dev.kord.core.entity/User.globalName.|(){}[0] final val isBot // dev.kord.core.entity/User.isBot|{}isBot[0] final fun (): kotlin/Boolean // dev.kord.core.entity/User.isBot.|(){}[0] + final val isSelf // dev.kord.core.entity/User.isSelf|{}isSelf[0] + final fun (): kotlin/Boolean // dev.kord.core.entity/User.isSelf.|(){}[0] final val publicFlags // dev.kord.core.entity/User.publicFlags|{}publicFlags[0] final fun (): dev.kord.common.entity/UserFlags? // dev.kord.core.entity/User.publicFlags.|(){}[0] final val tag // dev.kord.core.entity/User.tag|{}tag[0] From 55a3bfe3ad48c6e9986a2b38298bfff314613043 Mon Sep 17 00:00:00 2001 From: Taubsie Date: Sat, 8 Jun 2024 14:09:04 +0200 Subject: [PATCH 6/7] Added a function to load a message by a given message link. --- core/api/core.api | 1 + core/api/core.klib.api | 1 + core/src/commonMain/kotlin/Kord.kt | 34 ++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/core/api/core.api b/core/api/core.api index 19c163b67c2..3560eb402e0 100644 --- a/core/api/core.api +++ b/core/api/core.api @@ -62,6 +62,7 @@ public final class dev/kord/core/Kord : kotlinx/coroutines/CoroutineScope { public static synthetic fun getInvite$default (Ldev/kord/core/Kord;Ljava/lang/String;ZZLdev/kord/common/entity/Snowflake;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; public final fun getInviteOrNull (Ljava/lang/String;ZZLdev/kord/common/entity/Snowflake;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public static synthetic fun getInviteOrNull$default (Ldev/kord/core/Kord;Ljava/lang/String;ZZLdev/kord/common/entity/Snowflake;Lkotlin/coroutines/Continuation;ILjava/lang/Object;)Ljava/lang/Object; + public final fun getMessageByLink (Ljava/lang/String;Lkotlin/coroutines/Continuation;)Ljava/lang/Object; public final fun getNitroStickerPacks ()Lkotlinx/coroutines/flow/Flow; public final fun getRegions ()Lkotlinx/coroutines/flow/Flow; public final fun getResources ()Ldev/kord/core/ClientResources; diff --git a/core/api/core.klib.api b/core/api/core.klib.api index d7aa07aa4db..10df4d5c9e9 100644 --- a/core/api/core.klib.api +++ b/core/api/core.klib.api @@ -9274,6 +9274,7 @@ final class dev.kord.core/Kord : kotlinx.coroutines/CoroutineScope { // dev.kord final suspend fun getGuildPreviewOrNull(dev.kord.common.entity/Snowflake, dev.kord.core.supplier/EntitySupplyStrategy<*> = ...): dev.kord.core.entity/GuildPreview? // dev.kord.core/Kord.getGuildPreviewOrNull|getGuildPreviewOrNull(dev.kord.common.entity.Snowflake;dev.kord.core.supplier.EntitySupplyStrategy<*>){}[0] final suspend fun getInvite(kotlin/String, kotlin/Boolean = ..., kotlin/Boolean = ..., dev.kord.common.entity/Snowflake? = ...): dev.kord.core.entity/Invite // dev.kord.core/Kord.getInvite|getInvite(kotlin.String;kotlin.Boolean;kotlin.Boolean;dev.kord.common.entity.Snowflake?){}[0] final suspend fun getInviteOrNull(kotlin/String, kotlin/Boolean = ..., kotlin/Boolean = ..., dev.kord.common.entity/Snowflake? = ...): dev.kord.core.entity/Invite? // dev.kord.core/Kord.getInviteOrNull|getInviteOrNull(kotlin.String;kotlin.Boolean;kotlin.Boolean;dev.kord.common.entity.Snowflake?){}[0] + final suspend fun getMessageByLink(kotlin/String): dev.kord.core.entity/Message? // dev.kord.core/Kord.getMessageByLink|getMessageByLink(kotlin.String){}[0] final suspend fun getSelf(dev.kord.core.supplier/EntitySupplyStrategy<*> = ...): dev.kord.core.entity/User // dev.kord.core/Kord.getSelf|getSelf(dev.kord.core.supplier.EntitySupplyStrategy<*>){}[0] final suspend fun getSticker(dev.kord.common.entity/Snowflake): dev.kord.core.entity/Sticker // dev.kord.core/Kord.getSticker|getSticker(dev.kord.common.entity.Snowflake){}[0] final suspend fun getUser(dev.kord.common.entity/Snowflake, dev.kord.core.supplier/EntitySupplyStrategy<*> = ...): dev.kord.core.entity/User? // dev.kord.core/Kord.getUser|getUser(dev.kord.common.entity.Snowflake;dev.kord.core.supplier.EntitySupplyStrategy<*>){}[0] diff --git a/core/src/commonMain/kotlin/Kord.kt b/core/src/commonMain/kotlin/Kord.kt index f116b1b2571..112657aa234 100644 --- a/core/src/commonMain/kotlin/Kord.kt +++ b/core/src/commonMain/kotlin/Kord.kt @@ -6,6 +6,7 @@ import dev.kord.common.annotation.KordUnsafe import dev.kord.common.entity.DiscordShard import dev.kord.common.entity.Snowflake import dev.kord.common.exception.RequestException +import dev.kord.core.behavior.channel.asChannelOfOrNull import dev.kord.core.builder.kord.KordBuilder import dev.kord.core.builder.kord.KordProxyBuilder import dev.kord.core.builder.kord.KordRestOnlyBuilder @@ -15,6 +16,7 @@ import dev.kord.core.cache.data.UserData import dev.kord.core.entity.* import dev.kord.core.entity.application.* import dev.kord.core.entity.channel.Channel +import dev.kord.core.entity.channel.MessageChannel import dev.kord.core.event.Event import dev.kord.core.exception.EntityNotFoundException import dev.kord.core.exception.KordInitializationException @@ -43,6 +45,17 @@ import kotlinx.coroutines.channels.Channel as CoroutineChannel public val kordLogger: mu.KLogger = mu.KotlinLogging.logger { } private val logger = KotlinLogging.logger { } +private val messageLinkRegex = """(?x) # enable comments +| (?i) # allow ignore case +| (?:https?+://)?+ # https:// (or also http:// or an empty string) +| (?:(?:canary|ptb)\\.)?+ # canary. or ptb. +| discord(?:app)?+\\.com/channels/ # discord(app).com/channels/ +| (?:(?[0-9]++)|@me) # @me or the server id +| / # '/' +| (?[0-9]++) # the channel id (should only be a message channel) +| / # '/' +| (?[0-9]++) # the message id +| """.trimMargin().toRegex() @PublishedApi internal fun logCaughtThrowable(throwable: Throwable): Unit = logger.catching(throwable) @@ -258,6 +271,27 @@ public class Kord( strategy: EntitySupplyStrategy<*> = resources.defaultStrategy, ): Guild = strategy.supply(this).getGuild(id) + /** + * Returns the [Message] that is fetched by the given [messageLink]. + * If either the channel or the message id found in the link is invalid, null is returned. + * + * @throws IllegalArgumentException if the message link doesn't match the format found in [messageLinkRegex] + */ + public suspend fun getMessageByLink( + messageLink: String + ): Message? { + val matches = messageLinkRegex.matchEntire(messageLink) + + require(matches != null) { "The message link has an invalid format." } + + val channel = matches.groups["channel"]?.value + ?.let { getChannel(Snowflake(it)) } + ?.asChannelOfOrNull() + + return matches.groups["message"]?.value + ?.let { channel?.getMessageOrNull(Snowflake(it)) } + } + /** * Requests to get the [Webhook] in this guild. * From b039190e8988e7e84ddbea78bce3182284415c00 Mon Sep 17 00:00:00 2001 From: Taubsie Date: Sat, 8 Jun 2024 17:03:36 +0200 Subject: [PATCH 7/7] Fixed formatting issues that caused the regex to not work --- core/src/commonMain/kotlin/Kord.kt | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/core/src/commonMain/kotlin/Kord.kt b/core/src/commonMain/kotlin/Kord.kt index 112657aa234..2e1fc9c37d8 100644 --- a/core/src/commonMain/kotlin/Kord.kt +++ b/core/src/commonMain/kotlin/Kord.kt @@ -45,17 +45,18 @@ import kotlinx.coroutines.channels.Channel as CoroutineChannel public val kordLogger: mu.KLogger = mu.KotlinLogging.logger { } private val logger = KotlinLogging.logger { } -private val messageLinkRegex = """(?x) # enable comments -| (?i) # allow ignore case -| (?:https?+://)?+ # https:// (or also http:// or an empty string) -| (?:(?:canary|ptb)\\.)?+ # canary. or ptb. -| discord(?:app)?+\\.com/channels/ # discord(app).com/channels/ -| (?:(?[0-9]++)|@me) # @me or the server id -| / # '/' -| (?[0-9]++) # the channel id (should only be a message channel) -| / # '/' -| (?[0-9]++) # the message id -| """.trimMargin().toRegex() +private val messageLinkRegex = """ + |(?x) # enable comments + |(?i) # allow ignore case + |(?:https?+://)?+ # https:// (or also http:// or an empty string) + |(?:(?:canary|ptb)\.)?+ # canary. or ptb. + |discord(?:app)?+\.com/channels/ # discord(app).com/channels/ + |(?:(?[0-9]++)|@me) # @me or the server id + |/ # '/' + |(?[0-9]++) # the channel id (should only be a message channel) + |/ # '/' + |(?[0-9]++) # the message id + """.trimMargin().toRegex() @PublishedApi internal fun logCaughtThrowable(throwable: Throwable): Unit = logger.catching(throwable)