Skip to content

Commit

Permalink
messages cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Cyteon committed Nov 11, 2024
1 parent c77139b commit 68f0bfd
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 6 deletions.
1 change: 1 addition & 0 deletions gleam.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ birl = ">= 1.7.1 and < 2.0.0"
logging = ">= 1.3.0 and < 2.0.0"
stratus = ">= 0.9.0 and < 1.0.0"
repeatedly = ">= 2.1.1 and < 3.0.0"
bravo = ">= 4.0.1 and < 5.0.0"

[dev-dependencies]
gleeunit = ">= 1.0.0 and < 2.0.0"
2 changes: 2 additions & 0 deletions manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

packages = [
{ name = "birl", version = "1.7.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "ranger"], otp_app = "birl", source = "hex", outer_checksum = "5C66647D62BCB11FE327E7A6024907C4A17954EF22865FE0940B54A852446D01" },
{ name = "bravo", version = "4.0.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "bravo", source = "hex", outer_checksum = "D450DCD5A896ADDE442A93A7D6B5B2785374579A35416819E29E024887C2A872" },
{ name = "certifi", version = "2.12.0", build_tools = ["rebar3"], requirements = [], otp_app = "certifi", source = "hex", outer_checksum = "EE68D85DF22E554040CDB4BE100F33873AC6051387BAF6A8F6CE82272340FF1C" },
{ name = "dotenv", version = "3.1.0", build_tools = ["mix"], requirements = [], otp_app = "dotenv", source = "hex", outer_checksum = "01BED84D21BEDD8739AEBAD16489A3CE12D19C2D59AF87377DA65EBB361980D3" },
{ name = "gleam_crypto", version = "1.3.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "ADD058DEDE8F0341F1ADE3AAC492A224F15700829D9A3A3F9ADF370F875C51B7" },
Expand All @@ -29,6 +30,7 @@ packages = [

[requirements]
birl = { version = ">= 1.7.1 and < 2.0.0" }
bravo = { version = ">= 4.0.1 and < 5.0.0" }
dotenv = { version = ">= 3.1.0 and < 4.0.0" }
gleam_erlang = { version = ">= 0.25.0 and < 1.0.0" }
gleam_hackney = { version = ">= 1.2.0 and < 2.0.0" }
Expand Down
13 changes: 12 additions & 1 deletion src/discord_gleam.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import gleam/option
import discord_gleam/event_handler
import discord_gleam/http/endpoints
import discord_gleam/types/bot
Expand All @@ -7,9 +8,19 @@ 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

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

pub fn run(
Expand Down
30 changes: 30 additions & 0 deletions src/discord_gleam/event_handler.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import discord_gleam/ws/packets/message
import discord_gleam/ws/packets/message_delete
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 @@ -18,13 +20,41 @@ pub type Packet {
InteractionCreate(interaction_create.InteractionCreate)
}

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

case packet {
MessagePacket(msg) -> {
case bot.cache.messages {
option.Some(cache) -> {
uset.insert(cache, [#(msg.d.id, msg.d)])

Nil
}
option.None -> {
Nil
}
}
Nil
}
_ -> Nil
}
}

pub fn handle_event(
bot: bot.Bot,
msg: String,
handlers: List(EventHandler),
) -> Nil {
let packet = decode_packet(msg)

case packet {
UnknownPacket(_) -> Nil
_ -> internal_handler(bot, packet)
}

list.each(handlers, fn(handler) { handler(bot, packet) })
}

Expand Down
17 changes: 16 additions & 1 deletion src/discord_gleam/types/bot.gleam
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
import discord_gleam/discord/snowflake.{type Snowflake}
import discord_gleam/ws/packets/message .{type MessagePacketData}
import bravo/uset
import gleam/option


pub type Bot {
Bot(token: String)
Bot(
token: String,
cache: Cache,
)
}

pub type Cache {
Cache(
messages: option.Option(uset.USet(#(Snowflake, MessagePacketData))),
)
}
4 changes: 1 addition & 3 deletions src/discord_gleam/ws/event_loop.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import gleam/function
import gleam/http
import gleam/http/request
import gleam/int
import gleam/io
import gleam/option
import gleam/otp/actor
import logging
Expand Down Expand Up @@ -116,8 +115,7 @@ pub fn main(bot: bot.Bot, event_handlers: List(event_handler.EventHandler)) {

let assert Ok(subj) = stratus.initialize(builder)

let done =
process.new_selector()
process.new_selector()
|> process.selecting_process_down(
process.monitor_process(process.subject_owner(subj)),
function.identity,
Expand Down
23 changes: 22 additions & 1 deletion test/example_bot.gleam
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
import bravo/uset
import discord_gleam/types/bot
import discord_gleam
import discord_gleam/event_handler
import discord_gleam/types/message
import discord_gleam/types/slash_command
import gleam/list
import gleam/string
import gleam/option
import logging

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

let bot = discord_gleam.bot(token)

Expand Down Expand Up @@ -187,6 +190,24 @@ fn event_handler(bot, packet: event_handler.Packet) {
}
event_handler.MessageDeletePacket(deleted) -> {
logging.log(logging.Info, "Deleted message: " <> deleted.d.id)

case bot.cache.messages {
option.Some(cache) -> {
let msg = uset.lookup(cache, deleted.d.id)

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

Nil
}
option.None -> Nil
}
Nil
}
event_handler.InteractionCreate(interaction) -> {
Expand Down

0 comments on commit 68f0bfd

Please sign in to comment.