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 3
/
msgEmoji.go
116 lines (96 loc) · 2.3 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
package main
import (
"errors"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/bwmarrin/discordgo"
)
var (
errNotEmoji = errors.New("not an emoji")
)
func init() {
newCommand("bigMoji", 0, false, msgEmoji).setHelp("Args: [emoji]\n\nSends a large image of the given emoji.\n" +
"Command 'bigMoji' can be excluded for shorthand.\n\nExample:\n`!owo :smile:`\nor\n`!owo bigMoji :smile:`").add()
}
// Thanks to iopred
func emojiFile(s string) string {
var found, 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 sendEmojiFromFile(s *discordgo.Session, m *discordgo.MessageCreate, e string) (file io.ReadCloser, err error) {
emoji := emojiFile(e)
if emoji == "" {
return nil, errNotEmoji
}
return os.Open(fmt.Sprintf("emoji/%s.png", emoji))
}
func msgEmoji(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
if len(msglist) < 1 {
return
}
var emoji string
var emojiReader io.ReadCloser
var err error
filename := "emoji"
if strings.ToLower(msglist[0]) == "bigmoji" {
if len(msglist) < 2 {
return
}
emoji = msglist[1]
} else {
emoji = msglist[0]
}
submatch := emojiRegex.FindStringSubmatch(emoji)
if len(submatch) == 0 {
filename += ".png"
emojiReader, err = sendEmojiFromFile(s, m, emoji)
if err != nil {
if err != errNotEmoji {
log.Error("error getting emoji from file", err)
}
goto errored
}
} else {
var url string
switch submatch[1] {
case "":
url = fmt.Sprintf("https://cdn.discordapp.com/emojis/%s.png", submatch[2])
filename += ".png"
case "a":
url = fmt.Sprintf("https://cdn.discordapp.com/emojis/%s.gif", submatch[2])
filename += ".gif"
}
resp, err := http.Get(url)
if err != nil {
log.Error("error getting emoji from URL", err)
goto errored
}
emojiReader = resp.Body
}
defer emojiReader.Close()
errored:
if err != nil {
if err == errNotEmoji {
return
}
s.ChannelMessageSend(m.ChannelID, "There was an error getting the emoji :(")
return
}
s.ChannelFileSend(m.ChannelID, filename, emojiReader)
deleteMessage(m.Message, s)
}