Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bot message author #6

Merged
merged 2 commits into from
Dec 11, 2024
Merged
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
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
Loading