Skip to content

Commit 8471d34

Browse files
committed
Add withDisable on ButtonContent/ButtonFactory
1 parent 9797e8c commit 8471d34

File tree

6 files changed

+38
-22
lines changed

6 files changed

+38
-22
lines changed

src/main/kotlin/io/github/freya022/botcommands/api/components/Buttons.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ class Buttons internal constructor(componentController: ComponentController) : A
164164
*/
165165
@CheckReturnValue
166166
fun of(style: ButtonStyle, label: String): ButtonFactory =
167-
ButtonFactory(componentController, style, label, null)
167+
ButtonFactory(componentController, style, label, null, disabled = false)
168168

169169
/**
170170
* Creates a button factory with the style and emoji provided.
@@ -176,7 +176,7 @@ class Buttons internal constructor(componentController: ComponentController) : A
176176
*/
177177
@CheckReturnValue
178178
fun of(style: ButtonStyle, emoji: Emoji): ButtonFactory =
179-
ButtonFactory(componentController, style, null, emoji)
179+
ButtonFactory(componentController, style, null, emoji, disabled = false)
180180

181181
/**
182182
* Creates a button factory with the style, label and emoji provided.
@@ -190,7 +190,7 @@ class Buttons internal constructor(componentController: ComponentController) : A
190190
*/
191191
@CheckReturnValue
192192
fun of(style: ButtonStyle, label: String, emoji: Emoji): ButtonFactory =
193-
ButtonFactory(componentController, style, label, emoji)
193+
ButtonFactory(componentController, style, label, emoji, disabled = false)
194194

195195
/**
196196
* Creates a button factory with the style, label and emoji provided by the [ButtonContent].
@@ -204,7 +204,7 @@ class Buttons internal constructor(componentController: ComponentController) : A
204204
*/
205205
@CheckReturnValue
206206
fun of(content: ButtonContent): ButtonFactory =
207-
ButtonFactory(componentController, content.style, content.label, content.emoji)
207+
ButtonFactory(componentController, content.style, content.label, content.emoji, content.disabled)
208208

209209
/**
210210
* Creates a primary button factory with the label provided.

src/main/kotlin/io/github/freya022/botcommands/api/components/builder/button/ButtonFactory.kt

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ class ButtonFactory internal constructor(
1919
private val componentController: ComponentController,
2020
private val style: ButtonStyle,
2121
private val label: String?,
22-
private val emoji: Emoji?
22+
private val emoji: Emoji?,
23+
private val disabled: Boolean,
2324
) {
2425
init {
2526
require(label != null || emoji != null) { "A label or an emoji needs to be set" }
@@ -58,14 +59,20 @@ class ButtonFactory internal constructor(
5859
EmojiUtils.resolveJDAEmojiOrNull(it) ?: Emoji.fromFormatted(it)
5960
}
6061

61-
return ButtonFactory(componentController, style, label, newEmoji)
62+
return ButtonFactory(componentController, style, label, newEmoji, disabled)
6263
}
6364

6465
/**
6566
* Creates a new button factory with the provided JDA emoji.
6667
*/
6768
@CheckReturnValue
68-
fun withEmoji(emoji: Emoji?): ButtonFactory = ButtonFactory(componentController, style, label, emoji)
69+
fun withEmoji(emoji: Emoji?): ButtonFactory = ButtonFactory(componentController, style, label, emoji, disabled)
70+
71+
/**
72+
* Creates a new button factory with the provided disabled state.
73+
*/
74+
@CheckReturnValue
75+
fun withDisabled(disabled: Boolean): ButtonFactory = ButtonFactory(componentController, style, label, emoji, disabled)
6976

7077
/**
7178
* Creates an ephemeral button builder.
@@ -76,7 +83,7 @@ class ButtonFactory internal constructor(
7683
*/
7784
@CheckReturnValue
7885
fun ephemeral(): EphemeralButtonBuilder =
79-
EphemeralButtonBuilderImpl(componentController, style, label, emoji, InstanceRetriever())
86+
EphemeralButtonBuilderImpl(componentController, style, label, emoji, disabled, InstanceRetriever())
8087

