-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagebase.go
87 lines (80 loc) · 2.01 KB
/
imagebase.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
package main
import (
"fmt"
"io"
"log"
"net/http"
"net/url"
"strings"
"github.com/bwmarrin/discordgo"
)
func getImage(m *discordgo.MessageCreate, s *discordgo.Session, modname, cmdname string) io.ReadCloser {
defer func() {
e := recover()
if e != nil {
log.Println(fmt.Sprintf("[%v] ", modname), e)
}
}()
var earliestURL string
if len(m.Attachments) != 0 {
earliestURL = getURL(m.Message)
} else if len(strings.Split(m.Content, " ")) == 2 {
earliestURL = strings.Split(m.Content, " ")[1]
if _, err := url.ParseRequestURI(earliestURL); err != nil {
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("The url you put next to '$%v' is invalid.", cmdname))
return nil
}
} 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
break
}
}
}
if earliestURL == "" {
s.ChannelMessageSend(m.ChannelID, "Your command didnt include a url or attachment!")
return nil
}
//alright time to go
resp, err := http.Get(earliestURL)
if err != nil {
log.Println(fmt.Sprintf("[%v] Couldnt get: %q", modname, earliestURL))
s.MessageReactionAdd(m.ChannelID, m.ID, "❌")
return nil
}
if !strings.HasPrefix(resp.Header.Get("Content-Type"), "image/") {
log.Println(fmt.Sprintf("[%v] url was not an image: %q", resp.Request.URL))
s.ChannelMessageSend(m.ChannelID, "URL didn't give me an image.")
return nil
}
return resp.Body
}
func getURL(x *discordgo.Message) string {
if len(x.Attachments) != 0 {
return x.Attachments[0].URL
}
if x.Content != "" {
if _, err := url.ParseRequestURI(x.Content); err == nil {
if h, err := http.Head(x.Content); err == nil && strings.HasPrefix(h.Header.Get("Content-Type"), "image/") {
return x.Content
}
}
}
return ""
}
func reverse(lst []*discordgo.Message) chan *discordgo.Message {
ret := make(chan *discordgo.Message)
go func() {
for i := range lst {
ret <- lst[len(lst)-1-i]
}
close(ret)
}()
return ret
}