-
Notifications
You must be signed in to change notification settings - Fork 21
/
genkey.go
57 lines (50 loc) · 1.28 KB
/
genkey.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
package main
import (
"errors"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
"github.com/bwmarrin/discordgo"
)
func genkey(caller *discordgo.Member, msg *discordgo.Message, args []string) error {
// Strip command name from args and set default role
args = args[1:]
if len(args) < 1 {
args = []string{"premium"}
}
// Serialise args to a role array
// TODO ignore prefixing args?
var roles strings.Builder
for _, role := range args {
if roles.Len() > 0 {
roles.WriteString("&")
}
roles.WriteString("role=" + url.QueryEscape(role))
}
// Default to premium
if roles.Len() == 0 {
return errors.New("No valid roles in list")
}
code, err := get("https://api.impactclient.net/v1/integration/impactbot/genkey?auth=" + os.Getenv("IMPACTBOT_AUTH_SECRET") + "&" + roles.String())
if err != nil {
return err
}
return resp(msg.ChannelID, "Token is `"+code+"`\n[Click here to register an Impact Account](https://impactclient.net/register.html?token="+code+")")
}
func get(url string) (string, error) {
resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", errors.New(string(data))
}
return string(data), nil
}