Skip to content

Commit

Permalink
feat: flesh out stateful handlers (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
cablehead authored Aug 31, 2024
1 parent a879f9f commit 7e579b8
Show file tree
Hide file tree
Showing 12 changed files with 784 additions and 258 deletions.
72 changes: 65 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "xs"
version = "0.1.0"
edition = "2021"
version = "0.1.0"

[lib]
name = "xs"
Expand Down Expand Up @@ -47,3 +47,4 @@ assert_cmd = "2.0.14"
duct = "0.13.7"
static_assertions = "1.1.0"
tempfile = "3.10.1"
tracing-subscriber = "0.3.18"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
[![Discord](https://img.shields.io/discord/1182364431435436042?logo=discord)](https://discord.com/invite/YNbScHBHrh)

```
Status: WIP [██████████...... 50%]
Status: WIP [████████████.... 60%] ... (until a v0.1.0 release)
```

## Overview / Sketch
Expand Down
22 changes: 17 additions & 5 deletions examples/discord-bot/README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## starter for a discord bot

Requires:
Required to run:

- https://www.nushell.sh
- https://github.com/vi/websocat
- [scru128-cli](https://github.com/cablehead/scru128-cli)- needed for `scru128-since`

Expand All @@ -17,9 +16,22 @@ In another session:
use xs2.nu *
"websocat "wss://gateway.discord.gg/?v=8&encoding=json" --ping-interval 5 --ping-timeout 10 -E -t | lines" |
.append xs.generator.spawn --meta {topic: "discord" duplex: true}
source ./examples/discord-bot/beat.nu ; .cat | last | do $the_thing (do $the_init) | table -e
.append discord.spawn --meta {duplex: true}
open examples/discord-bot/beat.nu | .append "discord.heartbeat.register" --meta {
stateful: true
initial_state: {
s: null, # sequence number
heartbeat_interval: 0, # 0 means we are offline
last_sent: null,
last_ack: null,
authing: null,
session_id: null,
resume_gateway_url: null,
}
pulse: 1000
}
```


Expand Down
60 changes: 54 additions & 6 deletions examples/discord-bot/beat.nu
Original file line number Diff line number Diff line change
@@ -1,4 +1,48 @@
use op.nu *
const opcode = {
dispatch: 0,
heartbeat: 1,
identify: 2,
presence_update: 3,
voice_update: 4,
resume: 6,
reconnect: 7,
invalid_session: 9,
hello: 10,
heartbeat_ack: 11,
}

def "op heartbeat" [seq?: int] {
{
"op": $opcode.heartbeat,
"d": $seq,
}
}

def "op identify" [token: string, intents: int] {
{
"op": $opcode.identify,
"d": {
token: $token,
intents: $intents,
properties: {
os: (sys host | get name),
browser: "discord.nu",
device: "discord.nu",
},
},
}
}

def "op resume" [token: string, session_id: string, seq: int] {
{
"op": $opcode.resume,
"d": {
token: $token,
session_id: $session_id,
seq: $seq,
},
}
}

def "scru128-since" [$id1, $id2] {
let t1 = ($id1 | scru128 parse | into int)
Expand All @@ -23,14 +67,18 @@ def .send [] {
to json -r | $"($in)\n" | .append "discord.send"
}

let the_thing = {|state|
{|state|
mut state = $state
let frame = $in

if $frame.topic == "xs.pulse" {
# if we're online, but not authed, attempt to auth
# we're not online
if $state.heartbeat_interval == 0 {
return
}

# online, but not authed, attempt to auth
if (($state.heartbeat_interval != 0) and ($state.authing | is-empty)) {
print "sending identify!"
op identify $env.BOT_TOKEN 33281 | .send
return
}
Expand All @@ -47,7 +95,7 @@ let the_thing = {|state|
return
}

let message = $frame | .cas | from json
let message = $frame | .cas $in.hash | from json

match $message {
# hello
Expand Down Expand Up @@ -113,5 +161,5 @@ let the_thing = {|state|
}
}

$state
{ state: $state }
}
17 changes: 0 additions & 17 deletions examples/discord-bot/pump.nu

This file was deleted.

Loading

0 comments on commit 7e579b8

Please sign in to comment.