-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.go
347 lines (308 loc) · 9.15 KB
/
bot.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/bwmarrin/discordgo"
"io"
"log"
"net/http"
"os"
"os/signal"
)
// BotToken Flags
var (
BotToken = flag.String("token", "", "Bot token")
UnsplashToken = flag.String("unsplash-token", "", "Unsplash token")
)
var (
GuildID = flag.String("guild", "", "Test guild ID. If not passed - bot registers commands globally")
RemoveCommands = flag.Bool("rmcmd", true, "Remove all commands after shutdowning or not")
)
var s *discordgo.Session
type ExtendedCommand struct {
applicationcommand *discordgo.ApplicationCommand
}
type UnsplashRandom struct {
ID string `json:"id"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
Width int `json:"width"`
Height int `json:"height"`
Color string `json:"color"`
Description string `json:"description"`
URLs struct {
Raw string `json:"raw"`
Regular string `json:"regular"`
Small string `json:"small"`
Full string `json:"full"`
Thumb string `json:"thumb"`
S3 string `json:"small-s3"`
}
Links struct {
Self string `json:"self"`
HTML string `json:"html"`
Download string `json:"download"`
DownloadLocation string `json:"download_location"`
} `json:"links"`
User struct {
ID string `json:"id"`
UpdatedAt string `json:"updated_at"`
Username string `json:"username"`
Name string `json:"name"`
Links struct {
Self string `json:"self"`
HTML string `json:"html"`
Photos string `json:"photos"`
Likes string `json:"likes"`
Portfolio string `json:"portfolio"`
} `json:"links"`
} `json:"user"`
}
type UnsplashDownloadLoc struct {
URL string `json:"url"`
}
func init() { flag.Parse() }
func UnsplashImageFromApi(query string) *UnsplashRandom {
resp, err := http.Get(fmt.Sprintf("https://api.unsplash.com/photos/random?query=%v&client_id=%v", query, *UnsplashToken))
if err != nil {
log.Fatalf("An Error Occured %v", err)
}
var unsplash *UnsplashRandom
err = json.NewDecoder(resp.Body).Decode(&unsplash)
if err != nil {
var invalid *UnsplashRandom
return invalid
}
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
}
}(resp.Body)
return unsplash
}
func init() {
*BotToken = ReadTokenFromFile("token.txt")
if *BotToken != "" {
log.Println("Token read from file")
} else {
log.Println("Token not read from file, fetching from env")
*BotToken = os.Getenv("TOKEN")
}
*UnsplashToken = ReadTokenFromFile("unsplash-token.txt")
if *UnsplashToken != "" {
log.Println("Unsplash token read from file")
} else {
log.Println("Unsplash token not read from file, fetching from env")
*UnsplashToken = os.Getenv("UNSPLASH_TOKEN")
}
var err error
s, err = discordgo.New("Bot " + *BotToken)
if err != nil {
log.Fatalf("Invalid bot parameters: %v", err)
}
}
func ReadTokenFromFile(file string) string {
f, err := os.Open(file)
if err != nil {
return ""
}
defer func(f *os.File) {
err := f.Close()
if err != nil {
}
}(f)
buf := make([]byte, 1024)
n, err := f.Read(buf)
if err != nil {
}
buf = buf[:n]
return string(buf)
}
func init() {
flag.Parse()
s.AddHandler(func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if h, ok := commandHandlers[i.ApplicationCommandData().Name]; ok {
h(s, i)
}
})
}
var (
commands = []*ExtendedCommand{
{
applicationcommand: &discordgo.ApplicationCommand{
Name: "ping",
Description: "Ping",
},
},
{
applicationcommand: &discordgo.ApplicationCommand{
Name: "echo",
Description: "make the bot relay a message to a channel",
Options: []*discordgo.ApplicationCommandOption{
{
Type: discordgo.ApplicationCommandOptionChannel,
Name: "channel",
Description: "The channel to send the message to",
Required: true,
},
{
Type: discordgo.ApplicationCommandOptionString,
Name: "content",
Description: "The content of the message",
Required: true,
},
},
},
},
{
applicationcommand: &discordgo.ApplicationCommand{
Name: "randompigeon",
Description: "uses unsplash to get a random image of a pigeon",
},
},
}
commandHandlers = map[string]func(s *discordgo.Session, i *discordgo.InteractionCreate){
"ping": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "~~why do u hurt me~~ Pong!",
},
})
if err != nil {
return
}
},
"echo": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
if i.Interaction.Member.Permissions&discordgo.PermissionManageMessages == discordgo.PermissionManageMessages {
options := i.ApplicationCommandData().Options
optionMap := make(map[string]*discordgo.ApplicationCommandInteractionDataOption, len(options))
for _, opt := range options {
optionMap[opt.Name] = opt
}
var (
channelobj *discordgo.Channel
messagecontent string
)
// Get the value from the option map.
// When the option exists, ok = true
if option, ok := optionMap["channel"]; ok {
// Option values must be type asserted from interface{}.
// Discordgo provides utility functions to make this simple.
channelobj = option.ChannelValue(s)
}
if opt, ok := optionMap["content"]; ok {
messagecontent = opt.StringValue()
}
_, err := s.ChannelMessageSendComplex(channelobj.ID, &discordgo.MessageSend{
Content: messagecontent,
})
if err != nil {
return
}
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: fmt.Sprintf("Echo sent to %s:\n```%s```", channelobj.Mention(), messagecontent),
},
})
if err != nil {
return
}
} else {
return
}
},
"randompigeon": func(s *discordgo.Session, i *discordgo.InteractionCreate) {
var (
query = "pigeon"
err error
)
var unsplash = UnsplashImageFromApi(query)
if unsplash.URLs.Small != "" {
fmt.Println(unsplash.URLs.Small)
fmt.Println(unsplash.Links.DownloadLocation)
err = s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Embeds: []*discordgo.MessageEmbed{
{
Title: "Random Pigeon (click to view on Unsplash)",
URL: fmt.Sprintf("%s?utm_source=Pijin-Bot&utm_medium=referral", unsplash.Links.HTML),
Image: &discordgo.MessageEmbedImage{
URL: unsplash.URLs.Small,
Width: 512,
Height: 512,
},
Description: fmt.Sprintf("ALL IMAGES ARE TAKEN FROM UNSPLASH.COM\n\n%s", unsplash.Description),
Author: &discordgo.MessageEmbedAuthor{
Name: fmt.Sprintf("Photo by %s", unsplash.User.Name),
URL: unsplash.User.Links.HTML,
},
},
},
},
})
if err != nil {
return
}
} else {
err := s.InteractionRespond(i.Interaction, &discordgo.InteractionResponse{
Type: discordgo.InteractionResponseChannelMessageWithSource,
Data: &discordgo.InteractionResponseData{
Content: "Something happened, I couldn't find a random pigeon! (Small Image URL is Blank)",
},
})
if err != nil {
return
}
}
},
}
)
func main() {
s.AddHandler(func(s *discordgo.Session, r *discordgo.Ready) {
log.Printf("Logged in as: %v#%v", s.State.User.Username, s.State.User.Discriminator)
})
err := s.Open()
if err != nil {
log.Fatalf("Cannot open the session: %v", err)
}
log.Println("Adding commands...")
registeredCommands := make([]*discordgo.ApplicationCommand, len(commands))
for i, v := range commands {
cmd, err := s.ApplicationCommandCreate(s.State.User.ID, *GuildID, v.applicationcommand)
if err != nil {
log.Panicf("Cannot create '%v' command: %v", v.applicationcommand.Name, err)
}
registeredCommands[i] = cmd
}
defer func(s *discordgo.Session) {
err := s.Close()
if err != nil {
}
}(s)
s.Identify.Intents = discordgo.MakeIntent(discordgo.IntentsAllWithoutPrivileged)
stop := make(chan os.Signal, 1)
signal.Notify(stop, os.Interrupt)
<-stop
if *RemoveCommands {
log.Println("Removing commands...")
// // We need to fetch the commands, since deleting requires the command ID.
// // We are doing this from the returned commands on line 375, because using
// // this will delete all the commands, which might not be desirable, so we
// // are deleting only the commands that we added.
// registeredCommands, err := s.ApplicationCommands(s.State.User.ID, *GuildID)
// if err != nil {
// log.Fatalf("Could not fetch registered commands: %v", err)
// }
for _, v := range registeredCommands {
err := s.ApplicationCommandDelete(s.State.User.ID, *GuildID, v.ID)
if err != nil {
log.Panicf("Cannot delete '%v' command: %v", v.Name, err)
}
}
}
log.Println("Graceful shutdown")
}