-
Notifications
You must be signed in to change notification settings - Fork 0
/
level_cfg.go
262 lines (223 loc) · 8 KB
/
level_cfg.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
package levels
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/starshine-sys/bcr"
)
func (bot *Bot) configShow(ctx *bcr.Context) (err error) {
gc, err := bot.getGuildConfig(ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
desc := fmt.Sprintf(`**Levels enabled:** %v
**DM on reward:** %v
**Time between XP:** %v
**Reward log:** %v
**Nolevels log:** %v`, gc.LevelsEnabled, gc.DMOnReward, gc.BetweenXP, bot.mentionOrNone(gc.RewardLog), bot.mentionOrNone(gc.NolevelsLog))
e := discord.Embed{
Color: bot.Colour,
Title: "Configuration for " + ctx.Guild.Name,
Description: desc,
}
f := discord.EmbedField{
Name: "Blacklisted channels",
Inline: true,
}
if len(gc.BlockedChannels) == 0 {
f.Value = "None"
} else {
for _, ch := range gc.BlockedChannels {
f.Value += discord.ChannelID(ch).Mention() + "\n"
}
}
e.Fields = append(e.Fields, f)
f = discord.EmbedField{
Name: "Blacklisted categories",
Inline: true,
}
if len(gc.BlockedCategories) == 0 {
f.Value = "None"
} else {
for _, ch := range gc.BlockedCategories {
f.Value += discord.ChannelID(ch).Mention() + "\n"
}
}
e.Fields = append(e.Fields, f)
f = discord.EmbedField{
Name: "Blacklisted roles",
Inline: true,
}
if len(gc.BlockedRoles) == 0 {
f.Value = "None"
} else {
for _, ch := range gc.BlockedRoles {
f.Value += discord.RoleID(ch).Mention() + "\n"
}
}
e.Fields = append(e.Fields, f)
rewards, err := bot.getAllRewards(ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
fieldVal := ""
for _, reward := range rewards {
fieldVal += fmt.Sprintf("%v: %v\n", reward.Level, reward.RoleReward.Mention())
}
if fieldVal != "" {
e.Fields = append(e.Fields, discord.EmbedField{
Name: "Level rewards",
Value: fieldVal,
})
}
if gc.RewardText != "" {
e.Fields = append(e.Fields, discord.EmbedField{
Name: "Reward text",
Value: "```md\n" + gc.RewardText + "\n```",
})
}
return ctx.SendX("", e)
}
func (bot *Bot) mentionOrNone(id discord.ChannelID) string {
if id.IsValid() {
return id.Mention()
}
return "None"
}
func (bot *Bot) setConfig(ctx *bcr.Context) (err error) {
switch strings.ToLower(ctx.Args[0]) {
case "enable":
b, err := strconv.ParseBool(ctx.Args[1])
if err != nil {
_, err = ctx.Replyc(bcr.ColourRed, "`%v` is not a valid setting for `enable` (true or false)", ctx.Args[1])
return err
}
_, err = bot.DB.Exec(context.Background(), "update level_config set levels_enabled = $1 where id = $2", b, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
_ = ctx.State.React(ctx.Message.ChannelID, ctx.Message.ID, "✅")
case "dm_on_reward":
b, err := strconv.ParseBool(ctx.Args[1])
if err != nil {
_, err = ctx.Replyc(bcr.ColourRed, "`%v` is not a valid setting for `dm_on_reward` (true or false)", ctx.Args[1])
return err
}
_, err = bot.DB.Exec(context.Background(), "update level_config set dm_on_reward = $1 where id = $2", b, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
_ = ctx.State.React(ctx.Message.ChannelID, ctx.Message.ID, "✅")
case "between_xp":
dur, err := time.ParseDuration(ctx.Args[1])
if err != nil {
_, err = ctx.Replyc(bcr.ColourRed, "`%v` is not a valid setting for `between_xp` (must be a duration)", ctx.Args[1])
return err
}
_, err = bot.DB.Exec(context.Background(), "update level_config set between_xp = $1 where id = $2", dur, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
_ = ctx.State.React(ctx.Message.ChannelID, ctx.Message.ID, "✅")
case "reward_log", "nolevels_log":
ch, err := ctx.ParseChannel(ctx.Args[1])
if err != nil {
_, err = ctx.Replyc(bcr.ColourRed, "`%v` is not a valid setting for `%v` (must be a text channel in this guild)", ctx.Args[1], strings.ToLower(ctx.Args[0]))
return err
}
if ch.GuildID != ctx.Guild.ID || ch.Type != discord.GuildText {
_, err = ctx.Replyc(bcr.ColourRed, "%v is not a valid channel for `%v` (must be a text channel in this guild)", ch.Mention(), strings.ToLower(ctx.Args[0]))
return err
}
_, err = bot.DB.Exec(context.Background(), "update level_config set "+strings.ToLower(ctx.Args[0])+" = $1 where id = $2", ch.ID, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
_ = ctx.State.React(ctx.Message.ChannelID, ctx.Message.ID, "✅")
case "reward_text":
text := strings.TrimSpace(strings.TrimPrefix(ctx.RawArgs, ctx.Args[0]))
if text == ctx.RawArgs {
text = strings.Join(ctx.Args[1:], " ")
}
if len(text) > 900 {
_, err = ctx.Replyc(bcr.ColourRed, "Your text is too long! Maximum 900 characters.")
return
}
_, err = bot.DB.Exec(context.Background(), "update level_config set reward_text = $1 where id = $2", text, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
_ = ctx.State.React(ctx.Message.ChannelID, ctx.Message.ID, "✅")
default:
_, err = ctx.Replyc(bcr.ColourRed, "Sorry, but `%v` is not a valid configuration key. Valid keys are: enable, dm_on_reward, between_xp, nolevels_log, reward_log, reward_text.", ctx.Args[0])
return
}
return
}
func (bot *Bot) blacklistAdd(ctx *bcr.Context) (err error) {
// try parse channel
ch, err := ctx.ParseChannel(ctx.RawArgs)
if err == nil {
if ch.GuildID != ctx.Guild.ID || (ch.Type != discord.GuildText && ch.Type != discord.GuildNews && ch.Type != discord.GuildCategory) {
return ctx.SendX("Invalid channel provided, must be in this guild and be a text or category channel.")
}
if ch.Type == discord.GuildCategory {
_, err := bot.DB.Exec(context.Background(), "update level_config set blocked_categories = array_append(blocked_categories, $1) where id = $2", ch.ID, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
return ctx.SendfX("Added %v to the list of blocked categories!", ch.Name)
} else {
_, err := bot.DB.Exec(context.Background(), "update level_config set blocked_channels = array_append(blocked_channels, $1) where id = $2", ch.ID, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
return ctx.SendfX("Added %v/#%v to the list of blocked channels!", ch.Mention(), ch.Name)
}
}
// else try parsing role
r, err := ctx.ParseRole(ctx.RawArgs)
if err != nil {
return ctx.SendX("Input is not a valid role or channel.")
}
_, err = bot.DB.Exec(context.Background(), "update level_config set blocked_roles = array_append(blocked_roles, $1) where id = $2", r.ID, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
return ctx.SendfX("Added @%v to the list of blocked roles!", r.Name)
}
func (bot *Bot) blacklistRemove(ctx *bcr.Context) (err error) {
// try parse channel
ch, err := ctx.ParseChannel(ctx.RawArgs)
if err == nil {
if ch.GuildID != ctx.Guild.ID || (ch.Type != discord.GuildText && ch.Type != discord.GuildNews && ch.Type != discord.GuildCategory) {
return ctx.SendX("Invalid channel provided, must be in this guild and be a text or category channel.")
}
if ch.Type == discord.GuildCategory {
_, err := bot.DB.Exec(context.Background(), "update level_config set blocked_categories = array_remove(blocked_categories, $1) where id = $2", ch.ID, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
return ctx.SendfX("Remmoved %v from the list of blocked categories!", ch.Name)
} else {
_, err := bot.DB.Exec(context.Background(), "update level_config set blocked_channels = array_remove(blocked_channels, $1) where id = $2", ch.ID, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
return ctx.SendfX("Removed %v/#%v from the list of blocked channels!", ch.Mention(), ch.Name)
}
}
// else try parsing role
r, err := ctx.ParseRole(ctx.RawArgs)
if err != nil {
return ctx.SendX("Input is not a valid role or channel.")
}
_, err = bot.DB.Exec(context.Background(), "update level_config set blocked_roles = array_remove(blocked_roles, $1) where id = $2", r.ID, ctx.Guild.ID)
if err != nil {
return bot.Report(ctx, err)
}
return ctx.SendfX("Removed @%v from the list of blocked roles!", r.Name)
}