-
Notifications
You must be signed in to change notification settings - Fork 948
/
Copy pathrolecommands.go
266 lines (213 loc) · 5.65 KB
/
rolecommands.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// rolecommands is a plugin which allows users to assign roles to themselves
package rolecommands
//go:generate sqlboiler --no-hooks psql
import (
"context"
"fmt"
"sort"
"strconv"
"github.com/jonas747/discordgo/v2"
"github.com/jonas747/dstate/v4"
"github.com/jonas747/yagpdb/bot"
"github.com/jonas747/yagpdb/commands"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/rolecommands/models"
"github.com/jonas747/yagpdb/web"
"github.com/tidwall/buntdb"
"github.com/volatiletech/sqlboiler/v4/queries/qm"
)
type Plugin struct{}
func (p *Plugin) PluginInfo() *common.PluginInfo {
return &common.PluginInfo{
Name: "RoleCommands",
SysName: "role_commands",
Category: common.PluginCategoryMisc,
}
}
var logger = common.GetPluginLogger(&Plugin{})
const (
GroupModeNone = iota
GroupModeSingle
GroupModeMultiple
)
const (
RoleMenuStateSettingUp = 0
RoleMenuStateDone = 1
RoleMenuStateEditingOptionSelecting = 2
RoleMenuStateEditingOptionReplacing = 3
)
var (
_ common.Plugin = (*Plugin)(nil)
_ web.Plugin = (*Plugin)(nil)
_ bot.BotInitHandler = (*Plugin)(nil)
_ commands.CommandProvider = (*Plugin)(nil)
cooldownsDB *buntdb.DB
)
func RegisterPlugin() {
cooldownsDB, _ = buntdb.Open(":memory:")
p := &Plugin{}
common.RegisterPlugin(p)
common.InitSchemas("rolecommands", DBSchemas...)
}
func FindToggleRole(ctx context.Context, ms *dstate.MemberState, name string) (gaveRole bool, err error) {
cmd, err := models.RoleCommands(qm.Where("guild_id=?", ms.GuildID), qm.Where("name ILIKE ?", name), qm.Load("RoleGroup.RoleCommands")).OneG(ctx)
if err != nil {
return false, err
}
cr := CommonRoleFromRoleCommand(cmd.R.RoleGroup, cmd)
return cr.CheckToggleRole(ctx, ms)
}
func CheckRequiredRoles(requireOneOf []int64, has []int64) bool {
for _, r := range requireOneOf {
if common.ContainsInt64Slice(has, r) {
// Only 1 role required
return true
}
}
return false
}
func CheckIgnoredRoles(ignore []int64, has []int64) error {
for _, r := range ignore {
if common.ContainsInt64Slice(has, r) {
return NewRoleError("Has ignored role", r)
}
}
return nil
}
// Just a simple type but distinguishable from errors.Error
type SimpleError string
func (s SimpleError) Error() string {
return string(s)
}
func NewSimpleError(format string, args ...interface{}) error {
return SimpleError(fmt.Sprintf(format, args...))
}
type RoleError struct {
Role int64
Message string
}
func NewRoleError(msg string, role int64) error {
return &RoleError{
Role: role,
Message: msg,
}
}
func (r *RoleError) Error() string {
if r.Role == 0 {
return r.Message
}
return r.Message + ": " + strconv.FormatInt(r.Role, 10)
}
// Uses the role name from one of the passed roles with matching id instead of the id
func (r *RoleError) PrettyError(roles []discordgo.Role) string {
if r.Role == 0 {
return r.Message
}
idStr := strconv.FormatInt(r.Role, 10)
roleStr := ""
for _, v := range roles {
if v.ID == r.Role {
roleStr = "**" + v.Name + "**"
break
}
}
if roleStr == "" {
roleStr = "(unknown role " + idStr + ")"
}
return r.Message + ": " + roleStr
}
type LmitError struct {
Limit int
Message string
}
func NewLmitError(msg string, limit int) error {
return &LmitError{
Limit: limit,
Message: msg,
}
}
func (r *LmitError) Error() string {
return fmt.Sprintf(r.Message, r.Limit)
}
type GroupError struct {
Group *models.RoleGroup
Message string
}
func NewGroupError(msg string, group *models.RoleGroup) error {
return &GroupError{
Group: group,
Message: msg,
}
}
func (r *GroupError) Error() string {
return fmt.Sprintf(r.Message, r.Group.Name)
}
type CommonRoleError struct {
Group *CommonRoleSettings
Message string
}
func NewCommonRoleError(msg string, r *CommonRoleSettings) error {
return &CommonRoleError{
Group: r,
Message: msg,
}
}
func (r *CommonRoleError) Error() string {
name := ""
if r.Group.ParentGroup != nil {
name = r.Group.ParentGroup.Name
} else {
name = "role menu"
}
return fmt.Sprintf(r.Message, name)
}
func IsRoleCommandError(err error) bool {
switch err.(type) {
case *LmitError, *RoleError, *GroupError, SimpleError, *SimpleError:
return true
default:
return false
}
}
func RoleCommandsLessFunc(slice []*models.RoleCommand) func(int, int) bool {
return func(i, j int) bool {
// Compare timestamps if positions are equal, for deterministic output
if slice[i].Position == slice[j].Position {
return slice[i].CreatedAt.After(slice[j].CreatedAt)
}
if slice[i].Position > slice[j].Position {
return false
}
return true
}
}
func GetAllRoleCommandsSorted(ctx context.Context, guildID int64) (groups []*models.RoleGroup, grouped map[*models.RoleGroup][]*models.RoleCommand, unGrouped []*models.RoleCommand, err error) {
commands, err := models.RoleCommands(qm.Where(models.RoleCommandColumns.GuildID+"=?", guildID)).AllG(ctx)
if err != nil {
return
}
grps, err := models.RoleGroups(qm.Where(models.RoleGroupColumns.GuildID+"=?", guildID)).AllG(ctx)
if err != nil {
return
}
groups = grps
grouped = make(map[*models.RoleGroup][]*models.RoleCommand)
for _, group := range groups {
grouped[group] = make([]*models.RoleCommand, 0, 10)
for _, cmd := range commands {
if cmd.RoleGroupID.Valid && cmd.RoleGroupID.Int64 == group.ID {
grouped[group] = append(grouped[group], cmd)
}
}
sort.Slice(grouped[group], RoleCommandsLessFunc(grouped[group]))
}
unGrouped = make([]*models.RoleCommand, 0, 10)
for _, cmd := range commands {
if !cmd.RoleGroupID.Valid {
unGrouped = append(unGrouped, cmd)
}
}
sort.Slice(unGrouped, RoleCommandsLessFunc(unGrouped))
err = nil
return
}