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 pathmsgRule34.go
83 lines (65 loc) · 2.04 KB
/
msgRule34.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
package main
import (
"encoding/xml"
"fmt"
"net/http"
"strings"
"github.com/bwmarrin/discordgo"
)
type rule34 struct {
PostCount int `xml:"count,attr"`
Posts []struct {
URL string `xml:"file_url,attr"`
} `xml:"post"`
}
func init() {
newCommand("r34", 0, false, msgRule34).setHelp("Args: [search]\n\nReturns a random image from rule34 for the given search term.\n\nExample:\n`!owo r34 lewds`").add()
}
func msgRule34(s *discordgo.Session, m *discordgo.MessageCreate, msglist []string) {
if len(msglist) < 2 {
return
}
channel, err := channelDetails(m.ChannelID, s)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "There was a problem getting some details :( Please try again!")
return
}
guild, err := guildDetails("", channel.GuildID, s)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "There was a problem getting some details :( Please try again!")
return
}
if val, ok := sMap.server(guild.ID); ok && !val.Nsfw && (!strings.HasPrefix(channel.Name, "nsfw") && !channel.NSFW) {
s.ChannelMessageSend(m.ChannelID, "NSFW is disabled on this server~")
return
}
var r34 rule34
var query string
s.ChannelTyping(m.ChannelID)
for _, word := range msglist[1:] {
query += "+" + word
}
url := fmt.Sprintf("https://rule34.xxx/index.php?page=dapi&s=post&q=index&tags=%s", query)
page, err := http.Get(url)
if err != nil {
s.ChannelMessageSend(m.ChannelID, "error getting data from Rule34 :(")
log.Error("error from r34", err)
return
}
defer page.Body.Close()
if page.StatusCode != http.StatusOK {
s.ChannelMessageSend(m.ChannelID, "Rule34 didn't respond :(")
log.Error("non 200 status code", url)
return
}
if err = xml.NewDecoder(page.Body).Decode(&r34); err != nil {
log.Error("error unmarshalling xml", err)
return
}
if r34.PostCount == 0 {
s.ChannelMessageSend(m.ChannelID, "No results ¯\\_(ツ)_/¯")
return
}
url = r34.Posts[randRange(0, len(r34.Posts)-1)].URL
s.ChannelMessageSend(m.ChannelID, fmt.Sprintf("%s searched for `%s` \n%s", m.Author.Username, strings.Replace(query, "+", " ", -1), url))
}