Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyteon committed Nov 11, 2024
1 parent 58b1ef6 commit d83a056
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 45 deletions.
16 changes: 7 additions & 9 deletions src/discord_gleam.gleam
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import bravo
import bravo/uset
import discord_gleam/discord/intents
import gleam/option
import discord_gleam/event_handler
import discord_gleam/http/endpoints
import discord_gleam/types/bot
Expand All @@ -9,19 +10,16 @@ import discord_gleam/types/slash_command
import discord_gleam/ws/event_loop
import discord_gleam/ws/packets/interaction_create
import gleam/list
import bravo/uset
import bravo
import gleam/option

pub fn bot(token: String, intents: intents.Intents) -> bot.Bot {
bot.Bot(
token: token,
intents: intents,
cache: bot.Cache(
messages: case uset.new("MessagesCache", 1, bravo.Public) {
Ok(cache) -> option.Some(cache)
Error(_) -> option.None
}
),
cache: bot.Cache(messages: case uset.new("MessagesCache", 1, bravo.Public) {
Ok(cache) -> option.Some(cache)
Error(_) -> option.None
}),
)
}

Expand Down
11 changes: 5 additions & 6 deletions src/discord_gleam/discord/intents.gleam
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import gleam/io

pub type Intents {
Intents(
guild_messages: Bool,
message_content: Bool
)
Intents(guild_messages: Bool, message_content: Bool)
}

pub fn intents_to_bitfield(intents: Intents) -> Int {
let bitfield = 0

let bitfield = case intents.guild_messages {
True -> bitfield + 512 // 1 << 9
True -> bitfield + 512
// 1 << 9
False -> bitfield
}

let bitfield = case intents.message_content {
True -> bitfield + 32768 // 1 << 15
True -> bitfield + 32_768
// 1 << 15
False -> bitfield
}

Expand Down
8 changes: 2 additions & 6 deletions src/discord_gleam/event_handler.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import bravo/uset.{type USet}
import discord_gleam/types/bot
import discord_gleam/ws/packets/generic
import discord_gleam/ws/packets/interaction_create
Expand All @@ -7,7 +8,6 @@ import discord_gleam/ws/packets/ready
import gleam/list
import gleam/option
import gleam/result
import bravo/uset.{type USet}

pub type EventHandler =
fn(bot.Bot, Packet) -> Nil
Expand All @@ -20,11 +20,7 @@ pub type Packet {
InteractionCreate(interaction_create.InteractionCreate)
}

fn internal_handler(
bot: bot.Bot,
packet: Packet,
) -> Nil {

fn internal_handler(bot: bot.Bot, packet: Packet) -> Nil {
case packet {
MessagePacket(msg) -> {
case bot.cache.messages {
Expand Down
15 changes: 4 additions & 11 deletions src/discord_gleam/types/bot.gleam
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
import bravo/uset
import discord_gleam/discord/intents
import discord_gleam/discord/snowflake.{type Snowflake}
import discord_gleam/ws/packets/message .{type MessagePacketData}
import bravo/uset
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, intents: intents.Intents, cache: Cache)
}

pub type Cache {
Cache(
messages: option.Option(uset.USet(#(Snowflake, MessagePacketData))),
)
Cache(messages: option.Option(uset.USet(#(Snowflake, MessagePacketData))))
}
10 changes: 5 additions & 5 deletions src/discord_gleam/ws/event_loop.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ pub fn main(bot: bot.Bot, event_handlers: List(event_handler.EventHandler)) {
let assert Ok(subj) = stratus.initialize(builder)

process.new_selector()
|> process.selecting_process_down(
process.monitor_process(process.subject_owner(subj)),
function.identity,
)
|> process.select_forever
|> process.selecting_process_down(
process.monitor_process(process.subject_owner(subj)),
function.identity,
)
|> process.select_forever

process.sleep(10_000)

Expand Down
20 changes: 12 additions & 8 deletions test/example_bot.gleam
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import discord_gleam/discord/intents
import bravo/uset
import discord_gleam/types/bot
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
import gleam/string
import gleam/option
import gleam/string
import logging

pub fn main(token: String, client_id: String, guild_id: String) {
logging.configure()
logging.set_level(logging.Info)

let bot = discord_gleam.bot(token, intents.Intents(
message_content: True,
guild_messages: True,
))
let bot =
discord_gleam.bot(
token,
intents.Intents(message_content: True, guild_messages: True),
)

let test_cmd =
slash_command.SlashCommand(
Expand Down Expand Up @@ -201,7 +202,10 @@ fn event_handler(bot, packet: event_handler.Packet) {

case msg {
Ok(msg) -> {
logging.log(logging.Info, "Deleted message: " <> msg.1.content)
logging.log(
logging.Info,
"Deleted message: " <> { msg.1 }.content,
)
}
Error(_) -> {
logging.log(logging.Info, "Deleted message not found")
Expand Down

0 comments on commit d83a056

Please sign in to comment.