From 035d7d3c8130e7d635f8911f9a00284cef179ad7 Mon Sep 17 00:00:00 2001 From: Gareth Coles Date: Sun, 25 Feb 2024 11:22:44 +0000 Subject: [PATCH] Small cleanups --- .../components/ComponentContainer.kt | 458 +++++++++--------- .../kord/extensions/components/_Functions.kt | 360 +++++++------- 2 files changed, 400 insertions(+), 418 deletions(-) diff --git a/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/components/ComponentContainer.kt b/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/components/ComponentContainer.kt index 703aca37b0..2e3365f955 100644 --- a/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/components/ComponentContainer.kt +++ b/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/components/ComponentContainer.kt @@ -12,11 +12,8 @@ package com.kotlindiscord.kord.extensions.components import com.kotlindiscord.kord.extensions.koin.KordExKoinComponent import com.kotlindiscord.kord.extensions.utils.scheduling.Task +import dev.kord.rest.builder.message.MessageBuilder import dev.kord.rest.builder.message.actionRow -import dev.kord.rest.builder.message.create.MessageCreateBuilder -import dev.kord.rest.builder.message.create.actionRow -import dev.kord.rest.builder.message.modify.MessageModifyBuilder -import dev.kord.rest.builder.message.modify.actionRow import kotlinx.coroutines.runBlocking import org.koin.core.component.inject import kotlin.time.Duration @@ -40,253 +37,240 @@ public const val ROW_SIZE: Int = 5 * @param startTimeoutNow Whether to start the timeout immediately. */ public open class ComponentContainer( - public val timeout: Duration? = null, - startTimeoutNow: Boolean = false, + public val timeout: Duration? = null, + startTimeoutNow: Boolean = false, ) : KordExKoinComponent { - internal val registry: ComponentRegistry by inject() + internal val registry: ComponentRegistry by inject() - /** If a [timeout] was provided, the scheduled timeout task will be stored here. **/ - public open val timeoutTask: Task? = if (timeout != null) { - runBlocking { // This is a trivially quick block, so it should be fine. + /** If a [timeout] was provided, the scheduled timeout task will be stored here. **/ + public open val timeoutTask: Task? = if (timeout != null) { + runBlocking { // This is a trivially quick block, so it should be fine. registry.scheduler.schedule(timeout, startNow = startTimeoutNow) { removeAll() timeoutCallback?.invoke(this@ComponentContainer) } } - } else { - null - } - - /** Extra callback to run when this container times out, if any. **/ - public open var timeoutCallback: (suspend (ComponentContainer).() -> Unit)? = null - - /** Components that haven't been sorted into rows by [sort] yet. **/ - public open val unsortedComponents: MutableList = mutableListOf() - - /** Array containing sorted rows of components. **/ - public open val rows: Array> = arrayOf( - // Up to 5 rows of components - - mutableListOf(), - mutableListOf(), - mutableListOf(), - mutableListOf(), - mutableListOf(), - ) - - /** Register an additional callback to be run when this container times out, assuming a timeout is configured. **/ - public open fun onTimeout(callback: suspend (ComponentContainer).() -> Unit) { - timeoutCallback = callback - } - - /** Remove all components, and unregister them from the [ComponentRegistry]. **/ - public open suspend fun removeAll() { - rows.toList().flatten().forEach { component -> - if (component is ComponentWithID) { - registry.unregister(component) - } - } - - rows.forEach { it.clear() } - } - - /** Remove the given component, and unregister it from the [ComponentRegistry]. **/ - public open suspend fun remove(component: Component): Boolean { - if (rows.any { it.remove(component) }) { - if (component is ComponentWithID) { - registry.unregister(component) - } - - return true - } - - return false - } - - /** Given two components, replace the old component with the new one and likewise handle registration. **/ - public open suspend fun replace(old: Component, new: Component): Boolean { - for (row in rows) { - val index = row.indexOf(old) - - if (index == -1) { - continue - } - - @Suppress("UnnecessaryParentheses") // Yeah, but let me be paranoid. Please. - val freeSlots = (ROW_SIZE - rowWidth(row)) + old.unitWidth - - if (new.unitWidth > freeSlots) { - error( - "The given component takes up ${old.unitWidth} slot/s, but its row will only have " + - "$freeSlots available slots remaining." - ) - } - - row[index] = new - - if (new is ComponentWithID) { - registry.register(new) - } - - return true - } - - return false - } - - /** - * Given an old component ID and new component, replace the old component with the new one and likewise handle - * registration. - */ - public open suspend fun replace(id: String, new: Component): Boolean { - for (row in rows) { - val index = row.indexOfFirst { it is ComponentWithID && it.id == id } - - if (index == -1) { - continue - } - - val old = row[index] - val freeSlots = old.unitWidth + (ROW_SIZE - rowWidth(row)) - - if (new.unitWidth > freeSlots) { - error( - "The given component takes up ${old.unitWidth} slots, but its row will only have " + - "$freeSlots available slots remaining." - ) - } - - row[index] = new - - if (new is ComponentWithID) { - registry.register(new) - } - - return true - } - - return false - } - - /** - * Add a component. New components will be unsorted, or placed in the numbered row denoted by [rowNum] if - * possible. - */ - public open suspend fun add(component: Component, rowNum: Int? = null) { - component.validate() - - if (rowNum == null) { - unsortedComponents.add(component) - - return - } - - if (rowNum < 0 || rowNum >= rows.size) { - error("The given row number ($rowNum) must be between 0 to ${rows.size - 1}, inclusive.") - } - - val row = rows[rowNum] - - if (rowWidth(row) >= ROW_SIZE) { - error( - "Row $rowNum is full, no more components can be added to it." - ) - } - - if (rowWidth(row) + component.unitWidth > ROW_SIZE) { - error( - "The given component takes up ${component.unitWidth} slots, but row $rowNum only has " + - "${ROW_SIZE - rowWidth(row)} available slots remaining." - ) - } - - row.add(component) - - if (component is ComponentWithID) { - registry.register(component) - } - } - - /** Sort all components in [unsortedComponents] by packing them into rows as tightly as possible. **/ - public open suspend fun sort() { - while (unsortedComponents.isNotEmpty()) { - val component = unsortedComponents.removeFirst() - var sorted = false - - @Suppress("UnconditionalJumpStatementInLoop") // Yes, but this is nicer to read - for (row in rows) { - if (rowWidth(row) >= ROW_SIZE || rowWidth(row) + component.unitWidth > ROW_SIZE) { - continue - } - - row.add(component) - sorted = true - - break - } - - if (!sorted) { - error( - "Failed to sort components: Couldn't find a row with ${component.unitWidth} empty slots to fit" + - "$component." - ) - } - - if (component is ComponentWithID) { - registry.register(component) - } - } - } - - /** Apply the components in this container to a message that's being created. **/ - public open suspend fun MessageCreateBuilder.applyToMessage() { - sort() - - for (row in rows.filter { it.isNotEmpty() }) { - actionRow { - row.forEach { it.apply(this) } - } - } - } - - /** Apply the components in this container to a message that's being edited. **/ - public open suspend fun MessageModifyBuilder.applyToMessage() { - this.components = mutableListOf() // Clear 'em - - sort() - - for (row in rows.filter { it.isNotEmpty() }) { - actionRow { - row.forEach { it.apply(this) } - } - } - } - - /** - * Cancel the timeout task, and remove all components from this component container. - * - * This is equivalent to timing out this container early, but will not run the supplied [timeoutCallback], if one - * was provided in [onTimeout]. - */ - public open suspend fun cancel() { - timeoutTask?.cancel() - removeAll() - } - - private fun rowWidth(row: List): Int = row.sumOf { it.unitWidth } + } else { + null + } + + /** Extra callback to run when this container times out, if any. **/ + public open var timeoutCallback: (suspend (ComponentContainer).() -> Unit)? = null + + /** Components that haven't been sorted into rows by [sort] yet. **/ + public open val unsortedComponents: MutableList = mutableListOf() + + /** Array containing sorted rows of components. **/ + public open val rows: Array> = arrayOf( + // Up to 5 rows of components + + mutableListOf(), + mutableListOf(), + mutableListOf(), + mutableListOf(), + mutableListOf(), + ) + + /** Register an additional callback to be run when this container times out, assuming a timeout is configured. **/ + public open fun onTimeout(callback: suspend (ComponentContainer).() -> Unit) { + timeoutCallback = callback + } + + /** Remove all components, and unregister them from the [ComponentRegistry]. **/ + public open suspend fun removeAll() { + rows.toList().flatten().forEach { component -> + if (component is ComponentWithID) { + registry.unregister(component) + } + } + + rows.forEach { it.clear() } + } + + /** Remove the given component, and unregister it from the [ComponentRegistry]. **/ + public open suspend fun remove(component: Component): Boolean { + if (rows.any { it.remove(component) }) { + if (component is ComponentWithID) { + registry.unregister(component) + } + + return true + } + + return false + } + + /** Given two components, replace the old component with the new one and likewise handle registration. **/ + public open suspend fun replace(old: Component, new: Component): Boolean { + for (row in rows) { + val index = row.indexOf(old) + + if (index == -1) { + continue + } + + @Suppress("UnnecessaryParentheses") // Yeah, but let me be paranoid. Please. + val freeSlots = (ROW_SIZE - rowWidth(row)) + old.unitWidth + + if (new.unitWidth > freeSlots) { + error( + "The given component takes up ${old.unitWidth} slot/s, but its row will only have " + + "$freeSlots available slots remaining." + ) + } + + row[index] = new + + if (new is ComponentWithID) { + registry.register(new) + } + + return true + } + + return false + } + + /** + * Given an old component ID and new component, replace the old component with the new one and likewise handle + * registration. + */ + public open suspend fun replace(id: String, new: Component): Boolean { + for (row in rows) { + val index = row.indexOfFirst { it is ComponentWithID && it.id == id } + + if (index == -1) { + continue + } + + val old = row[index] + val freeSlots = old.unitWidth + (ROW_SIZE - rowWidth(row)) + + if (new.unitWidth > freeSlots) { + error( + "The given component takes up ${old.unitWidth} slots, but its row will only have " + + "$freeSlots available slots remaining." + ) + } + + row[index] = new + + if (new is ComponentWithID) { + registry.register(new) + } + + return true + } + + return false + } + + /** + * Add a component. New components will be unsorted, or placed in the numbered row denoted by [rowNum] if + * possible. + */ + public open suspend fun add(component: Component, rowNum: Int? = null) { + component.validate() + + if (rowNum == null) { + unsortedComponents.add(component) + + return + } + + if (rowNum < 0 || rowNum >= rows.size) { + error("The given row number ($rowNum) must be between 0 to ${rows.size - 1}, inclusive.") + } + + val row = rows[rowNum] + + if (rowWidth(row) >= ROW_SIZE) { + error( + "Row $rowNum is full, no more components can be added to it." + ) + } + + if (rowWidth(row) + component.unitWidth > ROW_SIZE) { + error( + "The given component takes up ${component.unitWidth} slots, but row $rowNum only has " + + "${ROW_SIZE - rowWidth(row)} available slots remaining." + ) + } + + row.add(component) + + if (component is ComponentWithID) { + registry.register(component) + } + } + + /** Sort all components in [unsortedComponents] by packing them into rows as tightly as possible. **/ + public open suspend fun sort() { + while (unsortedComponents.isNotEmpty()) { + val component = unsortedComponents.removeFirst() + var sorted = false + + @Suppress("UnconditionalJumpStatementInLoop") // Yes, but this is nicer to read + for (row in rows) { + if (rowWidth(row) >= ROW_SIZE || rowWidth(row) + component.unitWidth > ROW_SIZE) { + continue + } + + row.add(component) + sorted = true + + break + } + + if (!sorted) { + error( + "Failed to sort components: Couldn't find a row with ${component.unitWidth} empty slots to fit" + + "$component." + ) + } + + if (component is ComponentWithID) { + registry.register(component) + } + } + } + + /** Apply the components in this container to a message that's being created or edited. **/ + public open suspend fun MessageBuilder.applyToMessage() { + sort() + + for (row in rows.filter { it.isNotEmpty() }) { + actionRow { + row.forEach { it.apply(this) } + } + } + } + + /** + * Cancel the timeout task, and remove all components from this component container. + * + * This is equivalent to timing out this container early, but will not run the supplied [timeoutCallback], if one + * was provided in [onTimeout]. + */ + public open suspend fun cancel() { + timeoutTask?.cancel() + removeAll() + } + + private fun rowWidth(row: List): Int = row.sumOf { it.unitWidth } } /** DSL-style factory function to make component containers these by hand easier. **/ @Suppress("FunctionNaming") // It's a factory function, detekt... public suspend fun ComponentContainer( - timeout: Duration? = null, - startTimeoutNow: Boolean = false, - builder: suspend ComponentContainer.() -> Unit, + timeout: Duration? = null, + startTimeoutNow: Boolean = false, + builder: suspend ComponentContainer.() -> Unit, ): ComponentContainer { - val container = ComponentContainer(timeout, startTimeoutNow) + val container = ComponentContainer(timeout, startTimeoutNow) - builder(container) + builder(container) - return container + return container } diff --git a/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/components/_Functions.kt b/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/components/_Functions.kt index fd58c55c4d..c1b86a8c76 100644 --- a/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/components/_Functions.kt +++ b/kord-extensions/src/main/kotlin/com/kotlindiscord/kord/extensions/components/_Functions.kt @@ -19,360 +19,358 @@ import com.kotlindiscord.kord.extensions.components.menus.string.EphemeralString import com.kotlindiscord.kord.extensions.components.menus.string.PublicStringSelectMenu import com.kotlindiscord.kord.extensions.components.menus.user.EphemeralUserSelectMenu import com.kotlindiscord.kord.extensions.components.menus.user.PublicUserSelectMenu +import dev.kord.rest.builder.message.MessageBuilder import dev.kord.rest.builder.message.create.MessageCreateBuilder import dev.kord.rest.builder.message.modify.MessageModifyBuilder import kotlin.time.Duration /** DSL function for creating a disabled button and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.disabledButton( - row: Int? = null, - builder: suspend DisabledInteractionButton.() -> Unit, + row: Int? = null, + builder: suspend DisabledInteractionButton.() -> Unit, ): DisabledInteractionButton { - val component = DisabledInteractionButton() + val component = DisabledInteractionButton() - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral button and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralButton( - row: Int? = null, - builder: suspend EphemeralInteractionButton.() -> Unit, + row: Int? = null, + builder: suspend EphemeralInteractionButton.() -> Unit, ): EphemeralInteractionButton { - val component = EphemeralInteractionButton(timeoutTask) + val component = EphemeralInteractionButton(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral button with modal, and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralButton( - modal: (() -> M)?, - row: Int? = null, - builder: suspend EphemeralInteractionButton.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend EphemeralInteractionButton.() -> Unit, ): EphemeralInteractionButton { - val component = EphemeralInteractionButton(timeoutTask, modal) + val component = EphemeralInteractionButton(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a link button and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.linkButton( - row: Int? = null, - builder: suspend LinkInteractionButton.() -> Unit, + row: Int? = null, + builder: suspend LinkInteractionButton.() -> Unit, ): LinkInteractionButton { - val component = LinkInteractionButton() + val component = LinkInteractionButton() - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public button and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicButton( - row: Int? = null, - builder: suspend PublicInteractionButton.() -> Unit, + row: Int? = null, + builder: suspend PublicInteractionButton.() -> Unit, ): PublicInteractionButton { - val component = PublicInteractionButton(timeoutTask) + val component = PublicInteractionButton(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public button with modal, and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicButton( - modal: (() -> M)?, - row: Int? = null, - builder: suspend PublicInteractionButton.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend PublicInteractionButton.() -> Unit, ): PublicInteractionButton { - val component = PublicInteractionButton(timeoutTask, modal) + val component = PublicInteractionButton(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral string select menu and adding it to the current [ComponentContainer]. **/ @Deprecated( - message = "Deprecated to allow other option types.", - replaceWith = ReplaceWith("this.ephemeralStringSelectMenu(row, builder)") + message = "Deprecated to allow for other menu types.", + replaceWith = ReplaceWith("this.ephemeralStringSelectMenu(row, builder)"), + level = DeprecationLevel.ERROR ) public suspend fun ComponentContainer.ephemeralSelectMenu( - row: Int? = null, - builder: suspend EphemeralStringSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend EphemeralStringSelectMenu.() -> Unit, ): EphemeralStringSelectMenu = ephemeralStringSelectMenu(row, builder) /** DSL function for creating an ephemeral string select menu and adding it to the current [ComponentContainer]. **/ @Deprecated( - message = "Deprecated to allow other option types.", - replaceWith = ReplaceWith("this.ephemeralStringSelectMenu(modal, row, builder)") + message = "Deprecated to allow for other menu types.", + replaceWith = ReplaceWith("this.ephemeralStringSelectMenu(modal, row, builder)"), + level = DeprecationLevel.ERROR ) public suspend fun ComponentContainer.ephemeralSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend EphemeralStringSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend EphemeralStringSelectMenu.() -> Unit, ): EphemeralStringSelectMenu = ephemeralStringSelectMenu(modal, row, builder) /** DSL function for creating an ephemeral string select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralStringSelectMenu( - row: Int? = null, - builder: suspend EphemeralStringSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend EphemeralStringSelectMenu.() -> Unit, ): EphemeralStringSelectMenu { - val component = EphemeralStringSelectMenu(timeoutTask) + val component = EphemeralStringSelectMenu(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral string select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralStringSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend EphemeralStringSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend EphemeralStringSelectMenu.() -> Unit, ): EphemeralStringSelectMenu { - val component = EphemeralStringSelectMenu(timeoutTask, modal) + val component = EphemeralStringSelectMenu(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public string select menu and adding it to the current [ComponentContainer]. **/ @Deprecated( - message = "Deprecated to allow other option types.", - replaceWith = ReplaceWith("this.publicStringSelectMenu(row, builder)") + message = "Deprecated to allow for other menu types.", + replaceWith = ReplaceWith("this.publicStringSelectMenu(row, builder)"), + level = DeprecationLevel.ERROR ) public suspend fun ComponentContainer.publicSelectMenu( - row: Int? = null, - builder: suspend PublicStringSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend PublicStringSelectMenu.() -> Unit, ): PublicStringSelectMenu = publicStringSelectMenu(row, builder) /** DSL function for creating a public string select menu and adding it to the current [ComponentContainer]. **/ @Deprecated( - message = "Deprecated to allow other option types.", - replaceWith = ReplaceWith("this.publicStringSelectMenu(modal, row, builder)") + message = "Deprecated to allow for other menu types.", + replaceWith = ReplaceWith("this.publicStringSelectMenu(modal, row, builder)"), + level = DeprecationLevel.ERROR ) public suspend fun ComponentContainer.publicSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend PublicStringSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend PublicStringSelectMenu.() -> Unit, ): PublicStringSelectMenu = publicStringSelectMenu(modal, row, builder) /** DSL function for creating a public string select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicStringSelectMenu( - row: Int? = null, - builder: suspend PublicStringSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend PublicStringSelectMenu.() -> Unit, ): PublicStringSelectMenu { - val component = PublicStringSelectMenu(timeoutTask) + val component = PublicStringSelectMenu(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public string select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicStringSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend PublicStringSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend PublicStringSelectMenu.() -> Unit, ): PublicStringSelectMenu { - val component = PublicStringSelectMenu(timeoutTask, modal) + val component = PublicStringSelectMenu(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralUserSelectMenu( - row: Int? = null, - builder: suspend EphemeralUserSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend EphemeralUserSelectMenu.() -> Unit, ): EphemeralUserSelectMenu { - val component = EphemeralUserSelectMenu(timeoutTask) + val component = EphemeralUserSelectMenu(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralUserSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend EphemeralUserSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend EphemeralUserSelectMenu.() -> Unit, ): EphemeralUserSelectMenu { - val component = EphemeralUserSelectMenu(timeoutTask, modal) + val component = EphemeralUserSelectMenu(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicUserSelectMenu( - row: Int? = null, - builder: suspend PublicUserSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend PublicUserSelectMenu.() -> Unit, ): PublicUserSelectMenu { - val component = PublicUserSelectMenu(timeoutTask) + val component = PublicUserSelectMenu(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicUserSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend PublicUserSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend PublicUserSelectMenu.() -> Unit, ): PublicUserSelectMenu { - val component = PublicUserSelectMenu(timeoutTask, modal) + val component = PublicUserSelectMenu(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralRoleSelectMenu( - row: Int? = null, - builder: suspend EphemeralRoleSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend EphemeralRoleSelectMenu.() -> Unit, ): EphemeralRoleSelectMenu { - val component = EphemeralRoleSelectMenu(timeoutTask) + val component = EphemeralRoleSelectMenu(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralRoleSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend EphemeralRoleSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend EphemeralRoleSelectMenu.() -> Unit, ): EphemeralRoleSelectMenu { - val component = EphemeralRoleSelectMenu(timeoutTask, modal) + val component = EphemeralRoleSelectMenu(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicRoleSelectMenu( - row: Int? = null, - builder: suspend PublicRoleSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend PublicRoleSelectMenu.() -> Unit, ): PublicRoleSelectMenu { - val component = PublicRoleSelectMenu(timeoutTask) + val component = PublicRoleSelectMenu(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicRoleSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend PublicRoleSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend PublicRoleSelectMenu.() -> Unit, ): PublicRoleSelectMenu { - val component = PublicRoleSelectMenu(timeoutTask, modal) + val component = PublicRoleSelectMenu(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralChannelSelectMenu( - row: Int? = null, - builder: suspend EphemeralChannelSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend EphemeralChannelSelectMenu.() -> Unit, ): EphemeralChannelSelectMenu { - val component = EphemeralChannelSelectMenu(timeoutTask) + val component = EphemeralChannelSelectMenu(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating an ephemeral user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.ephemeralChannelSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend EphemeralChannelSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend EphemeralChannelSelectMenu.() -> Unit, ): EphemeralChannelSelectMenu { - val component = EphemeralChannelSelectMenu(timeoutTask, modal) + val component = EphemeralChannelSelectMenu(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicChannelSelectMenu( - row: Int? = null, - builder: suspend PublicChannelSelectMenu.() -> Unit, + row: Int? = null, + builder: suspend PublicChannelSelectMenu.() -> Unit, ): PublicChannelSelectMenu { - val component = PublicChannelSelectMenu(timeoutTask) + val component = PublicChannelSelectMenu(timeoutTask) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** DSL function for creating a public user select menu and adding it to the current [ComponentContainer]. **/ public suspend fun ComponentContainer.publicChannelSelectMenu( - modal: (() -> M)?, - row: Int? = null, - builder: suspend PublicChannelSelectMenu.() -> Unit, + modal: (() -> M)?, + row: Int? = null, + builder: suspend PublicChannelSelectMenu.() -> Unit, ): PublicChannelSelectMenu { - val component = PublicChannelSelectMenu(timeoutTask, modal) + val component = PublicChannelSelectMenu(timeoutTask, modal) - builder(component) - add(component, row) + builder(component) + add(component, row) - return component + return component } /** Convenience function for applying the components in a [ComponentContainer] to a message you're creating. **/ -public suspend fun MessageCreateBuilder.applyComponents(components: ComponentContainer) { - with(components) { - applyToMessage() - } -} - -/** Convenience function for applying the components in a [ComponentContainer] to a message you're editing. **/ -public suspend fun MessageModifyBuilder.applyComponents(components: ComponentContainer) { - with(components) { - applyToMessage() - } +public suspend fun MessageBuilder.applyComponents(components: ComponentContainer) { + with(components) { + applyToMessage() + } } /** @@ -381,14 +379,14 @@ public suspend fun MessageModifyBuilder.applyComponents(components: ComponentCon * of inactivity. */ public suspend fun MessageCreateBuilder.components( - timeout: Duration? = null, - builder: suspend ComponentContainer.() -> Unit, + timeout: Duration? = null, + builder: suspend ComponentContainer.() -> Unit, ): ComponentContainer { - val container = ComponentContainer(timeout, true, builder) + val container = ComponentContainer(timeout, true, builder) - applyComponents(container) + applyComponents(container) - return container + return container } /** @@ -397,12 +395,12 @@ public suspend fun MessageCreateBuilder.components( * of inactivity. */ public suspend fun MessageModifyBuilder.components( - timeout: Duration? = null, - builder: suspend ComponentContainer.() -> Unit, + timeout: Duration? = null, + builder: suspend ComponentContainer.() -> Unit, ): ComponentContainer { - val container = ComponentContainer(timeout, true, builder) + val container = ComponentContainer(timeout, true, builder) - applyComponents(container) + applyComponents(container) - return container + return container }