This is a presentation I gave at the Creative Code Toronto Sep '24 meetup :: slides :: video
deploy on SidePro
cast.mp4
┌────────────────────────────────────────┐
│ Discord Gateway │
└────────────▲────────────┳──────────────┘
┃ ┃
s r
e e op: 10 Hello
op: 02 Identify n c op: 00 Ready
op: 01 Heartbeat d v op: 11 Heartbeat ACK
┃ ┃
┌────────┻────────────▼────────┐
━━ stdin ━▶ $ websocat wss://gatewa... ┣━ stdout ━▶
▲└──────────────────────────────┘ │
│ │
discord.ws.send discord.ws.recv
│ │
┌┴──────────────────────────────────▼─┐
│ $ xs serve ./store │
└─────────────────────────────────────┘
Required to run:
Clone the repository:
git clone https://github.com/cablehead/xs.git
cd xs
Start the xs server:
% xs serve ./store
In another session:
Load xs.nu and set store path:
use xs.nu *
$env.XS_ADDR = "./store" | path expandSpawn Discord websocket connection:
r#'{
run: {|| websocat "wss://gateway.discord.gg/?v=10&encoding=json" --ping-interval 5 --ping-timeout 10 -E -t | lines | each { from json } },
duplex: true
}'# | .append discord.ws.spawnAdd your Discord bot token:
"<token>" | .append discord.ws.tokenRegister heartbeat actor for authentication:
open examples/discord-bot/actor-heartbeat.nu | .append "discord.heartbeat.register"At this point, all messages sent to the Discord server will be available on the event stream. I like to say the Discord gateway is now "composed on the stream."
You can build from there, creating actors that act on specific messages. For
example, you could register an actor that looks for messages in the form
./roll 1d4 and responds with a dice roll.
Load Discord REST API module:
http get https://raw.githubusercontent.com/cablehead/discord.nu/main/discord.nu | .append discord.nuRegister dice roll actor:
open examples/discord-bot/actor-roller.nu | .append "discord.roller.register"We should be able to make this nicer?
# create the command
# see discord.nu
discord app command create 1227338584814649364 dice "make a dice roll" --options [
(discord app command option int n "number of dice to roll" --required)
(discord app command option int d "die type / number of sides" --required)
(discord app command option int modifier "modifier")
]
# enable the command actor
open examples/discord-bot/actor-slash-dice.nu | .append "discord.slash-dice.register"