Skip to content

Commit

Permalink
Merge branch 'oscarfsbs-main'
Browse files Browse the repository at this point in the history
Merge pull request #3
  • Loading branch information
cyteon committed Aug 31, 2024
2 parents f63750e + 71050a2 commit fe73fe7
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/discord_gleam.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import discord_gleam/event_handler
import discord_gleam/http/endpoints
import discord_gleam/types/bot
import discord_gleam/types/message
import discord_gleam/types/reply
import discord_gleam/types/slash_command
import discord_gleam/ws/event_loop
import discord_gleam/ws/packets/interaction_create
Expand Down Expand Up @@ -29,6 +30,19 @@ pub fn send_message(
endpoints.send_message(bot.token, channel_id, msg)
}

pub fn reply(
bot: bot.Bot,
channel_id: String,
message_id: String,
message: String,
embeds: List(message.Embed),
) -> Nil {
let msg =
reply.Reply(content: message, message_id: message_id, embeds: embeds)

endpoints.reply(bot.token, channel_id, msg)
}

pub fn kick_member(
bot: bot.Bot,
guild_id: String,
Expand Down
40 changes: 40 additions & 0 deletions src/discord_gleam/http/endpoints.gleam
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import discord_gleam/http/request
import discord_gleam/internal/error
import discord_gleam/types/message
import discord_gleam/types/reply
import discord_gleam/types/slash_command
import discord_gleam/types/user
import discord_gleam/ws/packets/interaction_create
Expand Down Expand Up @@ -79,6 +80,45 @@ pub fn send_message(
}
}

pub fn reply(token: String, channel_id: String, message: reply.Reply) -> Nil {
let data = reply.to_string(message)
io.debug(data)

logging.log(logging.Debug, "Replying: " <> data)

let request =
request.new_auth_post(
http.Post,
"/channels/" <> channel_id <> "/messages",
token,
data,
)
case hackney.send(request) {
Ok(resp) -> {
case resp.status {
200 -> {
logging.log(logging.Debug, "Reply sent")
Nil
}
_ -> {
logging.log(logging.Error, "Failed to send reply")
io.debug(resp.body)

Nil
}
}

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

Nil
}
}
}

pub fn kick_member(
token: String,
guild_id: String,
Expand Down
28 changes: 28 additions & 0 deletions src/discord_gleam/types/reply.gleam
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import discord_gleam/types/message.{type Embed}
import gleam/json
import gleam/list

pub type Reply {
Reply(content: String, message_id: String, embeds: List(Embed))
}

pub fn to_string(msg: Reply) -> String {
let embeds_json = list.map(msg.embeds, embed_to_json)
json.object([
#("content", json.string(msg.content)),
#("embeds", json.array(embeds_json, of: fn(x) { x })),
#(
"message_reference",
json.object([#("message_id", json.string(msg.message_id))]),
),
])
|> json.to_string
}

pub fn embed_to_json(embed: Embed) -> json.Json {
json.object([
#("title", json.string(embed.title)),
#("description", json.string(embed.description)),
#("color", json.int(embed.color)),
])
}

0 comments on commit fe73fe7

Please sign in to comment.