8188
/**
8289
* Creates an ephemeral button.
@@ -98,7 +105,7 @@ class ButtonFactory internal constructor(
98105
*/
99106
@CheckReturnValue
100107
fun persistent(): PersistentButtonBuilder =
101-
PersistentButtonBuilderImpl(componentController, style, label, emoji, InstanceRetriever())
108+
PersistentButtonBuilderImpl(componentController, style, label, emoji, disabled, InstanceRetriever())
102109

103110
/**
104111
* Creates a persistent button.

src/main/kotlin/io/github/freya022/botcommands/api/components/utils/ButtonContent.kt

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import javax.annotation.CheckReturnValue
99
/**
1010
* Represents the visual content of a [Button], this contains at least an [Emoji] or a [String]
1111
*/
12-
data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji: Emoji?) {
12+
data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji: Emoji?, val disabled: Boolean) {
1313
init {
1414
require(label != null || emoji != null) { "A label or an emoji needs to be set" }
1515

@@ -47,38 +47,44 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
4747
EmojiUtils.resolveJDAEmojiOrNull(it) ?: Emoji.fromFormatted(it)
4848
}
4949

50-
return ButtonContent(style, label, newEmoji)
50+
return ButtonContent(style, label, newEmoji, disabled)
5151
}
5252

5353
/**
5454
* Creates a new button content with the provided JDA emoji.
5555
*/
5656
@CheckReturnValue
57-
fun withEmoji(emoji: Emoji?): ButtonContent = ButtonContent(style, label, emoji)
57+
fun withEmoji(emoji: Emoji?): ButtonContent = ButtonContent(style, label, emoji, disabled)
58+
59+
/**
60+
* Creates a new button content with the provided disabled state.
61+
*/
62+
@CheckReturnValue
63+
fun withDisabled(disabled: Boolean): ButtonContent = ButtonContent(style, label, emoji, disabled)
5864

