/
handler.go
151 lines (116 loc) 路 3.57 KB
/
handler.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package google
import (
"strings"
"github.com/Seklfreak/Robyul2/helpers"
"github.com/bwmarrin/discordgo"
)
type Handler struct {
}
type action func(args []string, in *discordgo.Message, out **discordgo.MessageSend) (next action)
const (
GoogleFriendlyUrl = "https://www.google.com/search"
)
func (h *Handler) Commands() []string {
return []string{
"google",
"g",
"image",
"img",
}
}
func (h *Handler) Init(session *shardmanager.Manager) {
defer helpers.Recover()
}
func (h *Handler) Action(command string, content string, msg *discordgo.Message, session *discordgo.Session) {
if !helpers.ModuleIsAllowed(msg.ChannelID, msg.ID, msg.Author.ID, helpers.ModulePermGoogle) {
return
}
var result *discordgo.MessageSend
args := strings.Fields(content)
action := h.actionSearch
switch command {
case "image", "img":
action = h.actionImageSearch
break
}
for action != nil {
action = action(args, msg, &result)
}
}
// [p]google|g <query>
func (h *Handler) actionSearch(args []string, in *discordgo.Message, out **discordgo.MessageSend) action {
quitChannel := helpers.StartTypingLoop(in.ChannelID)
defer func() { quitChannel <- 0 }()
parts := strings.Split(in.Content, " ")
if len(parts) < 2 {
*out = h.newMsg("bot.arguments.too-few")
return h.actionFinish
}
query := strings.TrimSpace(strings.Replace(in.Content, parts[0], "", 1))
channel, err := helpers.GetChannel(in.ChannelID)
helpers.Relax(err)
nsfw := channel.NSFW
results, err := search(query, nsfw, nil)
if err != nil {
if strings.Contains(err.Error(), "no search results") {
*out = h.newMsg("plugins.google.search-no-results")
return h.actionFinish
}
}
helpers.Relax(err)
if len(results) <= 0 {
*out = h.newMsg("plugins.google.search-no-results")
return h.actionFinish
}
embed := linkResultEmbed(results[0])
quitChannel <- 0
*out = &discordgo.MessageSend{
Content: helpers.GetText("<" + GoogleFriendlyUrl + "?" + getSearchQueries(query, nsfw, true) + ">"),
Embed: embed,
}
return h.actionFinish
}
// [p]image|img <query>
func (h *Handler) actionImageSearch(args []string, in *discordgo.Message, out **discordgo.MessageSend) action {
quitChannel := helpers.StartTypingLoop(in.ChannelID)
defer func() { quitChannel <- 0 }()
parts := strings.Split(in.Content, " ")
if len(parts) < 2 {
*out = h.newMsg("bot.arguments.too-few")
return h.actionFinish
}
query := strings.TrimSpace(strings.Replace(in.Content, parts[0], "", 1))
channel, err := helpers.GetChannel(in.ChannelID)
helpers.Relax(err)
nsfw := channel.NSFW
results, err := imageSearch(query, nsfw, nil)
if err != nil {
if strings.Contains(err.Error(), "no search results") {
*out = h.newMsg("plugins.google.search-no-results")
return h.actionFinish
}
}
helpers.Relax(err)
if len(results) <= 0 {
*out = h.newMsg("plugins.google.search-no-results")
return h.actionFinish
}
embed := imageResultEmbed(results[0])
quitChannel <- 0
*out = &discordgo.MessageSend{
Content: helpers.GetText("<" + GoogleFriendlyUrl + "?" + getImageSearchQuries(query, nsfw, true) + ">"),
Embed: embed,
}
return h.actionFinish
}
func (h *Handler) actionFinish(args []string, in *discordgo.Message, out **discordgo.MessageSend) action {
_, err := helpers.SendComplex(in.ChannelID, *out)
helpers.RelaxMessage(err, in.ChannelID, in.ID)
return nil
}
func (h *Handler) newMsg(content string, replacements ...interface{}) *discordgo.MessageSend {
if len(replacements) < 1 {
return &discordgo.MessageSend{Content: helpers.GetText(content)}
}
return &discordgo.MessageSend{Content: helpers.GetTextF(content, replacements...)}
}