Skip to content
Open
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
22 changes: 19 additions & 3 deletions src/glyph/clients/bot.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

import gleam/erlang/process
import gleam/int
import gleam/string
import gleam/json
import gleam/list
import gleam/result
import gleam/otp/supervisor
import glyph/models/discord.{type BotClient, type GatewayIntent}
import gleam/result
import gleam/option.{Some}
import glyph/internal/cache
import glyph/internal/encoders
import glyph/internal/decoders
import glyph/internal/encoders
import glyph/internal/network/gateway
import glyph/internal/network/rest
import glyph/models/discord.{type BotClient, type GatewayIntent}
import prng/random

/// Generic bot error
pub type BotError {
Expand Down Expand Up @@ -112,11 +115,24 @@ fn get_gateway_info(bot: BotClient) -> Result(discord.GetGatewayBot, BotError) {
Ok(gateway_info)
}

fn generate_nonce() -> String {
random.int(random.min_int, random.max_int)
|> random.random_sample
|> int.to_string
|> string.slice(at_index: 0, length: 25)
}

/// Send a message to a channel.
///
/// For constructing a message, see [the message builder](https://hexdocs.pm/glyph/glyph/builders/message.html).
/// For constructing an embed, see [the embed builder](https://hexdocs.pm/glyph/glyph/builders/embed.html).
pub fn send(bot: BotClient, channel_id: String, message: discord.MessagePayload) {
let message =
discord.MessagePayload(
..message,
nonce: Some(generate_nonce()),
enforce_nonce: Some(True),
)
let message_json = encoders.message_to_json(message)
let endpoint = "/channels/" <> channel_id <> "/messages"

Expand Down