diff --git a/src/glyph/clients/bot.gleam b/src/glyph/clients/bot.gleam index 65221ef..81f789c 100644 --- a/src/glyph/clients/bot.gleam +++ b/src/glyph/clients/bot.gleam @@ -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 { @@ -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"