Skip to content

Commit

Permalink
chore: clean up handler-heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
cablehead committed Sep 18, 2024
1 parent c6e5a7a commit 95eba3a
Showing 1 changed file with 62 additions and 71 deletions.
133 changes: 62 additions & 71 deletions examples/discord-bot/handler-heartbeat.nu
Original file line number Diff line number Diff line change
@@ -1,74 +1,6 @@
# op.nu
# we need a mechanism to be able to reuse snippets of code
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,
},
}
}
### end op.nu

def "scru128-since" [$id1, $id2] {
let t1 = ($id1 | scru128 parse | into int)
let t2 = ($id2 | scru128 parse | into int)
return ($t1 - $t2)
}

let the_init = {||
{
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,
}
}

def .send [] {
to json -r | $"($in)\n" | .append "discord.ws.send"
}
# https://discord.com/developers/docs/topics/gateway#list-of-intents
# GUILDS, GUILD_MEMBERS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, MESSAGE_CONTENT
const IDENTIFY_INTENTS = 34307

{|state|
mut state = $state
Expand Down Expand Up @@ -166,3 +98,62 @@ def .send [] {

{ state: $state }
}

# op.nu
# we need a mechanism to be able to reuse snippets of code
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,
},
}
}
### end op.nu

def "scru128-since" [$id1, $id2] {
let t1 = ($id1 | scru128 parse | into int)
let t2 = ($id2 | scru128 parse | into int)
return ($t1 - $t2)
}

def .send [] {
to json -r | $"($in)\n" | .append "discord.ws.send"
}

0 comments on commit 95eba3a

Please sign in to comment.