Skip to content

Commands

Commands in DDBKit come with a bunch of modifiers to control its behaviour. DDBKit includes Discord API modifiers (modifiers that directly control the command during registration, or modifiers that provide callbacks for discord command events). You can find more utilities in the DDBKitUtilities module which provides tools like Message builders and some misc abstractions.

To start, take a look at this command:

Command("embeds") { interaction in
let randomcolor: DiscordColor = {
let colors: [DiscordColor] = [.red, .orange, .yellow, .green, .mint, .teal, .cyan, .blue, .indigo, .purple, .pinks]
return colors.randomElement()!
}()
try? await interaction.respond {
Message {
MessageEmbed {
Title("gm")
Description {
Text("Did i mention how cool this is lmao")
}
}
.setColor(randomcolor)
}
}
}
.description("Test embeds")
.integrationType(.all, contexts: .all)

This demonstrates a few things to note. We use the respond(:) method on our InteractionExtras to send a Message object. The Message object allows you to take a result builder approach to structuring your message payload. You fill the message with MessageComponent types like MessageEmbed as seen here, or MessageContent for the text content of the message. Send attachments with MessageAttachment and attach message components with MessageComponents.

InteractionExtras is a special wrapper type around DiscordBM’s Interaction type. It provides a few extra utilities for handling interactions, such as the respond(:) method and more.

Message {
MessageContent {
Text("hi mom")
}
}

The above example sets up text content in the message, but if you only need to send text, you can skip MessageContent like below:

Message {
Text("hi mom")
}
Message {
MessageContent {
Text("gm")
.bold()
.underlined()
}
MessageAttachment(imgFileData, filename: "img.png")
.usage(.embed)
// the above modifier marks attachment so that it isn't sent as
// an actual attachment, but is included in the msg for use in embeds.
MessageEmbed {
Title("hello chat")
Description("how you doin")
Thumbnail(.attachment(name: "img.png"))
Footer("yea")
}
.setTimestamp() // defaults to current date
}

This demonstrates the process of structuring an embed, and using attachments in embeds or elsewhere. If you wish to have the attachment actually attach to the message rather than be uploaded for use elsewhere, omit the usage modifier.

Command("components") { i in
try? await i.respond {
Message {
MessageContent {
Text("buttons!")
.bold()
.underlined()
}
MessageComponents {
ActionRow {
Button("im a button")
.id("btn")
Button("im dangerous :3")
.style(.danger)
.id("scary")
}
}
}
}
}
.component(on: "btn") { i in
try? await i.respond {
Message {
Text("hii")
}
}
}
.component(on: "scary") { i in
try? await i.respond {
Message {
Text("boo!")
}
.ephemeral()
}
}

This example demonstrates using components!

The component events can be received through the aptly named component modifiers. You can either specify a hardcoded component ID to respond to, or omit the on parameter entirely to receive all events.