-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
event handler (only ready and message event)
- Loading branch information
cyteon
committed
Jul 17, 2024
1 parent
0f155a0
commit db2558a
Showing
8 changed files
with
181 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import discord_gleam/event_handler | ||
import discord_gleam/ws/event_loop | ||
|
||
pub fn run(token: String) -> Nil { | ||
event_loop.main(token) | ||
pub fn run( | ||
token: String, | ||
event_handlers: List(event_handler.EventHandler), | ||
) -> Nil { | ||
event_loop.main(token, event_handlers) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import discord_gleam/ws/packets/generic | ||
import discord_gleam/ws/packets/message | ||
import discord_gleam/ws/packets/ready | ||
import gleam/list | ||
import gleam/result | ||
|
||
pub type EventHandler = | ||
fn(Packet) -> Nil | ||
|
||
pub type Packet { | ||
MessagePacket(message.MessagePacket) | ||
ReadyPacket(ready.ReadyPacket) | ||
UnknownPacket(generic.GenericPacket) | ||
} | ||
|
||
pub fn handle_event(msg: String, handlers: List(EventHandler)) -> Nil { | ||
let packet = decode_packet(msg) | ||
|
||
list.each(handlers, fn(handler) { handler(packet) }) | ||
} | ||
|
||
fn decode_packet(msg: String) -> Packet { | ||
let generic_packet = generic.string_to_data(msg) | ||
case generic_packet.t { | ||
"MESSAGE_CREATE" -> | ||
message.string_to_data(msg) | ||
|> result.map(MessagePacket) | ||
|> result.unwrap(UnknownPacket(generic_packet)) | ||
"READY" -> | ||
ready.string_to_data(msg) | ||
|> result.map(ReadyPacket) | ||
|> result.unwrap(UnknownPacket(generic_packet)) | ||
_ -> UnknownPacket(generic_packet) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import gleam/dynamic | ||
import gleam/json | ||
|
||
pub type GenericPacket { | ||
GenericPacket(t: String, s: Int, op: Int) | ||
} | ||
|
||
pub fn string_to_data(encoded: String) -> GenericPacket { | ||
let decoder = | ||
dynamic.decode3( | ||
GenericPacket, | ||
dynamic.field("t", of: dynamic.string), | ||
dynamic.field("s", of: dynamic.int), | ||
dynamic.field("op", of: dynamic.int), | ||
) | ||
|
||
let data = json.decode(from: encoded, using: decoder) | ||
|
||
case data { | ||
Ok(decoded) -> decoded | ||
Error(_) -> GenericPacket("error", 0, 0) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,50 @@ | ||
import gleam/dynamic | ||
import gleam/json | ||
import gleam/result | ||
|
||
pub type MessageAuthor { | ||
MessageAuthor(id: String, username: String) | ||
} | ||
|
||
pub type MessagePacketData { | ||
MessagePacketData(content: String, guild_id: String, channel_id: String) | ||
MessagePacketData( | ||
content: String, | ||
guild_id: String, | ||
channel_id: String, | ||
author: MessageAuthor, | ||
) | ||
} | ||
|
||
pub type MessagePacket { | ||
MessagePacket(t: String, s: Int, op: Int, d: MessagePacketData) | ||
} | ||
|
||
pub fn string_to_data(encoded: String) -> Result(MessagePacket, String) { | ||
let decoder = | ||
dynamic.decode4( | ||
MessagePacket, | ||
dynamic.field("t", of: dynamic.string), | ||
dynamic.field("s", of: dynamic.int), | ||
dynamic.field("op", of: dynamic.int), | ||
dynamic.field( | ||
"d", | ||
of: dynamic.decode4( | ||
MessagePacketData, | ||
dynamic.field("content", of: dynamic.string), | ||
dynamic.field("channel_id", of: dynamic.string), | ||
dynamic.field("guild_id", of: dynamic.string), | ||
dynamic.field( | ||
"author", | ||
of: dynamic.decode2( | ||
MessageAuthor, | ||
dynamic.field("id", of: dynamic.string), | ||
dynamic.field("username", of: dynamic.string), | ||
), | ||
), | ||
), | ||
), | ||
) | ||
|
||
json.decode(from: encoded, using: decoder) | ||
|> result.map_error(fn(_) { "Failed to decode MessagePacket" }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import gleam/dynamic | ||
import gleam/json | ||
import gleam/result | ||
import logging | ||
|
||
pub type ReadyUser { | ||
ReadyUser(username: String) | ||
} | ||
|
||
pub type ReadyData { | ||
ReadyData(v: Int, user: ReadyUser) | ||
} | ||
|
||
pub type ReadyPacket { | ||
ReadyPacket(t: String, s: Int, op: Int, d: ReadyData) | ||
} | ||
|
||
pub fn string_to_data(encoded: String) -> Result(ReadyPacket, String) { | ||
let decoder = | ||
dynamic.decode4( | ||
ReadyPacket, | ||
dynamic.field("t", of: dynamic.string), | ||
dynamic.field("s", of: dynamic.int), | ||
dynamic.field("op", of: dynamic.int), | ||
dynamic.field( | ||
"d", | ||
of: dynamic.decode2( | ||
ReadyData, | ||
dynamic.field("v", of: dynamic.int), | ||
dynamic.field( | ||
"user", | ||
of: dynamic.decode1( | ||
ReadyUser, | ||
dynamic.field("username", of: dynamic.string), | ||
), | ||
), | ||
), | ||
), | ||
) | ||
|
||
json.decode(from: encoded, using: decoder) | ||
|> result.map_error(fn(_) { "Failed to decode MessagePacket" }) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,28 @@ | ||
import discord_gleam | ||
import discord_gleam/event_handler | ||
import logging | ||
|
||
pub fn main(token: String) { | ||
logging.configure() | ||
logging.set_level(logging.Debug) | ||
logging.set_level(logging.Info) | ||
|
||
discord_gleam.run(token) | ||
discord_gleam.run(token, [event_handler]) | ||
} | ||
|
||
fn event_handler(packet: event_handler.Packet) { | ||
case packet { | ||
event_handler.ReadyPacket(ready) -> { | ||
logging.log(logging.Info, "Logged in as " <> ready.d.user.username) | ||
} | ||
event_handler.MessagePacket(message) -> { | ||
logging.log( | ||
logging.Info, | ||
"Received message: '" | ||
<> message.d.content | ||
<> "' from " | ||
<> message.d.author.username, | ||
) | ||
} | ||
_ -> Nil | ||
} | ||
} |