/
emoji.go
423 lines (350 loc) · 10.1 KB
/
emoji.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package handlers
import (
"errors"
"fmt"
"math/rand"
"sort"
"strings"
"time"
"github.com/bwmarrin/discordgo"
"github.com/unswpcsoc/pcsocgo/commands"
"github.com/unswpcsoc/pcsocgo/internal/utils"
)
const (
keyEmoji = "emoji"
thinkingEmoji = string(0x1f914)
emojiLineLimit = 15
chungusW = "<:cw:590153701252005907>"
chungus1 = "<:c1_0:590153698324381698>"
chungus2 = "<:c2_0:590153703609204760>"
chungus1_1 = "<:c1_1:590153699372826634>"
chungus3_1 = "<:c3_1:590153701281497109>"
chungus0_2 = "<:c0_2:590153690493747220>"
chungus1_2 = "<:c1_2:590153704129298442>"
chungus2_2 = "<:c2_2:590153702443319307>"
chungus3_2 = "<:c3_2:590153704121040898>"
chungus0_3 = "<:c0_3:590153695363203072>"
chungus1_3 = "<:c1_3:590153703454015493>"
chungus2_3 = "<:c2_3:590153701969231873>"
chungus3_3 = "<:c3_3:590153703697416192>"
)
var (
ErrEmojiNotInit = errors.New("emoji counter not initialised")
)
// emojis implements the Storer interface
type emojis struct {
Counter map[string]int
Start time.Time
}
func (e *emojis) Index() string {
return keyEmoji
}
// emoji implements the Command interface
type emoji struct {
nilCommand
EmojiNames []string `arg:"emoji names"`
}
func newEmoji() *emoji { return &emoji{} }
func (e *emoji) Subcommands() []commands.Command {
return []commands.Command{
newEmojiCount(),
newEmojiChungus(),
newEmojiCunt(),
newEmojiRegional(),
}
}
func (e *emoji) Aliases() []string { return []string{"emoji", thinkingEmoji, "e"} }
func (e *emoji) Desc() string { return "Prints a random custom server emoji" }
func (e *emoji) MsgHandle(ses *discordgo.Session, msg *discordgo.Message) (*commands.CommandSend, error) {
// get guild emojis
emojis, err := ses.GuildEmojis(msg.GuildID)
if err != nil {
return nil, err
}
// seed random
r := rand.New(rand.NewSource(time.Now().UnixNano()))
if len(e.EmojiNames) != 0 {
outWords := []string{}
for _, emojiName := range e.EmojiNames {
// try find emoji
found := false
for _, emoji := range emojis {
if strings.ToLower(emoji.Name) == strings.ToLower(emojiName) {
// found it, append to output list
outWords = append(outWords, emoji.MessageFormat())
found = true
break
}
}
if !found {
// randomly select an emoji
outWords = append(outWords, emojis[r.Intn(len(emojis))].MessageFormat())
}
}
if len(emojis) > 0 {
return commands.NewSimpleSend(msg.ChannelID, strings.Join(outWords, "")), nil
}
}
// randomly select an emoji
picked := emojis[r.Intn(len(emojis))].MessageFormat()
return commands.NewSimpleSend(msg.ChannelID, picked), nil
}
type emojiCount struct {
nilCommand
}
func newEmojiCount() *emojiCount { return &emojiCount{} }
func (e *emojiCount) Aliases() []string {
return []string{"emoji count", "emoji co", "emoji stats", "emoji st"}
}
func (e *emojiCount) Desc() string {
return "Prints a summary of the usage of custom server emojis\nNote: emoji are counted per message and reaction; using 10 of the same emoji in one message will only count as 1"
}
func (e *emojiCount) MsgHandle(ses *discordgo.Session, msg *discordgo.Message) (*commands.CommandSend, error) {
// Get emojis
var emo emojis
err := commands.DBGet(&emojis{}, keyEmoji, &emo)
if err == commands.ErrDBNotFound {
return nil, ErrEmojiNotInit
} else if err != nil {
return nil, err
}
// sort by most used
type kv struct {
Key string
Value int
}
counts := []kv{}
for key, val := range emo.Counter {
counts = append(counts, kv{key, val})
}
sort.Slice(counts, func(left, right int) bool {
return counts[left].Value > counts[right].Value
})
var title string = "Emoji stats (from " + emo.Start.Format("15:04:05 MST 2006-01-02") + "):\n"
lines := []string{}
for _, item := range counts {
lines = append(lines, fmt.Sprintf("%s : %d", item.Key, item.Value))
}
unregister, needUnregister := InitPaginated(ses, msg, title, lines, emojiLineLimit)
if needUnregister {
timer := time.NewTimer(2 * time.Minute)
<-timer.C
// yeet
unregister()
}
return nil, nil
}
type emojiChungus struct {
nilCommand
Emoji []string `arg:"emoji"`
}
func newEmojiChungus() *emojiChungus { return &emojiChungus{} }
func (e *emojiChungus) Aliases() []string { return []string{"emoji chungus", "emoji ch", "chungus"} }
func (e *emojiChungus) Desc() string {
return "Prints a chungus with the emoji supplied or an emoji from this server (searches if a string is provided)"
}
func (e *emojiChungus) MsgHandle(ses *discordgo.Session, msg *discordgo.Message) (*commands.CommandSend, error) {
// get guild emojis
emojis, err := ses.GuildEmojis(msg.GuildID)
if err != nil {
return nil, err
}
// seed random
r := rand.New(rand.NewSource(time.Now().UnixNano()))
var picked string
filler1 := ""
filler2 := ""
filler3 := ""
if len(e.Emoji) == 0 {
// randomly select an emoji
picked = emojis[r.Intn(len(emojis))].MessageFormat()
} else if strings.HasPrefix(e.Emoji[0], "<") {
// has an emoji (I think), put it all in
picked = strings.Join(e.Emoji, "")
for i := 0; i < len(e.Emoji)-1; i++ {
filler1 += chungusW
filler2 += chungus2_2
filler3 += chungus2_3
}
} else {
// has a string other than that, search for the emoji, give a random one otherwise
outWords := []string{}
for _, emojiName := range e.Emoji {
// try find emoji
found := false
for _, emoji := range emojis {
if strings.ToLower(emoji.Name) == strings.ToLower(emojiName) {
// found it, append to output list
outWords = append(outWords, emoji.MessageFormat())
found = true
break
}
}
if !found {
// randomly select an emoji
outWords = append(outWords, emojis[r.Intn(len(emojis))].MessageFormat())
}
}
picked = strings.Join(outWords, "")
for i := 0; i < len(outWords)-1; i++ {
filler1 += chungusW
filler2 += chungus2_2
filler3 += chungus2_3
}
}
chungachunga := chungusW + chungus1 + filler1 + chungus2 + chungusW + "\n"
chungachunga += chungusW + chungus1_1 + picked + chungus3_1 + "\n"
chungachunga += chungus0_2 + chungus1_2 + filler2 + chungus2_2 + chungus3_2 + "\n"
chungachunga += chungus0_3 + chungus1_3 + filler3 + chungus2_3 + chungus3_3
return commands.NewSimpleSend(msg.ChannelID, chungachunga), nil
}
type emojiCunt struct {
nilCommand
}
func newEmojiCunt() *emojiCunt { return &emojiCunt{} }
func (e *emojiCunt) Aliases() []string { return []string{"emoji cunt"} }
func (e *emojiCunt) Desc() string { return "OI" }
func (e *emojiCunt) MsgHandle(ses *discordgo.Session, msg *discordgo.Message) (*commands.CommandSend, error) {
return commands.NewSimpleSend(msg.ChannelID, utils.EmojiAlpha("OI CUNT")), nil
}
type emojiRegional struct {
nilCommand
Message []string `arg:"Message"`
}
func newEmojiRegional() *emojiRegional { return &emojiRegional{} }
func (e *emojiRegional) Aliases() []string { return []string{"emoji regional", "regional"} }
func (e *emojiRegional) Desc() string { return "Returns alphanumeric messages" }
func (e *emojiRegional) MsgHandle(ses *discordgo.Session, msg *discordgo.Message) (*commands.CommandSend, error) {
return commands.NewSimpleSend(msg.ChannelID, utils.EmojiAlpha(strings.Join(e.Message, " "))), nil
}
// logger for emoji count
func initEmoji(ses *discordgo.Session) {
ses.AddHandler(func(se *discordgo.Session, mc *discordgo.MessageCreate) {
// lock the db for writing
commands.DBLock()
defer commands.DBUnlock()
if mc.Author.Bot {
return
}
var emo emojis
// get the emoji list
err := commands.DBGet(&emojis{}, keyEmoji, &emo)
if err == commands.ErrDBNotFound {
// create a new emoji list
emo = emojis{
Counter: make(map[string]int),
Start: time.Now(),
}
} else if err != nil {
return
}
// get guild emojis
var emojis []*discordgo.Emoji
emojis, err = ses.GuildEmojis(mc.GuildID)
if err != nil {
return
}
// check message for emojis
for _, emoji := range emojis {
var emojiText string = emoji.MessageFormat()
if strings.Contains(mc.Content, emojiText) {
emo.Counter[emojiText] += 1
}
}
// Set the in the db
commands.DBSet(&emo, keyEmoji)
})
ses.AddHandler(func(se *discordgo.Session, mra *discordgo.MessageReactionAdd) {
// lock the db for writing
commands.DBLock()
defer commands.DBUnlock()
mem, err := se.State.Member(mra.GuildID, mra.UserID)
if err == nil {
if mem.User.Bot {
return
}
} else {
// fall back to session
usr, err := se.User(mra.UserID)
if err != nil {
return
}
if usr.Bot {
return
}
}
var emo emojis
// get the emoji list
err = commands.DBGet(&emojis{}, keyEmoji, &emo)
if err == commands.ErrDBNotFound {
// create a new emoji list
emo = emojis{
Counter: make(map[string]int),
Start: time.Now(),
}
} else if err != nil {
return
}
// get guild emojis
var emojis []*discordgo.Emoji
emojis, err = ses.GuildEmojis(mra.GuildID)
if err != nil {
return
}
// check reaction
for _, emoji := range emojis {
var emojiText string = emoji.MessageFormat()
if mra.Emoji.MessageFormat() == emoji.MessageFormat() {
emo.Counter[emojiText] += 1
}
}
// Set the in the db
commands.DBSet(&emo, keyEmoji)
})
ses.AddHandler(func(se *discordgo.Session, mrr *discordgo.MessageReactionRemove) {
// lock the db for writing
commands.DBLock()
defer commands.DBUnlock()
mem, err := se.State.Member(mrr.GuildID, mrr.UserID)
if err == nil {
if mem.User.Bot {
return
}
} else {
// fall back to session
usr, err := se.User(mrr.UserID)
if err != nil {
return
}
if usr.Bot {
return
}
}
var emo emojis
// get the emoji list
err = commands.DBGet(&emojis{}, keyEmoji, &emo)
if err == commands.ErrDBNotFound {
return
} else if err != nil {
return
}
// get guild emojis
var emojis []*discordgo.Emoji
emojis, err = ses.GuildEmojis(mrr.GuildID)
if err != nil {
return
}
// check reaction
for _, emoji := range emojis {
var emojiText string = emoji.MessageFormat()
if mrr.Emoji.MessageFormat() == emoji.MessageFormat() {
if emo.Counter[emojiText] > 0 {
emo.Counter[emojiText] -= 1
}
}
}
// Set the in the db
commands.DBSet(&emo, keyEmoji)
})
return
}