This repository has been archived by the owner on Apr 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vcord.v
79 lines (67 loc) · 1.82 KB
/
vcord.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
module vcord
import net.websocket
import time
import eb
import rest
struct SharedHeartbeatData {
mut:
is_open bool
}
pub struct Bot {
token string [required]
intents Intent [required]
mut:
ws &websocket.Client = voidptr(0)
seq u32
sid string
hb shared SharedHeartbeatData
hb_thread thread ?
hb_last i64
http_endpoint string
resuming bool
user_agent string
pub mut:
latency u32
rest rest.Rest
events eb.EventBus
}
pub struct Config {
pub mut:
token string [required]
intents Intent [required]
}
pub fn new(mut conf Config) ?Bot {
mut bot := Bot{
token: conf.token
intents: conf.intents
ws: voidptr(0)
events: eb.new()
http_endpoint: 'https://discord.com/api/v9'
user_agent: 'DiscordBot (https://github.com/CesiumLabs/valkyria, v0.1.0)'
rest: rest.new(conf.token)
}
create_ws(mut bot) ?
return bot
}
pub fn (mut bot Bot) login() ? {
bot.ws.connect() ?
bot.ws.listen() ?
}
fn (mut this Bot) hb_proc(heartbeat_interval time.Duration) ? {
for {
println('$heartbeat_interval')
time.sleep(heartbeat_interval)
println('heartbeat')
rlock this.hb {
if !this.hb.is_open {
return
}
}
if this.seq == 0 {
this.ws.write_string('{"op":2,"d":null}') ?
} else {
this.ws.write_string('{"op":2,"d":$this.seq}') ?
}
this.hb_last = time.now().unix
}
}