-
Notifications
You must be signed in to change notification settings - Fork 927
/
plugin_botrest.go
59 lines (48 loc) · 1.65 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
53
54
55
56
57
58
59
package autorole
import (
"net/http"
"strconv"
"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/v2/bot"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/internalapi"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/mediocregopher/radix/v3"
"goji.io"
"goji.io/pat"
)
var _ internalapi.InternalAPIPlugin = (*Plugin)(nil)
var ErrAlreadyProcessingFullGuild = errors.New("Already processing users on this guild")
func (p *Plugin) InitInternalAPIRoutes(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 {
internalapi.ServerError(w, r, errors.New("unknown server"))
return
}
logger.WithField("guild", parsedGID).Info("autorole doing a full scan")
session := bot.ShardManager.SessionForGuild(parsedGID)
query := ""
session.GatewayManager.RequestGuildMembersComplex(&discordgo.RequestGuildMembersData{
GuildID: parsedGID,
Nonce: strconv.Itoa(int(parsedGID)),
Limit: 0,
Query: &query,
})
internalapi.ServeJson(w, r, "ok")
}
func botRestPostFullScan(guildID int64) error {
var resp string
err := common.RedisPool.Do(radix.Cmd(&resp, "SET", RedisKeyFullScanStatus(guildID), strconv.Itoa(FullScanStarted), "EX", "10", "NX"))
if err != nil {
return errors.WithMessage(err, "r.SET")
}
if resp != "OK" {
return ErrAlreadyProcessingFullGuild
}
err = internalapi.PostWithGuild(guildID, strconv.FormatInt(guildID, 10)+"/autorole/fullscan", nil, nil)
return err
}