This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
client.v
106 lines (95 loc) · 2.24 KB
/
client.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
module discordv
import log
import term
import time
import eb
import gateway
import rest
// Config struct
pub struct Config {
pub mut:
token string [required]
intents gateway.Intent = gateway.guilds | gateway.guild_messages
shard_count int = 1
userdata voidptr
dispatchers_on_shard int = 1
}
// Client represents a connection to the Discord API
[heap]
pub struct Client {
token string
intents gateway.Intent
pub:
shard_count int
mut:
events &eb.EventBus
shards []&gateway.Shard
pub mut:
rest &rest.REST
log &log.Log
userdata voidptr
}
// Creates a new Discord client
pub fn new(config Config) !&Client {
mut m_log := &log.Log{}
$if dv_debug ? {
m_log.set_level(.debug)
} $else {
m_log.set_level(.warn)
}
mut client := &Client{
token: config.token
intents: config.intents
shard_count: config.shard_count
userdata: config.userdata
events: eb.new()
rest: rest.new(config.token)
log: m_log
}
for i in 0 .. config.shard_count {
mut shard := gateway.new_shard(
token: config.token
intents: config.intents
shard_id: i
shards_in_total: config.shard_count
dispatchers: config.dispatchers_on_shard
) !
shard.log = client.log
$if dv_ws_debug ? {
shard.set_ws_log_level(.debug)
} $else {
shard.set_ws_log_level(.warn)
}
shard.set_reciever(client)
shard.on_dispatch(on_dispatch)
client.shards << shard
}
return client
}
// Creates a websocket connection to Discord
pub fn (mut client Client) run() []thread {
mut shards := []thread{}
for i in 0 .. client.shards.len {
shards << client.shards[i].run()
time.sleep(5 * time.second)
}
return shards
}
fn (mut client Client) shard_index(guild_id string) int {
return (guild_id.int() >> 22) % client.shards.len
}
pub fn (mut client Client) shard(guild_id string) &gateway.Shard {
return client.shards[client.shard_index(guild_id)]
}
pub fn (mut client Client) session_id(guild_id string) string {
mut shard := client.shard(guild_id)
return shard.get_session_id()
}
// Needed for logging purposes
fn prefix(level log.Level, colors_supported bool) string {
if colors_supported {
v := term.bold(term.rgb(95, 155, 230, 'v'))
return term.bright_white('[discord.$v] ')
}
return '[discord.v] '
}