forked from BlackArch/blackarch-discord-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
209 lines (168 loc) · 5.23 KB
/
main.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"github.com/bwmarrin/discordgo"
"flag"
"strings"
"os/signal"
"os"
"syscall"
"fmt"
"net/http"
"time"
"io/ioutil"
"bufio"
)
type buffer_t struct {
content string
timestamp int64
}
type entry_t struct {
name string
version string
description string
group string
url string
}
var (
tool_buffer buffer_t
entry_list []entry_t
)
func prettyOutput(text string) (*discordgo.MessageEmbed) {
thumbnail := discordgo.MessageEmbedThumbnail{
URL: "https://blackarch.org/images/logo/ba-logo.png",
Width: 50,
Height: 50,
}
output := discordgo.MessageEmbed{
Title: "BlackArch Tool Search",
URL: "https://blackarch.org/tools.html",
Thumbnail: &thumbnail,
Color: 13369344,
Description: text,
}
return &output
}
func bot_log(m *discordgo.MessageCreate, trigger string) {
fmt.Printf("GuildID: %s, ChannelID: %sm User: %s, Time: %s, Trigger: %s.\n",
m.Message.GuildID, m.Message.ChannelID, m.Message.Author,
m.Message.Timestamp, trigger)
}
func createToolBuffer() (err error) {
res, err := http.Get("https://raw.githubusercontent.com/BlackArch/blackarch-site/master/data/tools")
if err != nil {
return
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return
}
entry_list = nil
tool_buffer.content = fmt.Sprintf("%s", body)
tool_buffer.timestamp = (time.Now()).Unix()
scanner := bufio.NewScanner(strings.NewReader(tool_buffer.content))
for scanner.Scan() {
var entry_tmp entry_t
var tokenized []string = strings.Split(scanner.Text(), "|")
entry_tmp.name = tokenized[0]
entry_tmp.version = tokenized[1]
entry_tmp.description = tokenized[2]
entry_tmp.group = tokenized[3]
entry_tmp.url = tokenized[4]
entry_list = append(entry_list, entry_tmp)
}
return
}
func toolListUpdate() (result string, err error) {
now := (time.Now()).Unix()
if (now - 1800) < tool_buffer.timestamp {
result = "Too soon, can't update yet."
return
}
err = createToolBuffer()
result = "Updated!"
return
}
func searchTool(search string) (output string, err error) {
template := "Name: %s\nVersion: %s\nDescription: %s\nGroup: %s\nURL: %s\n\n"
for _, element := range entry_list {
if strings.Contains(element.name, search) {
output += fmt.Sprintf(template, element.name, element.version,
element.description, element.group, element.url)
if len(output) >= 1000 {
output += "\n-> **TRUNCATED**: too big, change your search."
break
}
}
}
if len(output) <= 0 {
output = "Sorry, nothing found"
}
return
}
// This function will be called (due to AddHandler above) every time a new
// message is created on any channel that the autenticated bot has access to.
func messageHandler(s *discordgo.Session, m *discordgo.MessageCreate) {
// Ignore all messages created by the bot itself
if m.Author.ID == s.State.User.ID {
return
}
// Tokenize message for parsing
var tokenized []string = strings.Split(m.Content, " ")
switch tokenized[0] {
case "+ping":
s.ChannelMessageSendEmbed(m.ChannelID, prettyOutput("pong"))
case "+search":
// Needs a parameters to search for
if len(tokenized[0:]) < 2 {
s.ChannelMessageSendEmbed(m.ChannelID,
prettyOutput("Are you crazy? Search what?"))
return
}
output, err := searchTool(tokenized[1])
if err != nil {
s.ChannelMessageSendEmbed(m.ChannelID,
prettyOutput("Something is wrong. Get a hold of an admin!"))
return
}
s.ChannelMessageSendEmbed(m.ChannelID, prettyOutput(output))
case "+update":
output, _ := toolListUpdate()
s.ChannelMessageSendEmbed(m.ChannelID, prettyOutput(output))
}
}
func main() {
// Get bot token. Maybe not a good idea?!
bot_token := flag.String("token", "", "The bot token.")
flag.Parse()
if len(*bot_token) <= 0 {
fmt.Println("You must be nutts! Where is my token?")
return
}
err := createToolBuffer()
if err != nil {
fmt.Println("Arghhh, can't get the tool list!")
return
}
// Create a new Discord session using the provided bot token.
dg, err := discordgo.New("Bot " + *bot_token)
if err != nil {
fmt.Println("Something is fucked up when connecting to Discord: ", err)
return
}
// Register the events callback handlers.
dg.AddHandler(messageHandler)
// Open a websocket connection to Discord and begin listening.
err = dg.Open()
if err != nil {
fmt.Println("Something is fucked up when openning connection: ", err)
return
}
fmt.Println("We are on, baby... :)")
// Wait here until CTRL-C or other term signal is received.
sc := make(chan os.Signal, 1)
signal.Notify(sc, syscall.SIGINT, syscall.SIGTERM, os.Interrupt, os.Kill)
<-sc
// Cleanly close down the Discord session.
fmt.Println("Bye-bye, darling... :(")
dg.Close()
}