This repository has been archived by the owner on Sep 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
commandreact.go
88 lines (74 loc) · 1.45 KB
/
commandreact.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
package main
import (
"strconv"
"strings"
)
func reactMessage(text []string) {
if Channel == nil {
Message("You're not in a channel!")
return
}
if len(text) < 3 {
Message("Invalid arguments! Refer to description.")
return
}
messageID, err := strconv.ParseInt(text[1], 10, 64)
if err != nil {
Message("Failed to find the message.")
return
}
message, err := d.State.Message(Channel.ID, messageID)
if err != nil {
Message("Failed to find the message.")
return
}
var (
emoji = make([]string, len(text)-2)
reacted = make([]bool, len(text)-2)
)
for i := 0; i < len(text)-2; i++ {
regres := EmojiRegex.FindAllStringSubmatch(text[i+2], -1)
if len(regres) > 0 && len(regres[0]) == 4 {
emoji[i] = regres[0][2] + ":" + regres[0][3]
for _, r := range message.Reactions {
if r.Emoji == nil {
continue
}
if strconv.FormatInt(r.Emoji.ID, 10) == regres[0][3] {
reacted[i] = r.Me
break
}
}
} else {
emoji[i] = strings.TrimSpace(text[i+2])
for _, r := range message.Reactions {
if r.Emoji == nil {
continue
}
if r.Emoji.Name == text[i+2] {
reacted[i] = r.Me
break
}
}
}
}
for i := 0; i < len(emoji); i++ {
if reacted[i] {
err = d.MessageReactionRemoveMe(
Channel.ID,
message.ID,
emoji[i],
)
} else {
err = d.MessageReactionAdd(
Channel.ID,
message.ID,
emoji[i],
)
}
if err != nil {
Warn(err.Error())
return
}
}
}