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
/
Copy pathmsgIbsearch.go
108 lines (86 loc) · 2.76 KB
/
msgIbsearch.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
package main
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"github.com/bwmarrin/discordgo"
)
type ibStruct struct {
Path string `json:"path"`
Server string `json:"server"`
}
func init() {
newCommand("ibsearch", 0, false, msgIbsearch).setHelp("Args: [search] | rating=[e,s,q] | format=[gif,png,jpg]\n\n" +
"Returns a random image from ibsearch for the given search term with the given filters applied.\n\n" +
"Example:\n`!owo ibsearch lewds | rating=e | format=gif`")
}
func msgIbsearch(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
if len(msglist) < 2 {
return
}
channel, err := channelDetails(m.ChannelID, s)
if err != nil {
return
}
guild, err := guildDetails("", channel.GuildID, s)
if err != nil {
return
}
if val, ok := sMap.server(guild.ID); ok && !val.Nsfw && !strings.Contains(channel.Name, "nsfw") && !channel.NSFW {
s.ChannelMessageSend(m.ChannelID, "NSFW is disabled on this server~")
return
}
s.ChannelTyping(m.ChannelID)
URL, err := url.Parse("https://ibsearch.xxx")
if err != nil {
log.Error("IBSearch query error", err)
return
}
var queries []string
queryList := strings.Split(strings.Join(remove(msglist, 0), " "), "|")
for i, item := range queryList {
if strings.Contains(item, "=") {
queries = append(queries, strings.TrimSpace(strings.Split(queryList[i], "=")[0]))
}
}
var finalQuery string
filters := []string{"rating", "format"}
for _, item1 := range filters {
for i, item2 := range queries {
if strings.Contains(item1, item2) {
finalQuery += strings.Replace(queryList[i+1], " ", "", -1) + " "
}
}
}
//Assemble the URL
var par url.Values
URL.Path += "/api/v1/images.json"
par.Add("q", strings.TrimSpace(queryList[0])+" "+finalQuery+"random:")
par.Add("limit", "1")
//Public key that is for free, worst that can happen is that
//i hit the ratelimit, but please dont do that to me
par.Add("key", "2480CFA681A7A882CB33C0E4BA00A812C6F906A6")
URL.RawQuery = par.Encode()
client := http.Client{Timeout: time.Second * 2}
page, err := client.Get(URL.String())
if err != nil {
log.Error("Ibsearch http error", err)
s.ChannelMessageSend(m.ChannelID, "Error getting results from ibsearch")
return
}
defer page.Body.Close()
if page.StatusCode != http.StatusOK {
s.ChannelMessageSend(m.ChannelID, "IBSearch didn't respond :(")
return
}
var ibsearchStruct [1]ibStruct
if err := json.NewDecoder(page.Body).Decode(&ibsearchStruct); err != nil {
log.Error("IBSearch json unmarshal err", err)
s.ChannelMessageSend(m.ChannelID, "No results ¯\\_(ツ)_/¯")
return
}
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s searched for `%s` \nhttps://%s.ibsearch.xxx/%s", m.Author.Username, queryList[0], ibsearchStruct[0].Server, ibsearchStruct[0].Path))
}