-
Notifications
You must be signed in to change notification settings - Fork 3
/
discord.go
111 lines (91 loc) · 2.78 KB
/
discord.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
package main
import (
"encoding/json"
"errors"
"github.com/MinecraftHopper/panel/env"
"net/http"
"net/url"
)
const DiscordEndpoint = "https://discord.com/api/oauth2/token"
const userInfoEndpoint string = "https://discord.com/api/v10/users/"
const UserAgent string = "DiscordBot (https://github.com/MinecraftHopper, v0)"
var NoDiscordUser = errors.New("no discord user")
func redeemCode(code string) (string, error) {
clientId := env.Get("discord.clientid")
clientSecret := env.Get("discord.clientsecret")
redirectUrl := env.Get("web.host") + "/login-callback"
data := map[string]string{
"client_id": clientId,
"client_secret": clientSecret,
"grant_type": "authorization_code",
"code": code,
"redirect_uri": redirectUrl,
"scope": "identify email connections",
}
values := url.Values{}
for k, v := range data {
values.Add(k, v)
}
response, err := HttpClient.PostForm(DiscordEndpoint, values)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", errors.New("invalid response code from Discord (" + response.Status + ")")
}
discordResponse := DiscordOAuth2Response{}
err = json.NewDecoder(response.Body).Decode(&discordResponse)
if err != nil {
return "", err
}
return discordResponse.AccessToken, nil
}
func getUserId(accessToken string) (string, error) {
u, err := url.Parse(userInfoEndpoint + "@me")
if err != nil {
return "", err
}
request := &http.Request{
URL: u,
Header: map[string][]string{"Authorization": {"Bearer " + accessToken}, "User-Agent": {UserAgent}},
}
response, err := HttpClient.Do(request)
if err != nil {
return "", err
}
defer response.Body.Close()
if response.StatusCode != http.StatusOK {
return "", errors.New("invalid response code from Discord (" + response.Status + ")")
}
discordResponse := DiscordUser{}
err = json.NewDecoder(response.Body).Decode(&discordResponse)
if err != nil {
return "", err
}
return discordResponse.Id, nil
}
func getUser(id string) (DiscordUser, error) {
var botToken = env.Get("discord.clientbot")
u, err := url.Parse(userInfoEndpoint + id)
if err != nil {
return DiscordUser{}, err
}
request := &http.Request{
URL: u,
Header: map[string][]string{"Authorization": {"Bot " + botToken}, "User-Agent": {UserAgent}},
}
response, err := HttpClient.Do(request)
if err != nil {
return DiscordUser{}, err
}
defer response.Body.Close()
if response.StatusCode == http.StatusNotFound {
return DiscordUser{}, NoDiscordUser
} else if response.StatusCode != http.StatusOK {
return DiscordUser{}, errors.New("invalid response code from Discord (" + response.Status + ")")
}
discordResponse := DiscordUser{}
err = json.NewDecoder(response.Body).Decode(&discordResponse)
return discordResponse, err
}