-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (96 loc) · 2.53 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
"regexp"
"time"
"github.com/bwmarrin/discordgo"
"github.com/joho/godotenv"
)
var BotId string
var goBot *discordgo.Session
func Validator(s *discordgo.Session, m *discordgo.MessageCreate) {
if m.Author.ID == BotId {
return
}
rxStrict := regexp.MustCompile(`(?i)\bhttps?://\S+\b`)
url := rxStrict.FindString(m.Content)
if url != "" {
fmt.Println("Found URL:", url)
_, _ = s.ChannelMessageSend(m.ChannelID, "Hang on! NetSepio is verifying the link")
apiUrl := "https://gateway.netsepio.com/api/v1.0/stats?siteUrl=" + url
request, err := http.NewRequest("GET", apiUrl, nil)
if err != nil {
fmt.Println("Error creating GET request:", err)
_, _ = s.ChannelMessageSend(m.ChannelID, "Error contacting NetSepio API.")
return
}
client := &http.Client{Timeout: time.Second * 10}
response, err := client.Do(request)
if err != nil {
fmt.Println("Error sending GET request:", err)
_, _ = s.ChannelMessageSend(m.ChannelID, "Error contacting NetSepio API.")
return
}
defer response.Body.Close()
responseBody, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Println("Error reading response body:", err)
_, _ = s.ChannelMessageSend(m.ChannelID, "Error contacting NetSepio API.")
return
}
var data map[string]interface{}
err = json.Unmarshal(responseBody, &data)
if err != nil {
fmt.Println("Error unmarshalling response:", err)
_, _ = s.ChannelMessageSend(m.ChannelID, "Error processing NetSepio API response.")
return
}
message := fmt.Sprintf("`The link %s is not tested.`", url)
var siteSafety string
if safety, ok := data["payload"]; ok {
if safetyList, ok := safety.([]interface{}); ok {
if len(safetyList) > 0 {
if safetyMap, ok := safetyList[0].(map[string]interface{}); ok {
if safetyValue, ok := safetyMap["siteSafety"].(string); ok {
siteSafety = safetyValue
}
}
}
}
}
if siteSafety != "" {
message = fmt.Sprintf("`The link %s is classified as`**`%s`**", url, siteSafety)
}
_, _ = s.ChannelMessageSend(m.ChannelID, message)
}
}
func Start() {
godotenv.Load(".env")
goBot, err := discordgo.New("Bot " + os.Getenv("Token"))
if err != nil {
fmt.Println(err)
return
}
u, err := goBot.User("@me")
if err != nil {
fmt.Println(err)
return
}
BotId = u.ID
goBot.AddHandler(Validator)
err = goBot.Open()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("Bot is live")
}
func main() {
Start()
<-make(chan struct{})
return
}