forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
web.go
77 lines (57 loc) · 1.95 KB
/
web.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
package autorole
import (
"github.com/jonas747/discordgo"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/common/pubsub"
"github.com/jonas747/yagpdb/web"
"github.com/mediocregopher/radix.v3"
"goji.io"
"goji.io/pat"
"html/template"
"net/http"
)
type Form struct {
GeneralConfig `valid:"traverse"`
}
var _ web.SimpleConfigSaver = (*Form)(nil)
func (f Form) Save(guildID int64) error {
pubsub.Publish("autorole_stop_processing", guildID, nil)
err := common.SetRedisJson(KeyGeneral(guildID), f.GeneralConfig)
if err != nil {
return err
}
return nil
}
func (f Form) Name() string {
return "Autorole"
}
func (p *Plugin) InitWeb() {
tmplPathSettings := "templates/plugins/autorole.html"
if common.Testing {
tmplPathSettings = "../../autorole/assets/autorole.html"
}
web.Templates = template.Must(web.Templates.ParseFiles(tmplPathSettings))
muxer := goji.SubMux()
web.CPMux.Handle(pat.New("/autorole"), muxer)
web.CPMux.Handle(pat.New("/autorole/*"), muxer)
muxer.Use(web.RequireFullGuildMW) // need roles
muxer.Use(web.RequireBotMemberMW) // need the bot's role
muxer.Use(web.RequirePermMW(discordgo.PermissionManageRoles))
getHandler := web.RenderHandler(HandleAutoroles, "cp_autorole")
muxer.Handle(pat.Get(""), getHandler)
muxer.Handle(pat.Get("/"), getHandler)
muxer.Handle(pat.Post(""), web.SimpleConfigSaverHandler(Form{}, getHandler))
muxer.Handle(pat.Post("/"), web.SimpleConfigSaverHandler(Form{}, getHandler))
}
func HandleAutoroles(w http.ResponseWriter, r *http.Request) interface{} {
ctx := r.Context()
activeGuild, tmpl := web.GetBaseCPContextData(ctx)
general, err := GetGeneralConfig(activeGuild.ID)
web.CheckErr(tmpl, err, "Failed retrieving general config (contact support)", web.CtxLogger(r.Context()).Error)
tmpl["Autorole"] = general
var proc int
common.RedisPool.Do(radix.Cmd(&proc, "GET", KeyProcessing(activeGuild.ID)))
tmpl["Processing"] = proc
tmpl["ProcessingETA"] = int(proc / 60)
return tmpl
}