5965
companion object {
6066
/**
6167
* Constructs a [ButtonContent] with a label.
6268
*/
6369
@JvmStatic
6470
fun fromLabel(style: ButtonStyle, label: String): ButtonContent {
65-
return ButtonContent(style, label, null)
71+
return ButtonContent(style, label, null, disabled = false)
6672
}
6773

6874
/**
6975
* Constructs a [ButtonContent] with an [Emoji].
7076
*/
7177
@JvmStatic
7278
fun fromEmoji(style: ButtonStyle, emoji: Emoji): ButtonContent {
73-
return ButtonContent(style, null, emoji)
79+
return ButtonContent(style, null, emoji, disabled = false)
7480
}
7581

7682
/**
7783
* Constructs a [ButtonContent] with a label and an [Emoji].
7884
*/
7985
@JvmStatic
8086
fun fromEmoji(style: ButtonStyle, label: String, emoji: Emoji): ButtonContent {
81-
return ButtonContent(style, label, emoji)
87+
return ButtonContent(style, label, emoji, disabled = false)
8288
}
8389

8490
/**
@@ -87,7 +93,7 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
8793
*/
8894
@JvmStatic
8995
fun fromUnicode(style: ButtonStyle, unicode: String): ButtonContent {
90-
return ButtonContent(style, null, Emoji.fromUnicode(unicode))
96+
return ButtonContent(style, null, Emoji.fromUnicode(unicode), disabled = false)
9197
}
9298

9399
/**
@@ -102,7 +108,7 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
102108
replaceWith = ReplaceWith(expression = "fromEmoji(style, label, Emojis.)", imports = ["dev.freya02.jda.emojis.Emojis"])
103109
)
104110
fun fromUnicode(style: ButtonStyle, label: String, unicode: String): ButtonContent {
105-
return ButtonContent(style, label, Emoji.fromUnicode(unicode))
111+
return ButtonContent(style, label, Emoji.fromUnicode(unicode), disabled = false)
106112
}
107113

108114
/**
@@ -116,7 +122,7 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
116122
replaceWith = ReplaceWith(expression = "fromEmoji(style, Emojis.)", imports = ["dev.freya02.jda.emojis.Emojis"])
117123
)
118124
fun fromShortcode(style: ButtonStyle, shortcode: String): ButtonContent {
119-
return ButtonContent(style, null, EmojiUtils.resolveJDAEmoji(shortcode))
125+
return ButtonContent(style, null, EmojiUtils.resolveJDAEmoji(shortcode), disabled = false)
120126
}
121127

122128
/**
@@ -130,7 +136,7 @@ data class ButtonContent(val style: ButtonStyle, val label: String?, val emoji:
130136
replaceWith = ReplaceWith(expression = "fromEmoji(style, text, Emojis.)", imports = ["dev.freya02.jda.emojis.Emojis"])
131137
)
132138
fun fromShortcode(style: ButtonStyle, text: String, shortcode: String): ButtonContent {
133-
return ButtonContent(style, text, EmojiUtils.resolveJDAEmoji(shortcode))
139+
return ButtonContent(style, text, EmojiUtils.resolveJDAEmoji(shortcode), disabled = false)
134140
}
135141
}
136142
}

src/main/kotlin/io/github/freya022/botcommands/internal/components/builder/button/AbstractButtonBuilder.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal sealed class AbstractButtonBuilder<T : ButtonBuilder<T>>(
1818
private val style: ButtonStyle,
1919
private val label: String?,
2020
private val emoji: Emoji?,
21+
private val disabled: Boolean,
2122
instanceRetriever: InstanceRetriever<T>
2223
) : AbstractComponentBuilder<T>(instanceRetriever),
2324
ButtonBuilder<T> {
@@ -37,7 +38,7 @@ internal sealed class AbstractButtonBuilder<T : ButtonBuilder<T>>(
3738
ButtonImpl(
3839
componentController,
3940
internalId,
40-
JDAButton.of(style, componentId, label, emoji)
41+
JDAButton.of(style, componentId, label, emoji).withDisabled(disabled)
4142
)
4243
}
4344
}

src/main/kotlin/io/github/freya022/botcommands/internal/components/builder/button/EphemeralButtonBuilderImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ internal class EphemeralButtonBuilderImpl internal constructor(
1717
style: ButtonStyle,
1818
label: String?,
1919
emoji: Emoji?,
20+
disabled: Boolean,
2021
instanceRetriever: InstanceRetriever<EphemeralButtonBuilder>
21-
) : AbstractButtonBuilder<EphemeralButtonBuilder>(componentController, style, label, emoji, instanceRetriever),
22+
) : AbstractButtonBuilder<EphemeralButtonBuilder>(componentController, style, label, emoji, disabled, instanceRetriever),
2223
EphemeralButtonBuilder,
2324
IEphemeralActionableComponentMixin<EphemeralButtonBuilder, ButtonEvent> by EphemeralActionableComponentImpl(
2425
componentController.context,

src/main/kotlin/io/github/freya022/botcommands/internal/components/builder/button/PersistentButtonBuilderImpl.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ internal class PersistentButtonBuilderImpl internal constructor(
1616
style: ButtonStyle,
1717
label: String?,
1818
emoji: Emoji?,
19+
disabled: Boolean,
1920
instanceRetriever: InstanceRetriever<PersistentButtonBuilder>
20-
) : AbstractButtonBuilder<PersistentButtonBuilder>(componentController, style, label, emoji, instanceRetriever),
21+
) : AbstractButtonBuilder<PersistentButtonBuilder>(componentController, style, label, emoji, disabled, instanceRetriever),
2122
PersistentButtonBuilder,
2223
IPersistentActionableComponentMixin<PersistentButtonBuilder> by PersistentActionableComponentImpl(
2324
componentController.context,

0 commit comments

Comments
 (0)