forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
163 lines (135 loc) · 3.38 KB
/
client.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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package botrest
import (
"bytes"
"encoding/json"
"fmt"
"github.com/jonas747/discordgo"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"net/http"
"net/url"
"strconv"
"sync"
"time"
)
var (
ErrServerError = errors.New("reststate server is having issues")
)
func get(url string, dest interface{}) error {
resp, err := http.Get("http://" + serverAddr + "/" + url)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var errDest string
err := json.NewDecoder(resp.Body).Decode(&errDest)
if err != nil {
return ErrServerError
}
return errors.New(errDest)
}
return errors.WithMessage(json.NewDecoder(resp.Body).Decode(dest), "json.Decode")
}
func post(url string, bodyData interface{}, dest interface{}) error {
var bodyBuf bytes.Buffer
if bodyData != nil {
encoder := json.NewEncoder(&bodyBuf)
err := encoder.Encode(bodyData)
if err != nil {
return err
}
}
resp, err := http.Post("http://"+serverAddr+"/"+url, "application/json", &bodyBuf)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
var errDest string
err := json.NewDecoder(resp.Body).Decode(&errDest)
if err != nil {
return ErrServerError
}
return errors.New(errDest)
}
if dest == nil {
return nil
}
return errors.WithMessage(json.NewDecoder(resp.Body).Decode(dest), "json.Decode")
}
func GetGuild(guildID int64) (g *discordgo.Guild, err error) {
err = get(discordgo.StrID(guildID)+"/guild", &g)
return
}
func GetBotMember(guildID int64) (m *discordgo.Member, err error) {
err = get(discordgo.StrID(guildID)+"/botmember", &m)
return
}
func GetOnlineCount(guildID int64) (c int64, err error) {
err = get(discordgo.StrID(guildID)+"/onlinecount", &c)
return
}
func GetMembers(guildID int64, members ...int64) (m []*discordgo.Member, err error) {
stringed := make([]string, 0, len(members))
for _, v := range members {
stringed = append(stringed, strconv.FormatInt(v, 10))
}
query := url.Values{"users": stringed}
encoded := query.Encode()
err = get(discordgo.StrID(guildID)+"/members?"+encoded, &m)
return
}
func GetChannelPermissions(guildID, channelID int64) (perms int64, err error) {
err = get(discordgo.StrID(guildID)+"/channelperms/"+discordgo.StrID(channelID), &perms)
return
}
func GetShardStatuses() (st []*ShardStatus, err error) {
err = get("gw_status", &st)
return
}
func SendReconnectShard(shardID int, reidentify bool) (err error) {
queryParams := ""
if reidentify {
queryParams = "?reidentify=1"
}
err = post(fmt.Sprintf("shard/%d/reconnect"+queryParams, shardID), nil, nil)
return
}
func SendReconnectAll(reidentify bool) (err error) {
queryParams := ""
if reidentify {
queryParams = "?reidentify=1"
}
err = post("shard/*/reconnect"+queryParams, nil, nil)
return
}
var (
lastPing time.Time
lastPingMutex sync.RWMutex
)
func RunPinger() {
lastFailed := false
for {
time.Sleep(time.Second)
var dest string
err := get("ping", &dest)
if err != nil {
if !lastFailed {
logrus.Warn("Ping failed: ", err)
lastFailed = true
}
continue
}
lastPingMutex.Lock()
lastPing = time.Now()
lastPingMutex.Unlock()
lastFailed = false
}
}
// Returns wether the bot is running or not, (time since last sucessfull ping was less than 5 seconds)
func BotIsRunning() bool {
lastPingMutex.RLock()
defer lastPingMutex.RUnlock()
return time.Since(lastPing) < time.Second*5
}