Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: guild commands & ephemeral interaction callback #4

Merged
merged 5 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
TEST_BOT_TOKEN=
TEST_BOT_CLIENT_ID=
TEST_BOT_CLIENT_ID=
TEST_BOT_GUILD_ID=
48 changes: 38 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,37 @@
```sh
gleam add discord_gleam
```

```gleam
import discord_gleam
import discord_gleam/event_handler
import discord_gleam/types/message
import gleam/list
import gleam/string
import logging

pub fn main() {
todo
logging.configure()
logging.set_level(logging.Info)

let bot = discord_gleam.bot("YOUR TOKEN")

discord_gleam.run(bot, [event_handler])
}

fn event_handler(bot, packet: event_handler.Packet) {
case packet {
event_handler.MessagePacket(message) -> {
logging.log(logging.Info, "Message: " <> message.d.content)
case message.d.content {
"!ping" -> {
discord_gleam.send_message(bot, message.d.channel_id, "Pong!", [])
}
_ -> Nil
}
}
_ -> Nil
}
}
```

Expand All @@ -23,19 +49,21 @@ gleam test # Run the tests
```

## Features:
| Feature | Status |
| --- | --- |
| Basic events | ✅ |
| More events | 🔨 |
| Sending messages | ✅ |
| Ban/kick | ✅ |
| Deleting messages | ✅ |
| Embeds | ✅ |
| Basic Slash commands | ✅ |

| Feature | Status |
| -------------------- | ------ |
| Basic events | ✅ |
| More events | 🔨 |
| Sending messages | ✅ |
| Ban/kick | ✅ |
| Deleting messages | ✅ |
| Embeds | ✅ |
| Basic Slash commands | ✅ |

✅ - Done | 🔨 - In Progress | 📆 - Planned | ❌ - Not Planned

## Supported events:

- READY
- MESSAGE_CREATE
- MESSAGE_DELETE
Expand Down
10 changes: 7 additions & 3 deletions examples/slash_commands.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ pub fn main() {
options: [],
)

discord_gleam.register_commands(bot, "YOUR BOT ID", [test_cmd, test_cmd2])
discord_gleam.register_global_commands(bot, "YOUR BOT ID", [test_cmd])

discord_gleam.register_guild_commands(bot, "YOUR BOT ID", "YOUR GUILD ID", [
test_cmd2,
])

discord_gleam.run(bot, [event_handler])
}
Expand All @@ -53,12 +57,12 @@ fn event_handler(bot, packet: event_handler.Packet) {

case interaction.d.data.name {
"ping" -> {
discord_gleam.interaction_reply_message(interaction, "pong")
discord_gleam.interaction_reply_message(interaction, "pong", False)

Nil
}
"pong" -> {
discord_gleam.interaction_reply_message(interaction, "ping")
discord_gleam.interaction_reply_message(interaction, "ping", False)

Nil
}
Expand Down
2 changes: 1 addition & 1 deletion gleam.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = "discord_gleam"
version = "0.0.4"
version = "0.0.5"

# Fill out these fields if you intend to generate HTML documentation or publish
# your project to the Hex package manager.
Expand Down
33 changes: 28 additions & 5 deletions src/discord_gleam.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,46 @@ pub fn delete_message(
endpoints.delete_message(bot.token, channel_id, message_id, reason)
}

pub fn wipe_slash_commands(bot: bot.Bot, client_id: String) -> #(String, String) {
endpoints.wipe_slash_commands(bot.token, client_id)
pub fn wipe_global_commands(
bot: bot.Bot,
client_id: String,
) -> #(String, String) {
endpoints.wipe_global_commands(bot.token, client_id)
}

pub fn wipe_guild_commands(
bot: bot.Bot,
client_id: String,
guild_id: String,
) -> #(String, String) {
endpoints.wipe_guild_commands(bot.token, client_id, guild_id)
}

pub fn register_commands(
pub fn register_global_commands(
bot: bot.Bot,
client_id: String,
commands: List(slash_command.SlashCommand),
) {
list.each(commands, fn(command) {
endpoints.register_slash_command(bot.token, client_id, command)
endpoints.register_global_command(bot.token, client_id, command)
})
}

pub fn register_guild_commands(
bot: bot.Bot,
client_id: String,
guild_id: String,
commands: List(slash_command.SlashCommand),
) {
list.each(commands, fn(command) {
endpoints.register_guild_command(bot.token, client_id, guild_id, command)
})
}

pub fn interaction_reply_message(
interaction: interaction_create.InteractionCreate,
message: String,
ephemeral: Bool,
) -> #(String, String) {
endpoints.interaction_send_text(interaction, message)
endpoints.interaction_send_text(interaction, message, ephemeral)
}
103 changes: 93 additions & 10 deletions src/discord_gleam/http/endpoints.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ pub fn delete_message(
}
}

pub fn wipe_slash_commands(
pub fn wipe_global_commands(
token: String,
client_id: String,
) -> #(String, String) {
Expand All @@ -249,27 +249,64 @@ pub fn wipe_slash_commands(
Ok(resp) -> {
case resp.status {
200 -> {
logging.log(logging.Debug, "Wiped Slash Commands")
logging.log(logging.Debug, "Wiped global commands")
#("OK", resp.body)
}
_ -> {
logging.log(logging.Error, "Failed to wipe Slash Commands")
logging.log(logging.Error, "Failed to wipe global commands")
io.debug(resp.body)

#("FAILED", resp.body)
}
}
}
Error(err) -> {
logging.log(logging.Error, "Failed to wipe Slash Commands")
logging.log(logging.Error, "Failed to wipe global commands")
io.debug(err)

#("FAILED", "ERROR")
}
}
}

pub fn register_slash_command(
pub fn wipe_guild_commands(
token: String,
client_id: String,
guild_id: String,
) -> #(String, String) {
let request =
request.new_auth_post(
http.Put,
"/applications/" <> client_id <> "/guilds/" <> guild_id <> "/commands",
token,
"{}",
)

case hackney.send(request) {
Ok(resp) -> {
case resp.status {
200 -> {
logging.log(logging.Debug, "Wiped guild commands")
#("OK", resp.body)
}
_ -> {
logging.log(logging.Error, "Failed to wipe guild commands")
io.debug(resp.body)

#("FAILED", resp.body)
}
}
}
Error(err) -> {
logging.log(logging.Error, "Failed to wipe guild commands")
io.debug(err)

#("FAILED", "ERROR")
}
}
}

pub fn register_global_command(
token: String,
client_id: String,
command: slash_command.SlashCommand,
Expand All @@ -286,17 +323,62 @@ pub fn register_slash_command(
Ok(resp) -> {
case resp.status {
201 -> {
logging.log(logging.Debug, "Added Slash Command " <> command.name)
logging.log(logging.Debug, "Added global command " <> command.name)
#("OK", resp.body)
}
200 -> {
logging.log(logging.Debug, "Updated global command " <> command.name)
#("OK", resp.body)
}
_ -> {
logging.log(
logging.Error,
"Failed to add global command" <> command.name,
)
io.debug(resp.body)

#("FAILED", resp.body)
}
}
}
Error(err) -> {
logging.log(logging.Error, "Failed to add global command" <> command.name)
io.debug(err)

#("FAILED", "ERROR")
}
}
}

pub fn register_guild_command(
token: String,
client_id: String,
guild_id: String,
command: slash_command.SlashCommand,
) -> #(String, String) {
let request =
request.new_auth_post(
http.Post,
"/applications/" <> client_id <> "/guilds/" <> guild_id <> "/commands",
token,
slash_command.command_to_string(command),
)

case hackney.send(request) {
Ok(resp) -> {
case resp.status {
201 -> {
logging.log(logging.Debug, "Added guild command " <> command.name)
#("OK", resp.body)
}
200 -> {
logging.log(logging.Debug, "Updated Slash Command " <> command.name)
logging.log(logging.Debug, "Updated guild command " <> command.name)
#("OK", resp.body)
}
_ -> {
logging.log(
logging.Error,
"Failed to add Slash Command" <> command.name,
"Failed to add guild command" <> command.name,
)
io.debug(resp.body)

Expand All @@ -305,7 +387,7 @@ pub fn register_slash_command(
}
}
Error(err) -> {
logging.log(logging.Error, "Failed to add Slash Command" <> command.name)
logging.log(logging.Error, "Failed to add guild command" <> command.name)
io.debug(err)

#("FAILED", "ERROR")
Expand All @@ -316,6 +398,7 @@ pub fn register_slash_command(
pub fn interaction_send_text(
interaction: interaction_create.InteractionCreate,
message: String,
ephemeral: Bool,
) -> #(String, String) {
let request =
request.new_post(
Expand All @@ -325,7 +408,7 @@ pub fn interaction_send_text(
<> "/"
<> interaction.d.token
<> "/callback",
slash_command.make_basic_text_reply(message),
slash_command.make_basic_text_reply(message, ephemeral),
)

case hackney.send(request) {
Expand Down
2 changes: 1 addition & 1 deletion src/discord_gleam/http/request.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub fn new(method: http.Method, path: String) -> request.Request(String) {
|> request.prepend_header("accept", "application/json")
|> request.prepend_header(
"User-Agent",
"DiscordBot (https://github.com/cyteon/discord_gleam, 0.0.4)",
"DiscordBot (https://github.com/cyteon/discord_gleam, 0.0.5)",
)
}

Expand Down
9 changes: 7 additions & 2 deletions src/discord_gleam/types/slash_command.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,18 @@ type BasicResponse {
BasicResponse(type_: Int, data: BasicResponseData)
}

pub fn make_basic_text_reply(message: String) -> String {
pub fn make_basic_text_reply(message: String, ephemeral: Bool) -> String {
let data = BasicResponseData(content: message)
let response = BasicResponse(type_: 4, data: data)

let callback_data = case ephemeral {
True -> [#("content", json.string(data.content)), #("flags", json.int(64))]
False -> [#("content", json.string(data.content))]
}

json.object([
#("type", json.int(response.type_)),
#("data", json.object([#("content", json.string(data.content))])),
#("data", json.object(callback_data)),
])
|> json.to_string
}
2 changes: 1 addition & 1 deletion src/discord_gleam/ws/event_loop.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub fn main(bot: bot.Bot, event_handlers: List(event_handler.EventHandler)) {
|> request.set_path("/?v=10&encoding=json")
|> request.set_header(
"User-Agent",
"DiscordBot (https://github.com/cyteon/discord_gleam, 0.0.4)",
"DiscordBot (https://github.com/cyteon/discord_gleam, 0.0.5)",
)
|> request.set_header("Host", "gateway.discord.gg")
|> request.set_header("Connection", "Upgrade")
Expand Down
3 changes: 2 additions & 1 deletion test/discord_gleam_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ pub fn main() {
{
use token <- result.try(get_env("TEST_BOT_TOKEN"))
use client_id <- result.try(get_env("TEST_BOT_CLIENT_ID"))
use guild_id <- result.try(get_env("TEST_BOT_GUILD_ID"))

Ok(example_bot.main(token, client_id))
Ok(example_bot.main(token, client_id, guild_id))
}
{
Ok(_) -> Nil
Expand Down
Loading
Loading