Skip to content

Commit

Permalink
About command sections, sort buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
gdude2002 committed Aug 9, 2024
1 parent aa49d50 commit 0ea7648
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -613,8 +613,25 @@ public open class ExtensibleBotBuilder {
/** Your bot's version number. **/
public var version: String? = null

internal val sections: MutableList<Section> = mutableListOf()
internal val buttons: MutableList<Button> = mutableListOf()

/**
* Add a custom about command subcommand, with your own custom content.
*
* If you decide to add sections, Kord Extensions will move the default about command defined in this
* configuration to a subcommand named "general".
*/
public fun section(body: Section.() -> Unit) {
val section = Section()

body(section)

section.validate()

sections.add(section)
}

/**
* Add a custom button to be displayed under your bot's about information.
*
Expand Down Expand Up @@ -693,6 +710,36 @@ public open class ExtensibleBotBuilder {
/** An emoji to show as this button's icon. **/
public var emoji: ReactionEmoji? = null
}

public class Section {
public lateinit var name: String
public lateinit var description: String
public lateinit var messageBuilder: suspend MessageCreateBuilder.() -> Unit

public var bundle: String? = null

public fun message(body: suspend MessageCreateBuilder.() -> Unit) {
messageBuilder = body
}

public fun validate() {
if (
!::name.isInitialized ||
!::description.isInitialized
) {
error("About command sections must contain a name and description.")
}

if (
!::messageBuilder.isInitialized
) {
error(
"About command sections must contain a message builder; use the `message` DSL function to " +
"provide one."
)
}
}
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,59 @@ import dev.kord.rest.builder.message.create.MessageCreateBuilder
import dev.kord.rest.builder.message.embed
import dev.kordex.core.builders.ExtensibleBotBuilder
import dev.kordex.core.commands.CommandContext
import dev.kordex.core.commands.application.slash.ephemeralSubCommand
import dev.kordex.core.extensions.Extension
import dev.kordex.core.extensions.chatCommand
import dev.kordex.core.extensions.chatGroupCommand
import dev.kordex.core.extensions.ephemeralSlashCommand
import org.koin.core.component.inject

@Suppress("StringLiteralDuplication")
public class AboutExtension : Extension() {
override val name: String = "kordex.about"

private val settings: ExtensibleBotBuilder by inject()

override suspend fun setup() {
chatCommand {
name = "extensions.about.commandName"
description = "extensions.about.commandDescription"
if (settings.aboutBuilder.sections.isEmpty()) {
chatCommand {
name = "extensions.about.commandName"
description = "extensions.about.commandDescription"

action {
message.reply {
addAbout(this@action)
}
}
}
} else {
chatGroupCommand {
name = "extensions.about.commandName"
description = "extensions.about.commandDescription"

this.chatCommand {
name = "extensions.about.generalCommandName"
description = "extensions.about.generalCommandDescription"

action {
message.reply {
addAbout(this@action)
}
}
}

action {
message.reply {
addAbout(this@action)
settings.aboutBuilder.sections.forEach { section ->
this.chatCommand {
name = section.name
description = section.description
bundle = section.bundle

action {
message.reply {
section.messageBuilder(this)
}
}
}
}
}
}
Expand All @@ -40,9 +75,36 @@ public class AboutExtension : Extension() {
name = "extensions.about.commandName"
description = "extensions.about.commandDescription"

action {
respond {
addAbout(this@action)
if (settings.aboutBuilder.sections.isNotEmpty()) {
action {
respond {
addAbout(this@action)
}
}
} else {
ephemeralSubCommand {
name = "extensions.about.generalCommandName"
description = "extensions.about.generalCommandDescription"

action {
respond {
addAbout(this@action)
}
}
}

settings.aboutBuilder.sections.forEach { section ->
ephemeralSubCommand {
name = section.name
description = section.description
bundle = section.bundle

action {
respond {
section.messageBuilder(this)
}
}
}
}
}
}
Expand Down Expand Up @@ -89,33 +151,43 @@ public class AboutExtension : Extension() {
else -> context.translate("extensions.about.defaultTitle")
}

if (builder.logoUrl != null) {
thumbnail {
url = builder.logoUrl!!
if (builder.logoUrl != null) {
thumbnail {
url = builder.logoUrl!!
}
}
}

footer {
icon = "https://kordex.dev/logo-transparent.png"
text = context.translate("extensions.about.madeWith") + " • https://kordex.dev"
footer {
icon = "https://kordex.dev/logo-transparent.png"
text = context.translate("extensions.about.madeWith") + " • EUPL v1.2 • https://kordex.dev"
}
}
}

if (builder.buttons.isNotEmpty()) {
actionRow {
if (builder.buttons.isNotEmpty()) {
val names = mutableMapOf<String, String>()

builder.buttons.forEach { button ->
linkButton(button.url) {
label = context.translate(button.name, bundle)
names[button.name] = context.translate(button.name, bundle)
}

actionRow {
builder.buttons
.sortedWith { left, right ->
names[left.name]!!.compareTo(names[right.name]!!, true)
}
.forEach { button ->
linkButton(button.url) {
label = names[button.name]!!

when (val e = button.emoji) {
null -> {} // Nothing
when (val e = button.emoji) {
null -> {} // Nothing

is ReactionEmoji.Custom -> emoji(e)
is ReactionEmoji.Unicode -> emoji(e)
is ReactionEmoji.Custom -> emoji(e)
is ReactionEmoji.Unicode -> emoji(e)
}
}
}
}
}
}
}
}
}
13 changes: 11 additions & 2 deletions test-bot/src/main/kotlin/dev/kordex/test/bot/TestBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,18 @@ public suspend fun main() {
url = "https://kordex.dev"
version = KORDEX_VERSION

docsButton("https://docs.kordex.dev")
donationButton("https://ko-fi.com/gsc")
sourceButton("https://github.com/Kord-Extensions/kord-extensions")
donationButton("https://ko-fi.com/gsc")
docsButton("https://docs.kordex.dev")

section {
name = "banana"
description = "banana"

message {
content = "Banana!"
}
}
}

chatCommands {
Expand Down

0 comments on commit 0ea7648

Please sign in to comment.