/
register.go
63 lines (47 loc) · 1.69 KB
/
register.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
package commands
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/pazuzu156/atlas"
)
// Register command.
type Register struct{ Command }
// InitRegister initializes the login command.
func InitRegister() Register {
return Register{Init(&CommandItem{
Name: "register",
Description: "Starts the bot's registration flow",
})}
}
// RegisterResponse represents the login API response body.
type RegisterResponse struct {
Token string `json:"request_token"`
Expires int32 `json:"expires"`
ExpiresString string `json:"expires_string"`
Error bool `json:"error"`
ErrorMessage string `json:"message"`
}
// Register registers and runs the login command.
func (c Register) Register() *atlas.Command {
c.CommandInterface.Run = func(ctx atlas.Context) {
res, err := http.Get(fmt.Sprintf("%s/login/request_token/%s", config.Website.APIURL, ctx.Message.Author.ID.String()))
if err != nil {
ctx.Message.Reply(ctx.Context, ctx.Atlas, "An error occurred when attempting to communitate with the authentication server. Please try again later")
return
}
defer res.Body.Close()
var lr RegisterResponse
body, _ := ioutil.ReadAll(res.Body)
json.Unmarshal(body, &lr)
if lr.Error == true {
ctx.Message.Reply(ctx.Context, ctx.Atlas, lr.ErrorMessage)
return
}
url := fmt.Sprintf("%s/auth/authenticate/%s/%s", config.Website.AppURL, ctx.Message.Author.ID.String(), lr.Token)
ctx.Message.Reply(ctx.Context, ctx.Atlas, fmt.Sprintf("Your login request was received. Use this link to begin the login process: %s", url))
ctx.Message.Reply(ctx.Context, ctx.Atlas, fmt.Sprintf("This link %s", lr.ExpiresString))
}
return c.CommandInterface
}