-
Notifications
You must be signed in to change notification settings - Fork 35
/
main.go
113 lines (96 loc) · 3.08 KB
/
main.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
112
113
package main
import (
"math/rand"
"net/http"
"os"
"strconv"
"github.com/disgoorg/disgo"
"github.com/disgoorg/disgo/bot"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/oauth2"
"github.com/disgoorg/json"
"github.com/disgoorg/log"
)
var (
letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
token = os.Getenv("disgo_token")
clientSecret = os.Getenv("disgo_client_secret")
baseURL = os.Getenv("disgo_base_url")
client bot.Client
oAuth2Client oauth2.Client
)
func main() {
log.SetLevel(log.LevelDebug)
log.Info("starting example...")
log.Infof("disgo %s", disgo.Version)
var err error
client, err = disgo.New(token)
if err != nil {
log.Panic(err)
}
_, _ = client.Rest().UpdateApplicationRoleConnectionMetadata(client.ApplicationID(), []discord.ApplicationRoleConnectionMetadata{
{
Type: discord.ApplicationRoleConnectionMetadataTypeIntegerGreaterThanOrEqual,
Key: "cookies_eaten",
Name: "Cookies Eaten",
Description: "How many cookies have you eaten?",
},
})
oAuth2Client = oauth2.New(client.ApplicationID(), clientSecret)
mux := http.NewServeMux()
mux.HandleFunc("/verify", handleVerify)
mux.HandleFunc("/callback", handleCallback)
_ = http.ListenAndServe(":6969", mux)
}
func handleVerify(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, oAuth2Client.GenerateAuthorizationURL(baseURL+"/callback", discord.PermissionsNone, 0, false, discord.OAuth2ScopeIdentify, discord.OAuth2ScopeRoleConnectionsWrite), http.StatusTemporaryRedirect)
}
func handleCallback(w http.ResponseWriter, r *http.Request) {
var (
query = r.URL.Query()
code = query.Get("code")
state = query.Get("state")
)
if code != "" && state != "" {
identifier := randStr(32)
session, err := oAuth2Client.StartSession(code, state, identifier)
if err != nil {
writeError(w, "error while starting session", err)
return
}
user, err := oAuth2Client.GetUser(session)
if err != nil {
writeError(w, "error while getting user", err)
return
}
_, err = oAuth2Client.UpdateApplicationRoleConnection(session, client.ApplicationID(), discord.ApplicationRoleConnectionUpdate{
PlatformName: json.Ptr("Cookie Monster " + user.Username),
PlatformUsername: json.Ptr("Cookie Monster " + user.Tag()),
Metadata: &map[string]string{
"cookies_eaten": strconv.Itoa(rand.Intn(100)),
},
})
if err != nil {
writeError(w, "error while updating role connection", err)
return
}
metadata, err := oAuth2Client.GetApplicationRoleConnection(session, client.ApplicationID())
if err != nil {
writeError(w, "error while getting role connection", err)
return
}
data, _ := json.MarshalIndent(metadata, "", "\t")
_, _ = w.Write([]byte("updated role connection:\n" + string(data)))
}
}
func writeError(w http.ResponseWriter, text string, err error) {
w.WriteHeader(http.StatusInternalServerError)
_, _ = w.Write([]byte(text + ": " + err.Error()))
}
func randStr(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}