From b12e33209042eacc31037e56643d54b2ae092bf1 Mon Sep 17 00:00:00 2001 From: Raphael Campos Date: Sun, 8 Dec 2024 18:26:56 -0300 Subject: [PATCH 1/2] client id inside bot.Bot --- src/discord_gleam.gleam | 33 ++++++++++++++++--------------- src/discord_gleam/types/bot.gleam | 2 +- test/example_bot.gleam | 9 +++++---- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/discord_gleam.gleam b/src/discord_gleam.gleam index 4e99774..83a8aa0 100644 --- a/src/discord_gleam.gleam +++ b/src/discord_gleam.gleam @@ -12,9 +12,14 @@ import discord_gleam/ws/packets/interaction_create import gleam/list import gleam/option -pub fn bot(token: String, intents: intents.Intents) -> bot.Bot { +pub fn bot( + token: String, + client_id: String, + intents: intents.Intents, +) -> bot.Bot { bot.Bot( token: token, + client_id: client_id, intents: intents, cache: bot.Cache(messages: case uset.new("MessagesCache", 1, bravo.Public) { Ok(cache) -> option.Some(cache) @@ -81,39 +86,35 @@ pub fn delete_message( endpoints.delete_message(bot.token, channel_id, message_id, reason) } -pub fn wipe_global_commands( - bot: bot.Bot, - client_id: String, -) -> #(String, String) { - endpoints.wipe_global_commands(bot.token, client_id) +pub fn wipe_global_commands(bot: bot.Bot) -> #(String, String) { + endpoints.wipe_global_commands(bot.token, bot.client_id) } -pub fn wipe_guild_commands( - bot: bot.Bot, - client_id: String, - guild_id: String, -) -> #(String, String) { - endpoints.wipe_guild_commands(bot.token, client_id, guild_id) +pub fn wipe_guild_commands(bot: bot.Bot, guild_id: String) -> #(String, String) { + endpoints.wipe_guild_commands(bot.token, bot.client_id, guild_id) } pub fn register_global_commands( bot: bot.Bot, - client_id: String, commands: List(slash_command.SlashCommand), ) { list.each(commands, fn(command) { - endpoints.register_global_command(bot.token, client_id, command) + endpoints.register_global_command(bot.token, bot.client_id, command) }) } pub fn register_guild_commands( bot: bot.Bot, - client_id: String, guild_id: String, commands: List(slash_command.SlashCommand), ) { list.each(commands, fn(command) { - endpoints.register_guild_command(bot.token, client_id, guild_id, command) + endpoints.register_guild_command( + bot.token, + bot.client_id, + guild_id, + command, + ) }) } diff --git a/src/discord_gleam/types/bot.gleam b/src/discord_gleam/types/bot.gleam index 8a1960d..9f7d3de 100644 --- a/src/discord_gleam/types/bot.gleam +++ b/src/discord_gleam/types/bot.gleam @@ -5,7 +5,7 @@ import discord_gleam/ws/packets/message.{type MessagePacketData} import gleam/option pub type Bot { - Bot(token: String, intents: intents.Intents, cache: Cache) + Bot(token: String, client_id: String, intents: intents.Intents, cache: Cache) } pub type Cache { diff --git a/test/example_bot.gleam b/test/example_bot.gleam index fd13281..a0739a0 100644 --- a/test/example_bot.gleam +++ b/test/example_bot.gleam @@ -16,6 +16,7 @@ pub fn main(token: String, client_id: String, guild_id: String) { let bot = discord_gleam.bot( token, + client_id, intents.Intents(message_content: True, guild_messages: True), ) @@ -49,11 +50,11 @@ pub fn main(token: String, client_id: String, guild_id: String) { ], ) - discord_gleam.wipe_global_commands(bot, client_id) - discord_gleam.register_global_commands(bot, client_id, [test_cmd]) + discord_gleam.wipe_global_commands(bot) + discord_gleam.register_global_commands(bot, [test_cmd]) - discord_gleam.wipe_guild_commands(bot, client_id, guild_id) - discord_gleam.register_guild_commands(bot, client_id, guild_id, [test_cmd2]) + discord_gleam.wipe_guild_commands(bot, guild_id) + discord_gleam.register_guild_commands(bot, guild_id, [test_cmd2]) discord_gleam.run(bot, [event_handler]) } From ec737b788c5f633a30445fb9429ccab9d37e70a1 Mon Sep 17 00:00:00 2001 From: Raphael Campos Date: Sun, 8 Dec 2024 18:40:28 -0300 Subject: [PATCH 2/2] added bot.client_id as snowflake --- src/discord_gleam/types/bot.gleam | 7 +++- test/example_bot.gleam | 67 +++++++++++++++++++------------ 2 files changed, 47 insertions(+), 27 deletions(-) diff --git a/src/discord_gleam/types/bot.gleam b/src/discord_gleam/types/bot.gleam index 9f7d3de..29732be 100644 --- a/src/discord_gleam/types/bot.gleam +++ b/src/discord_gleam/types/bot.gleam @@ -5,7 +5,12 @@ import discord_gleam/ws/packets/message.{type MessagePacketData} import gleam/option pub type Bot { - Bot(token: String, client_id: String, intents: intents.Intents, cache: Cache) + Bot( + token: String, + client_id: Snowflake, + intents: intents.Intents, + cache: Cache, + ) } pub type Cache { diff --git a/test/example_bot.gleam b/test/example_bot.gleam index a0739a0..38b9411 100644 --- a/test/example_bot.gleam +++ b/test/example_bot.gleam @@ -2,6 +2,7 @@ import bravo/uset import discord_gleam import discord_gleam/discord/intents import discord_gleam/event_handler +import discord_gleam/types/bot import discord_gleam/types/message import discord_gleam/types/slash_command import gleam/list @@ -59,7 +60,7 @@ pub fn main(token: String, client_id: String, guild_id: String) { discord_gleam.run(bot, [event_handler]) } -fn event_handler(bot, packet: event_handler.Packet) { +fn event_handler(bot: bot.Bot, packet: event_handler.Packet) { case packet { event_handler.ReadyPacket(ready) -> { logging.log(logging.Info, "Logged in as " <> ready.d.user.username) @@ -68,32 +69,46 @@ fn event_handler(bot, packet: event_handler.Packet) { } event_handler.MessagePacket(message) -> { logging.log(logging.Info, "Message: " <> message.d.content) - case message.d.content { - "!ping" -> { - discord_gleam.send_message(bot, message.d.channel_id, "Pong!", []) - } - "!embed" -> { - let embed1 = - message.Embed( - title: "Embed Title", - description: "Embed Description", - color: 0x00FF00, - ) - - discord_gleam.send_message(bot, message.d.channel_id, "Embed!", [ - embed1, - ]) - } - "!reply" -> { - discord_gleam.reply( - bot, - message.d.channel_id, - message.d.id, - "Reply!", - [], - ) + case message.d.author.id != bot.client_id { + True -> { + case message.d.content { + "!ping" -> { + discord_gleam.send_message(bot, message.d.channel_id, "Pong!", []) + } + "!embed" -> { + let embed1 = + message.Embed( + title: "Embed Title", + description: "Embed Description", + color: 0x00FF00, + ) + + discord_gleam.send_message(bot, message.d.channel_id, "Embed!", [ + embed1, + ]) + } + "!reply" -> { + discord_gleam.reply( + bot, + message.d.channel_id, + message.d.id, + "Reply!", + [], + ) + } + "hello" -> { + discord_gleam.reply( + bot, + message.d.channel_id, + message.d.id, + "hello", + [], + ) + } + _ -> Nil + } } - _ -> Nil + False -> Nil } case string.starts_with(message.d.content, "!kick ") {