forked from lukasl-dev/waterlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.go
72 lines (59 loc) · 1.46 KB
/
option.go
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
package waterlink
import (
"errors"
"strings"
)
var (
ErrShardsNegative = errors.New("total shards cannot be negative")
)
type Option func(client *Client) error
func formatHost(host, normal, secured string) string {
var builder strings.Builder
if !strings.HasPrefix(host, normal+"://") && !strings.HasPrefix(host, secured+"://") {
builder.WriteString(normal)
builder.WriteString("://")
}
if strings.HasPrefix(host, ":") {
builder.WriteString("127.0.0.1")
}
builder.WriteString(host)
return builder.String()
}
// HTTP defines the server's http url address.
func HTTP(host string) Option {
return func(client *Client) error {
client.httpHost = formatHost(host, "http", "https")
return nil
}
}
// WS defines the server's websocket url address.
func WS(host string) Option {
return func(client *Client) error {
client.wsHost = formatHost(host, "ws", "wss")
return nil
}
}
// Password defines the server's password.
func Password(password string) Option {
return func(client *Client) error {
client.password = password
return nil
}
}
// TotalShards defines the total number of shards your bot is operating on.
func TotalShards(totalShards int) Option {
return func(client *Client) error {
if totalShards < 0 {
return ErrShardsNegative
}
client.totalShards = totalShards
return nil
}
}
// UserID defines the bot user's id.
func UserID(userID string) Option {
return func(client *Client) error {
client.userID = userID
return nil
}
}