commandos
🌟 A Solution to Your Discord HTTP Interaction Woes
Commandos is a library which contains an HTTP middleware for handling webhook-based Discord interactions.
Commandos works on all targets, except the browser.
Cool! Why isn’t it in Grom, though?
Commandos depends on Kryptos for cryptographic verification of sent payloads.
Kryptos doesn’t run in a browser. You also wouldn’t run an HTTP server in a browser.
Grom aims to be usable on all targets, including the browser, so Commandos must be in a separate package.
Installation
gleam add commandos
Example
import commandos
import envoy
import gleam/http
import gleam/http/response
// Note: Wisp is just an example here! You can use any other web framework/server,
// like smol.
import wisp
import wisp_ewe
type RequestHandlerContext {
RequestHandlerContext(
// add any other fields you require to properly handle requests
discord_public_key: commandos.PublicKey
)
}
pub fn main() -> Nil {
let assert Ok(discord_public_key) = envoy.get("DISCORD_PUBLIC_KEY")
let assert Ok(discord_public_key) = commandos.public_key_from_string(
discord_public_key
)
wisp.configure_logger()
let http_server =
wisp_ewe.handler(handle_request(_, RequestHandlerContext(discord_public_key)))
|> todo as "carry on creating your http server"
}
fn handle_request(request: Request(wisp.Body), context: RequestHandlerContext) {
use <- wisp.log_request(request)
use <- wisp.rescue_crashes()
case wisp.path_segments(request) {
// or any other endpoint, really
["discord-interactions"] -> {
use <- wisp.require_method(request, http.Post)
use body <- wisp.require_string_body(request)
// commandos requires a `Request(String)`, and wisp returns a `Response(wisp.Body)`
let request = Request(..request, body:)
request
|> commandos.handle_interaction_request(
public_key: context.discord_public_key,
// you shouldn't do lots of blocking code here, just send off your interaction to a
// factory-supervised actor to handle, and return Nil ASAP
//
// this prevents HTTP timeouts
handler: todo as "handle your interaction here!"
)
// wisp requires a `Response(wisp.Body)`, and commandos returns a `Response(String)`
|> response.map(wisp.Text)
}
}
}
How to deploy this thing?
You’ll need:
- a domain and an SSL certificate
- somewhere to host this (serverless, VPS, homelab, etc.)
- a Discord app
Your bot’s public key can be found on your bot’s General Information page in the Discord Developer Portal.
Once your HTTP server is deployed to an internet-facing URL (like https://commandos.folospior.dev/discord-interactions), paste it into the “Interaction Endpoint URL” field in your bot’s General Information page in the Discord Developer Portal.
Further documentation can be found at https://commandos.hexdocs.pm/.