This repository has been archived by the owner on Sep 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
msgEmoji.go
136 lines (118 loc) · 2.98 KB
/
msgEmoji.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
package main
import (
"fmt"
"net/http"
"os"
"strings"
"github.com/bwmarrin/discordgo"
)
//Thanks to iopred
func emojiFile(s string) string {
var found string
var filename string
for _, r := range s {
if filename != "" {
filename = fmt.Sprintf("%s-%x", filename, r)
} else {
filename = fmt.Sprintf("%x", r)
}
if _, err := os.Stat(fmt.Sprintf("emoji/%s.png", filename)); err == nil {
found = filename
} else if found != "" {
return found
}
}
return found
}
func msgEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
submatch := emojiRegex.FindStringSubmatch(msglist[0])
var url string
var filename string
if len(submatch) == 3 {
switch submatch[1] {
case "":
url = fmt.Sprintf("https://cdn.discordapp.com/emojis/%s.png", submatch[2])
filename += "emoji.png"
case "a":
url = fmt.Sprintf("https://cdn.discordapp.com/emojis/%s.gif", submatch[2])
filename += "emoji.gif"
}
resp, err := http.Get(url)
if err != nil {
errorLog.Println("Custom emoji err:", err.Error())
return
}
defer resp.Body.Close()
s.ChannelFileSend(m.ChannelID, filename, resp.Body)
s.ChannelMessageDelete(m.ChannelID, m.ID)
return
}
emoji := emojiFile(msglist[0])
if emoji != "" {
file, err := os.Open(fmt.Sprintf("emoji/%s.png", emoji))
if err != nil {
errorLog.Println("Twemoji emoji err:", err.Error())
return
}
defer file.Close()
s.ChannelFileSend(m.ChannelID, "emoji.png", file)
s.ChannelMessageDelete(m.ChannelID, m.ID)
}
}
func msgFindEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
var emojiName string
submatch := emojiRegex.FindStringSubmatch(strings.Join(msglist, " "))
if len(submatch) < 2 {
emojiName = msglist[0]
} else {
emojiName = submatch[1]
}
var emojis []string
var lenEmojiNames int
var lenEmoji int
var done bool
for _, guild := range s.State.Guilds {
for _, emoji := range guild.Emojis {
if strings.Contains(strings.ToLower(emoji.Name), strings.ToLower(emojiName)) {
if !done && lenEmojiNames+len("<:"+emoji.APIName()+">") >= 900 {
done = true
lenEmoji = 0
}
if done {
lenEmoji++
continue
}
lenEmojiNames += len("<:" + emoji.APIName() + ">")
emojis = append(emojis, "<:"+emoji.APIName()+">")
}
}
}
emojisEmbed := []*discordgo.MessageEmbedField{
{Name: "", Value: strings.Join(emojis, " ") + func() string {
if lenEmoji > 0 {
return fmt.Sprintf(" and %d more...", lenEmoji)
}
return ""
}()},
}
userColor := s.State.UserColor(s.State.User.ID, m.ChannelID)
if len(emojis) == 0 {
edit := newEdit(s, m, userColor)
edit.setTitle("No emojis found!")
edit.send()
return
}
_, err := s.ChannelMessageEditComplex(&discordgo.MessageEdit{
ID: m.Message.ID,
Channel: m.ChannelID,
Content: &content,
Embed: &discordgo.MessageEmbed{
Title: "Emojis with the substring `" + emojiName + "`",
Color: userColor,
Fields: emojisEmbed,
},
})
if err != nil {
errorLog.Println(err)
}
}