Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

run through

This is a presentation I gave at the Creative Code Toronto Sep '24 meetup :: slides :: video

deploy on SidePro

cast.mp4

starter for a discord bot

       ┌────────────────────────────────────────┐
       │            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 expand

Spawn 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.spawn

Add your Discord bot token:

"<token>" | .append discord.ws.token

Register 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.nu

Register dice roll actor:

open examples/discord-bot/actor-roller.nu | .append "discord.roller.register"

Slash commands

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"