Skip to content

Commit

Permalink
example: discord-bot: fix up heartbeat handler
Browse files Browse the repository at this point in the history
  • Loading branch information
cablehead committed Sep 18, 2024
1 parent 9da89bc commit 2f22f91
Showing 1 changed file with 62 additions and 62 deletions.
124 changes: 62 additions & 62 deletions examples/discord-bot/handler-heartbeat.nu
Original file line number Diff line number Diff line change
@@ -1,11 +1,70 @@
# https://discord.com/developers/docs/topics/gateway#list-of-intents
# GUILDS, GUILD_MEMBERS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, MESSAGE_CONTENT
const IDENTIFY_INTENTS = 34307
# 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"
}

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

# https://discord.com/developers/docs/topics/gateway#list-of-intents
# GUILDS, GUILD_MEMBERS, GUILD_MESSAGES, GUILD_MESSAGE_REACTIONS, MESSAGE_CONTENT
let IDENTIFY_INTENTS = 34307

if $frame.topic == "xs.pulse" {
# we're not online
if $state.heartbeat_interval == 0 {
Expand Down Expand Up @@ -98,62 +157,3 @@ const IDENTIFY_INTENTS = 34307

{ 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 2f22f91

Please sign in to comment.