-
Notifications
You must be signed in to change notification settings - Fork 0
/
meme.go
111 lines (102 loc) · 2.41 KB
/
meme.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
// +build ignore
//ITS BAD
package main
import (
"bytes"
"encoding/csv"
"fmt"
"image"
_ "image/gif"
_ "image/jpeg"
_ "image/png"
"log"
"net/http"
"net/url"
"strings"
"github.com/bwmarrin/discordgo"
"github.com/fogleman/gg"
)
func meme(s *discordgo.Session, m *discordgo.MessageCreate) {
sv := csv.NewReader(strings.NewReader(m.Content))
sv.Comma = ' '
parsed, err := sv.Read()
if err != nil || len(parsed) < 3 {
s.ChannelMessageSend(m.ChannelID, permusage)
return
}
defer func() {
e := recover()
if e != nil {
log.Println("[MEME] ", e)
}
}()
var earliestUrl string
if len(m.Attachments) != 0 {
earliestUrl = getUrl(m.Message)
} else if len(parsed) == 4 {
earliestUrl = parsed[3]
if _, err := url.ParseRequestURI(earliestUrl); err != nil {
s.ChannelMessageSend(m.ChannelID, "The url you put next to '&meme' is invalid.")
return
}
} else {
msgs, err := s.ChannelMessages(m.ChannelID, 100, m.ID, "", "")
if err != nil {
panic(err)
}
for _, x := range msgs {
u := getUrl(x)
if u != "" {
earliestUrl = u
log.Println(u)
break
}
}
}
if earliestUrl == "" {
s.ChannelMessageSend(m.ChannelID, "Your command didnt include a url or attachment!")
return
}
//alright time to go
resp, err := http.Get(earliestUrl)
if err != nil {
log.Println(fmt.Sprintf("[MEME] Couldnt get: %v", earliestUrl))
s.MessageReactionAdd(m.ChannelID, m.ID, "❌")
return
}
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "image/") {
log.Println(resp.Request.URL)
s.ChannelMessageSend(m.ChannelID, "The link wont work...")
return
}
i, _, err := image.Decode(resp.Body)
if err != nil {
log.Println(resp.Request.URL)
s.ChannelMessageSend(m.ChannelID, "The link wont work...")
return
}
resp.Body.Close()
dc := gg.NewContextForImage(i)
ppep := i.Bounds().Size()
if err := dc.LoadFontFace("font/impact.ttf", 96); err != nil {
panic(err)
}
dc.SetRGB(0, 0, 0)
n := 6 // "stroke" size
for dy := -n; dy <= n; dy++ {
for dx := -n; dx <= n; dx++ {
if dx*dx+dy*dy >= n*n {
// give it rounded corners
continue
}
x := float64(ppep.X)/2 + float64(dx)
y := float64(ppep.Y)/2 + float64(dy)
dc.DrawStringAnchored(parsed[1], x, y, 0.5, 0.5)
}
}
dc.SetRGB(1, 1, 1)
dc.DrawStringAnchored(parsed[1], float64(ppep.X)/2, float64(ppep.Y)/2, 0.5, 0.5)
var buf bytes.Buffer
dc.EncodePNG(&buf)
s.ChannelFileSend(m.ChannelID, "meme.png", buf.Bytes())
}