Skip to content

Commit

Permalink
sending messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cyteon committed Jul 17, 2024
1 parent db2558a commit f7e09c4
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 20 deletions.
17 changes: 15 additions & 2 deletions src/discord_gleam.gleam
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
import discord_gleam/event_handler
import discord_gleam/http/endpoints
import discord_gleam/types/bot
import discord_gleam/types/message
import discord_gleam/ws/event_loop

pub fn bot(token: String) -> bot.Bot {
bot.Bot(token: token)
}

pub fn run(
token: String,
bot: bot.Bot,
event_handlers: List(event_handler.EventHandler),
) -> Nil {
event_loop.main(token, event_handlers)
event_loop.main(bot, event_handlers)
}

pub fn send_message(bot: bot.Bot, message: String, channel_id: String) -> Nil {
let msg = message.Message(content: message)

endpoints.send_message(bot.token, msg, channel_id)
}
11 changes: 8 additions & 3 deletions src/discord_gleam/event_handler.gleam
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
import discord_gleam/types/bot
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
fn(bot.Bot, Packet) -> Nil

pub type Packet {
MessagePacket(message.MessagePacket)
ReadyPacket(ready.ReadyPacket)
UnknownPacket(generic.GenericPacket)
}

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

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

fn decode_packet(msg: String) -> Packet {
Expand Down
36 changes: 36 additions & 0 deletions src/discord_gleam/http/endpoints.gleam
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import discord_gleam/http/request
import discord_gleam/internal/error
import discord_gleam/types/message
import discord_gleam/types/user
import gleam/dynamic
import gleam/hackney
import gleam/http
import gleam/http/response
import gleam/io
import logging

pub fn me(token: String) -> Result(user.User, error.DiscordError) {
let request = request.new_auth(http.Get, "/users/@me", token)
Expand All @@ -29,3 +32,36 @@ pub fn me(token: String) -> Result(user.User, error.DiscordError) {
Error(err) -> Error(error.HttpError(err))
}
}

pub fn send_message(
token: String,
message: message.Message,
channel_id: String,
) -> Nil {
let data = message.to_string(message)

io.debug(data)

logging.log(logging.Debug, "Sending message: " <> message.content)

let request =
request.new_auth_post(
http.Post,
"/channels/" <> channel_id <> "/messages",
token,
data,
)
case hackney.send(request) {
Ok(resp) -> {
io.debug(resp)

Nil
}
Error(err) -> {
logging.log(logging.Error, "Failed to send message: ")
io.debug(err)

Nil
}
}
}
15 changes: 14 additions & 1 deletion src/discord_gleam/http/request.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn new(method: http.Method, path: String) -> request.Request(String) {
|> request.prepend_header("accept", "application/json")
|> request.prepend_header(
"User-Agent",
"DiscordBot using Gleam and cyteon/discord_gleam",
"DiscordBot (https://github.com/cyteon/discord_gleam, 0.0.0)",
)
}

Expand All @@ -22,3 +22,16 @@ pub fn new_auth(
new(method, path)
|> request.prepend_header("Authorization", "Bot " <> token)
}

/// We have this to send post requests with token authentication
pub fn new_auth_post(
method: http.Method,
path: String,
token: String,
data: String,
) -> request.Request(String) {
new(method, path)
|> request.prepend_header("Authorization", "Bot " <> token)
|> request.set_body(data)
|> request.prepend_header("Content-Type", "application/json")
}
3 changes: 3 additions & 0 deletions src/discord_gleam/types/bot.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub type Bot {
Bot(token: String)
}
10 changes: 10 additions & 0 deletions src/discord_gleam/types/message.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import gleam/json

pub type Message {
Message(content: String)
}

pub fn to_string(msg: Message) -> String {
json.object([#("content", json.string(msg.content))])
|> json.to_string
}
7 changes: 4 additions & 3 deletions src/discord_gleam/ws/event_loop.gleam
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import discord_gleam/event_handler
import discord_gleam/types/bot
import discord_gleam/ws/packets/hello
import discord_gleam/ws/packets/identify
import gleam/erlang/process
Expand All @@ -21,7 +22,7 @@ pub type State {
State(has_received_hello: Bool, sequence: Int)
}

pub fn main(token: String, event_handlers: List(event_handler.EventHandler)) {
pub fn main(bot: bot.Bot, event_handlers: List(event_handler.EventHandler)) {
logging.log(logging.Debug, "Requesting gateway")

let req =
Expand Down Expand Up @@ -54,7 +55,7 @@ pub fn main(token: String, event_handlers: List(event_handler.EventHandler)) {
logging.log(logging.Debug, msg)
case state.has_received_hello {
False -> {
let identify = identify.create_packet(token)
let identify = identify.create_packet(bot.token)
let _ = stratus.send_text_message(conn, identify)

let new_state = State(has_received_hello: True, sequence: 0)
Expand Down Expand Up @@ -83,7 +84,7 @@ pub fn main(token: String, event_handlers: List(event_handler.EventHandler)) {
actor.continue(new_state)
}
True -> {
event_handler.handle_event(msg, event_handlers)
event_handler.handle_event(bot, msg, event_handlers)

actor.continue(state)
}
Expand Down
2 changes: 1 addition & 1 deletion src/discord_gleam/ws/packets/message.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ pub fn string_to_data(encoded: String) -> Result(MessagePacket, String) {
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("channel_id", of: dynamic.string),
dynamic.field(
"author",
of: dynamic.decode2(
Expand Down
22 changes: 12 additions & 10 deletions test/example_bot.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,26 @@ import logging

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

discord_gleam.run(token, [event_handler])
let bot = discord_gleam.bot(token)

discord_gleam.run(bot, [event_handler])
}

fn event_handler(packet: event_handler.Packet) {
fn event_handler(bot, 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,
)
logging.log(logging.Info, "Message: " <> message.d.content)
case message.d.content {
"!ping" -> {
discord_gleam.send_message(bot, "Pong!", message.d.channel_id)
}
_ -> Nil
}
}
_ -> Nil
}
Expand Down

0 comments on commit f7e09c4

Please sign in to comment.