Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/slash_commands.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub fn main() {
description: "string yummy",
type_: slash_command.StringOption,
required: False,
choices: [],
),
],
)
Expand Down
11 changes: 10 additions & 1 deletion src/discord_gleam/types/message.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ pub type Message {
content: String,
embeds: List(embed.Embed),
components: List(component.Component),
ephemeral: Bool,
)
}

/// Create a new message with the given content, no embeds and components are there by default \
/// To add a embed or component use the add_embed and add_component functions
pub fn new(content: String) -> Message {
Message(content: content, embeds: [], components: [])
Message(content: content, embeds: [], components: [], ephemeral: False)
}

/// Add a embed to a message
Expand All @@ -27,6 +28,10 @@ pub fn add_component(msg: Message, component: component.Component) -> Message {
Message(..msg, components: list.append(msg.components, [component]))
}

pub fn set_ephemeral(msg: Message, ephemeral: Bool) -> Message {
Message(..msg, ephemeral: ephemeral)
}

pub fn to_string(msg: Message) -> String {
let embeds_json = list.map(msg.embeds, embed.embed_to_json)
let components_json = list.map(msg.components, component.to_json)
Expand All @@ -35,6 +40,10 @@ pub fn to_string(msg: Message) -> String {
#("content", json.string(msg.content)),
#("embeds", json.array(embeds_json, of: fn(x) { x })),
#("components", json.array(components_json, of: fn(x) { x })),
#("flags", case msg.ephemeral {
True -> json.int(64)
False -> json.null()
}),
])
|> json.to_string
}
23 changes: 21 additions & 2 deletions src/discord_gleam/types/slash_command.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pub type CommandOption {
description: String,
type_: CommandOptionType,
required: Bool,
choices: List(#(String, String)),
)
}

Expand Down Expand Up @@ -59,10 +60,28 @@ pub fn command_to_string(raw: SlashCommand) -> String {
}

pub fn options_to_string(option: CommandOption) -> json.Json {
json.object([
let base = [
#("name", json.string(option.name)),
#("description", json.string(option.description)),
#("type", json.int(type_to_int(option.type_))),
#("required", json.bool(option.required)),
])
]

case option.choices {
[] -> json.object(base)
choices ->
json.object(
list.append(base, [
#(
"choices",
json.array(choices, of: fn(choice) {
json.object([
#("name", json.string(choice.0)),
#("value", json.string(choice.1)),
])
}),
),
]),
)
}
}
4 changes: 4 additions & 0 deletions test/bots/example_bot.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,14 @@ pub fn main() {
description: "Test option",
type_: slash_command.StringOption,
required: False,
choices: [],
),
slash_command.CommandOption(
name: "int",
description: "Test option",
type_: slash_command.IntOption,
required: False,
choices: [],
),
],
)
Expand All @@ -72,12 +74,14 @@ pub fn main() {
description: "Test option",
type_: slash_command.BoolOption,
required: False,
choices: [],
),
slash_command.CommandOption(
name: "float",
description: "Test option",
type_: slash_command.FloatOption,
required: False,
choices: [],
),
],
)
Expand Down
4 changes: 4 additions & 0 deletions test/bots/interaction_bot.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ pub fn main() {
description: "Test option",
type_: slash_command.StringOption,
required: False,
choices: [],
),
slash_command.CommandOption(
name: "int",
description: "Test option",
type_: slash_command.IntOption,
required: False,
choices: [],
),
],
)
Expand All @@ -66,12 +68,14 @@ pub fn main() {
description: "Test option",
type_: slash_command.BoolOption,
required: False,
choices: [],
),
slash_command.CommandOption(
name: "float",
description: "Test option",
type_: slash_command.FloatOption,
required: False,
choices: [],
),
],
)
Expand Down