-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
205 lines (172 loc) · 5.63 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
package main
import (
"io/ioutil"
"log"
"math/rand"
"net/http"
"regexp"
"time"
"github.com/bwmarrin/discordgo"
// yaml "gopkg.in/yaml.v2"
)
func errCheck(msg string, err error) {
if err != nil {
log.Printf("%s: %+v", msg, err)
panic(err)
}
}
var (
commandPrefix string
botID string
)
type apikeys struct {
BotKey string
}
type corgiAPIresp struct {
Status string `json:"status"`
Message string `json:"message"`
}
// func (a *apikeys) getAPIKeys(filename string) *apikeys {
// yamlFile, err := ioutil.ReadFile(filename)
// errCheck("", err)
// a := new(a)
// err = yaml.Unmarshal(yamlFile, a)
// errCheck("", err)
// return a
// }
func commandHandler(discord *discordgo.Session, message *discordgo.MessageCreate) {
user := message.Author
if user.ID == botID || user.Bot {
//Do nothing because the bot is talking
return
}
channel, _ := discord.State.Channel(message.ChannelID)
gid := channel.GuildID
roles, _ := discord.GuildRoles(gid)
adminRID := ""
for _, v := range roles {
if v.Name == "admin" {
adminRID = v.ID
}
}
member, _ := discord.GuildMember(gid, message.Author.ID)
admin := false
for _, r := range member.Roles {
if r == adminRID {
admin = true
}
}
content := message.Content
if content == "!test" && admin == true {
discord.ChannelMessageSend(message.ChannelID, "Testing..")
log.Printf("Command: %+v Message: %+v || From: %s\n", content, message.Message, message.Author)
}
if content == "!corgime" {
// https://dog.ceo/api/breed/corgi/images/random
// resp, err := http.Get("https://dog.ceo/api/breed/corgi/images/random")
// if err != nil {
// discord.ChannelMessageSend(message.ChannelID, "Failed to get corgi pic")
// return
// }
// body, err := ioutil.ReadAll(resp.Body)
// defer resp.Body.Close()
// errCheck("failed to parse response", err)
// corgiPic := corgiAPIresp{}
// err = json.Unmarshal(body, &corgiPic)
// errCheck("unmarshal of json failed", err)
corgi := randomCorgi()
embed := &discordgo.MessageEmbed{
Author: &discordgo.MessageEmbedAuthor{},
Color: 0x9542f4, // Green
Image: &discordgo.MessageEmbedImage{
//URL: corgiPic.Message,
URL: corgi,
},
Timestamp: time.Now().Format(time.RFC3339), // Discord wants ISO8601; RFC3339 is an extension of ISO8601 and should be completely compatible.
}
discord.ChannelMessageSendEmbed(message.ChannelID, embed)
log.Printf("Command: %+v Message: %+v || From: %s\n", content, message.Message, message.Author)
}
}
func randomCorgi() string {
url := "https://www.google.com/search?q=corgi&tbm=isch"
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
errCheck("New Request", err)
req.Header.Set("User-Agent", "Mozilla/5.0 (X11; CrOS x86_64 12607.34.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.42 Safari/537.36")
resp, err := client.Do(req)
errCheck("client do", err)
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
errCheck("io read body", err)
// ou":"http://spectrum-sitecore-spectrumbrands.netdna-ssl.com/~/media/Pet/Furminator/Images/Solution%20Center%20Images/Feature%20Images/corgi.jpg"
re := regexp.MustCompile("ou\":\"(http[^\"]+)\"")
//re := regexp.MustCompile("src=\"(http[^\"]+)\"")
matches := re.FindAllStringSubmatch(string(body), -1)
log.Println(string(body))
corgis := make([]string, len(matches))
log.Println(len(matches))
for index, match := range matches {
log.Println(match[0])
corgis[index] = match[0]
}
//seed with nanoseconds to get make sure unique random number returned
rand.Seed(time.Now().UnixNano())
corgi := corgis[rand.Intn(len(corgis))]
//get random image url and print to stdout
log.Println(corgi)
return corgi
}
func main() {
log.SetFlags(log.LstdFlags | log.Lmicroseconds | log.Lshortfile)
k := new(apikeys)
//k.getAPIKeys("/run/secrets/botkey")
botkey, err := ioutil.ReadFile("/run/secrets/botkey")
errCheck("Not able to read botkey secret", err)
k.BotKey = string(botkey)
discord, err := discordgo.New("Bot " + k.BotKey)
errCheck("error creating discord session", err)
user, err := discord.User("@me")
errCheck("error retrieving account", err)
botID = user.ID
discord.AddHandler(commandHandler)
discord.AddHandler(func(discord *discordgo.Session, ready *discordgo.Ready) {
err = discord.UpdateStatus(0, "Admiring Corgi Butt")
if err != nil {
log.Println("Error attempting to set my status")
}
servers := discord.State.Guilds
log.Printf("CorgiBot has started on %d servers", len(servers))
})
err = discord.Open()
errCheck("Error opening connection to Discord", err)
//chans, err := discord.GuildChannels(discord.)
defer discord.Close()
commandPrefix = "!"
<-make(chan struct{})
}
// embed := &discordgo.MessageEmbed{
// Author: &discordgo.MessageEmbedAuthor{},
// Color: 0x9542f4, // Green
// Description: "This is a discordgo embed",
// Fields: []*discordgo.MessageEmbedField{
// &discordgo.MessageEmbedField{
// Name: "I am a field",
// Value: "I am a value",
// Inline: true,
// },
// &discordgo.MessageEmbedField{
// Name: "I am a second field",
// Value: "I am a value",
// Inline: true,
// },
// },
// Image: &discordgo.MessageEmbedImage{
// URL: corgiPic.Message,
// },
// Thumbnail: &discordgo.MessageEmbedThumbnail{
// URL: corgiPic.Message,
// },
// Timestamp: time.Now().Format(time.RFC3339), // Discord wants ISO8601; RFC3339 is an extension of ISO8601 and should be completely compatible.
// Title: "I am an Embed",
// }