-
Notifications
You must be signed in to change notification settings - Fork 906
/
plugin_web.go
148 lines (116 loc) · 5.32 KB
/
plugin_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
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
package moderation
import (
_ "embed"
"fmt"
"html/template"
"net/http"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/cplogs"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/web"
"goji.io"
"goji.io/pat"
)
//go:embed assets/moderation.html
var PageHTML string
var (
panelLogKeyUpdatedSettings = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "moderation_settings_updated", FormatString: "Updated moderation config"})
panelLogKeyClearWarnings = cplogs.RegisterActionFormat(&cplogs.ActionFormat{Key: "moderation_warnings_cleared", FormatString: "Cleared %d moderation user warnings"})
)
func (p *Plugin) InitWeb() {
web.AddHTMLTemplate("moderation/assets/moderation.html", PageHTML)
web.AddSidebarItem(web.SidebarCategoryModeration, &web.SidebarItem{
Name: "Moderation",
URL: "moderation",
Icon: "fas fa-gavel",
})
subMux := goji.SubMux()
web.CPMux.Handle(pat.New("/moderation"), subMux)
web.CPMux.Handle(pat.New("/moderation/*"), subMux)
subMux.Use(web.RequireBotMemberMW) // need the bot's role
subMux.Use(web.RequirePermMW(discordgo.PermissionManageRoles, discordgo.PermissionKickMembers, discordgo.PermissionBanMembers, discordgo.PermissionManageMessages, discordgo.PermissionEmbedLinks, discordgo.PermissionModerateMembers))
getHandler := web.ControllerHandler(HandleModeration, "cp_moderation")
postHandler := web.ControllerPostHandler(HandlePostModeration, getHandler, Config{})
clearServerWarnings := web.ControllerPostHandler(HandleClearServerWarnings, getHandler, nil)
subMux.Handle(pat.Get(""), getHandler)
subMux.Handle(pat.Get("/"), getHandler)
subMux.Handle(pat.Post(""), postHandler)
subMux.Handle(pat.Post("/"), postHandler)
subMux.Handle(pat.Post("/clear_server_warnings"), clearServerWarnings)
}
// HandleModeration servers the moderation page itself
func HandleModeration(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
templateData["DefaultDMMessage"] = DefaultDMMessage
templateData["DefaultTimeoutDuration"] = int(DefaultTimeoutDuration.Minutes())
if _, ok := templateData["ModConfig"]; !ok {
config, err := GetConfig(activeGuild.ID)
if err != nil {
return templateData, err
}
templateData["ModConfig"] = config
}
return templateData, nil
}
// HandlePostModeration update the settings
func HandlePostModeration(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
templateData["VisibleURL"] = "/manage/" + discordgo.StrID(activeGuild.ID) + "/moderation/"
newConfig := ctx.Value(common.ContextKeyParsedForm).(*Config)
newConfig.DefaultMuteDuration.Valid = true
newConfig.DefaultTimeoutDuration.Valid = true
newConfig.DefaultBanDeleteDays.Valid = true
templateData["ModConfig"] = newConfig
err := newConfig.Save(activeGuild.ID)
templateData["DefaultDMMessage"] = DefaultDMMessage
if err == nil {
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyUpdatedSettings))
}
return templateData, err
}
// Clear all server warnigns
func HandleClearServerWarnings(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
ctx := r.Context()
activeGuild, templateData := web.GetBaseCPContextData(ctx)
templateData["VisibleURL"] = "/manage/" + discordgo.StrID(activeGuild.ID) + "/moderation/"
rows := common.GORM.Where("guild_id = ?", activeGuild.ID).Delete(WarningModel{}).RowsAffected
templateData.AddAlerts(web.SucessAlert("Deleted ", rows, " warnings!"))
templateData["DefaultDMMessage"] = DefaultDMMessage
if rows > 0 {
go cplogs.RetryAddEntry(web.NewLogEntryFromContext(r.Context(), panelLogKeyClearWarnings, &cplogs.Param{Type: cplogs.ParamTypeInt, Value: rows}))
}
return templateData, nil
}
var _ web.PluginWithServerHomeWidget = (*Plugin)(nil)
func (p *Plugin) LoadServerHomeWidget(w http.ResponseWriter, r *http.Request) (web.TemplateData, error) {
activeGuild, templateData := web.GetBaseCPContextData(r.Context())
templateData["WidgetTitle"] = "Moderation"
templateData["SettingsPath"] = "/moderation"
config, err := GetConfig(activeGuild.ID)
if err != nil {
return templateData, err
}
format := `<ul>
<li>Report command: %s</li>
<li>Clean command: %s</li>
<li>Giverole/Takerole commands: %s</li>
<li>Kick command: %s</li>
<li>Ban command: %s</li>
<li>Mute/Unmute commands: %s</li>
<li>Timeout/Untimeout commands: %s</li>
<li>Warning commands: %s</li>
</ul>`
if config.ReportEnabled || config.CleanEnabled || config.GiveRoleCmdEnabled || config.ActionChannel != "" ||
config.MuteEnabled || config.KickEnabled || config.BanEnabled || config.WarnCommandsEnabled || config.TimeoutEnabled {
templateData["WidgetEnabled"] = true
} else {
templateData["WidgetDisabled"] = true
}
templateData["WidgetBody"] = template.HTML(fmt.Sprintf(format, web.EnabledDisabledSpanStatus(config.ReportEnabled),
web.EnabledDisabledSpanStatus(config.CleanEnabled), web.EnabledDisabledSpanStatus(config.GiveRoleCmdEnabled),
web.EnabledDisabledSpanStatus(config.KickEnabled), web.EnabledDisabledSpanStatus(config.BanEnabled),
web.EnabledDisabledSpanStatus(config.MuteEnabled), web.EnabledDisabledSpanStatus(config.TimeoutEnabled),
web.EnabledDisabledSpanStatus(config.WarnCommandsEnabled)))
return templateData, nil
}