-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.go
72 lines (68 loc) · 1.63 KB
/
util.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
package main
import (
"fmt"
"github.com/bwmarrin/discordgo"
"github.com/kyoh86/xdg"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
)
// no error handling because a blank avatar isn't fatal
func getAvatarFor(u *discordgo.User) (avatarPath string) {
avatarDir := filepath.Join(xdg.CacheHome(), "discord-avatars")
if err := os.MkdirAll(avatarDir, os.ModePerm); err != nil {
log.Println("warning: unable to make avatar cache dir: ", err)
return
}
avatarPath = filepath.Join(avatarDir, u.Avatar+".png")
if _, err := os.Stat(avatarPath); os.IsNotExist(err) {
resp, err := http.Get(u.AvatarURL("64"))
if err != nil {
log.Println("warning: unable to download avatar: ", err)
return
}
defer resp.Body.Close()
outFile, err := os.Create(avatarPath)
if err != nil {
log.Println("warning: ", err)
return
}
_, err = io.Copy(outFile, resp.Body)
if err != nil {
log.Println("warning: unable to save avatar: ", err)
return
}
}
return avatarPath
}
func readTokenFromFile() (string, error) {
configPath, err := xdg.FindConfigFile("discord.token")
if err != nil {
return "", err
}
tokenBytes, err := ioutil.ReadFile(configPath)
if err != nil {
return "", err
}
token := strings.TrimSpace(string(tokenBytes))
return token, nil
}
func openBrowser(url string) (err error) {
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return
}