forked from johansundell/cocbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpics.go
More file actions
60 lines (56 loc) · 1.23 KB
/
pics.go
File metadata and controls
60 lines (56 loc) · 1.23 KB
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
package main
import (
"context"
"errors"
"math/rand"
"os"
"path"
"path/filepath"
"time"
"github.com/bwmarrin/discordgo"
)
func init() {
key := commandFunc{"!send me nude pics", "To see me nude", "", categoryFun}
lockMap.Lock()
defer lockMap.Unlock()
botFuncs[key] = func(ctx context.Context, command string) (string, error) {
if key.command == command {
if s := ctx.Value("sess"); s != nil {
if m := ctx.Value("msg"); m != nil {
str, err := getRandomImage()
if err != nil {
return "", err
}
f, err := os.Open(str)
if err != nil {
return "", err
}
_, _ = s.(*discordgo.Session).ChannelFileSend(m.(*discordgo.MessageCreate).ChannelID, "me.jpg", f)
}
}
}
return "", nil
}
}
func getRandomImage() (string, error) {
m := make(map[int]string)
ex, err := os.Executable()
if err != nil {
return "", err
}
exPath := path.Dir(ex)
files, err := filepath.Glob(exPath + string(os.PathSeparator) + "*.jpg")
if err != nil {
return "", err
}
for k, v := range files {
m[k] = v
}
rand.Seed(time.Now().UTC().UnixNano())
//fmt.Println(rand.Intn(len(m)), len(m))
str, found := m[rand.Intn(len(m))]
if !found {
return "", errors.New("No files found")
}
return str, nil
}