forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugin_botrest.go
52 lines (42 loc) · 1.45 KB
/
plugin_botrest.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
package autorole
import (
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/bot/botrest"
"github.com/jonas747/yagpdb/common"
"github.com/mediocregopher/radix.v3"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"goji.io"
"goji.io/pat"
"net/http"
"strconv"
)
var _ botrest.BotRestPlugin = (*Plugin)(nil)
var ErrAlreadyProcessingFullGuild = errors.New("Already processing users on this guild")
func (p *Plugin) InitBotRestServer(mux *goji.Mux) {
mux.Handle(pat.Post("/:guild/autorole/fullscan"), http.HandlerFunc(botRestHandleScanFullServer))
}
func botRestHandleScanFullServer(w http.ResponseWriter, r *http.Request) {
guildID := pat.Param(r, "guild")
parsedGID, _ := strconv.ParseInt(guildID, 10, 64)
if parsedGID == 0 {
botrest.ServerError(w, r, errors.New("unknown server"))
return
}
logrus.WithField("guild", parsedGID).Info("autorole doing a full scan")
session := bot.ShardManager.SessionForGuild(parsedGID)
session.GatewayManager.RequestGuildMembers(parsedGID, "", 0)
botrest.ServeJson(w, r, "ok")
}
func botRestPostFullScan(guildID int64) error {
var resp string
err := common.RedisPool.Do(radix.Cmd(&resp, "SET", RedisKeyGuildChunkProecssing(guildID), "1", "EX", "10", "NX"))
if err != nil {
return errors.WithMessage(err, "r.SET")
}
if resp != "OK" {
return ErrAlreadyProcessingFullGuild
}
err = botrest.Post(bot.GuildShardID(guildID), strconv.FormatInt(guildID, 10)+"/autorole/fullscan", nil, nil)
return err
}