Skip to content

Commit

Permalink
Merge branch 'raphaelantoniocampos-bot-message-author'
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyteon committed Dec 11, 2024
2 parents d5847ff + ec737b7 commit b9f3eb3
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 47 deletions.
33 changes: 17 additions & 16 deletions src/discord_gleam.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
)
})
}

Expand Down
7 changes: 6 additions & 1 deletion src/discord_gleam/types/bot.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ 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: Snowflake,
intents: intents.Intents,
cache: Cache,
)
}

pub type Cache {
Expand Down
76 changes: 46 additions & 30 deletions test/example_bot.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +17,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),
)

Expand Down Expand Up @@ -49,16 +51,16 @@ 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])
}

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)
Expand All @@ -67,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 ") {
Expand Down

0 comments on commit b9f3eb3

Please sign in to comment.