Skip to content

Commit

Permalink
#358 — Stop PK module from accessing cross-posted message data.
Browse files Browse the repository at this point in the history
I don't think PK even handles this type of message. Also adds a small utility.
  • Loading branch information
gdude2002 committed Jan 8, 2025
1 parent 5466c68 commit 8de96ab
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
33 changes: 33 additions & 0 deletions kord-extensions/src/main/kotlin/dev/kordex/core/utils/_Message.kt
Original file line number Diff line number Diff line change
Expand Up @@ -512,3 +512,36 @@ public suspend fun CommandContext.waitForResponse(

return event?.message
}

/**
* Attempt to retrieve the message that the current message is in reply to, if any.
*
* In some situations, such as when dealing with a cross-posted (forwarded) message, Discord may return an HTTP 403.
* This function returns `null` in those situations.
*
* This function also returns `null` when the messages come from different channels, to avoid cross-posted messages.
*
* @return Corresponding [Message] object if found, accessible, and correct; `null` otherwise.
*/
public suspend fun Message.repliedMessageOrNull(): Message? {
val logger = KotlinLogging.logger("dev.kordex.core.utils.repliedMessageOrNull")
val reference = messageReference?.message

if (reference == null) {
return null
}

try {
val newMessage = reference.asMessageOrNull()

if (newMessage == null || newMessage.channelId != channelId) {
return null
}

return newMessage
} catch (e: RestRequestException) {
logger.debug(e) { "Failed to retrieve referenced message (${reference.id}) for reply-message ($id)" }

return null
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import dev.kordex.core.storage.StorageType
import dev.kordex.core.storage.StorageUnit
import dev.kordex.core.utils.MutableStringKeyedMap
import dev.kordex.core.utils.kordExUserAgent
import dev.kordex.core.utils.repliedMessageOrNull
import dev.kordex.core.utils.scheduling.Scheduler
import dev.kordex.core.utils.scheduling.Task
import dev.kordex.modules.pluralkit.api.PluralKit
Expand Down Expand Up @@ -142,7 +143,7 @@ class PKExtension(val config: PKConfigBuilder) : Extension() {
awaitingEvents[message.id] = event
}

val referencedMessage = message.messageReference?.message?.asMessageOrNull()
val referencedMessage = message.repliedMessageOrNull()

if (referencedMessage != null) {
replyCache[message.id] = referencedMessage
Expand Down

0 comments on commit 8de96ab

Please sign in to comment.