-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathdiscord.go
144 lines (130 loc) · 3.32 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
)
type discordLinkResponse struct {
Status int `json:"status"`
Message string `json:"message"`
}
type discordLinkRequestMethod int
const (
getRequest discordLinkRequestMethod = 0
postRequest = 1
)
func discordLinkURLSimple(handler string) string {
return fmt.Sprintf(
config.OldFrontend+"/discord/%s?k=%s",
handler, config.DonorBotSecret,
)
}
func discordLinkURL(handler string, ept string, qs ...interface{}) string {
return fmt.Sprintf(
"%s&%s",
discordLinkURLSimple(handler),
fmt.Sprintf(ept, qs...),
)
}
func discordLinkRequest(
url string, m discordLinkRequestMethod, postBody *map[string]interface{},
) (*discordLinkResponse, error) {
var resp *http.Response
var err error
if m == getRequest {
resp, err = http.Get(url)
} else {
var jsonB []byte
jsonB, err = json.Marshal(postBody)
if err != nil {
return nil, err
}
resp, err = http.Post(url, "application/json", bytes.NewBuffer(jsonB))
}
if err != nil {
return nil, err
}
var o discordLinkResponse
err = json.NewDecoder(resp.Body).Decode(&o)
if err != nil {
return nil, fmt.Errorf("o9d decode: %v", err)
}
if o.Status != 200 {
return nil, fmt.Errorf("o9d: %d (%s)", resp.StatusCode, o.Message)
}
return &o, nil
}
func discordLinkGet(url string) (*discordLinkResponse, error) {
return discordLinkRequest(url, getRequest, nil)
}
func discordLinkPost(
url string, postBody *map[string]interface{},
) (*discordLinkResponse, error) {
return discordLinkRequest(url, postRequest, postBody)
}
func discordLinkFinish(c *gin.Context) {
defer func() {
getSession(c).Save()
c.Redirect(302, "/settings/discord")
}()
_, err := discordLinkGet(
discordLinkURL(
"oauth.php",
"state=%s&code=%s",
c.Query("state"), c.Query("code"),
),
)
if err != nil {
c.Error(err)
addMessage(c, errorMessage{T(c, "An error occurred.")})
return
}
addMessage(c, successMessage{T(c, "Your account has been linked successfully!")})
}
func discordUnlink(c *gin.Context) {
defer func() {
getSession(c).Save()
c.Redirect(302, "/settings/discord")
}()
ctx := getContext(c)
if ok, _ := CSRF.Validate(ctx.User.ID, c.Query("csrf")); !ok {
addMessage(c, errorMessage{T(c, "Your session has expired. Please try redoing what you were trying to do.")})
return
}
_, err := discordLinkGet(
discordLinkURL("unlink.php", "uid=%d", ctx.User.ID),
)
if err != nil {
c.Error(err)
addMessage(c, errorMessage{T(c, "An error occurred.")})
return
}
addMessage(c, successMessage{T(c, "Your account has been unlinked successfully!")})
}
func discordSubmit(c *gin.Context) {
defer func() {
getSession(c).Save()
c.Redirect(302, "/settings/discord")
}()
ctx := getContext(c)
if ok, _ := CSRF.Validate(ctx.User.ID, c.PostForm("csrf")); !ok {
addMessage(c, errorMessage{T(c, "Your session has expired. Please try redoing what you were trying to do.")})
return
}
_, err := discordLinkPost(
discordLinkURLSimple("role.php"),
&map[string]interface{}{
"uid": ctx.User.ID,
"colour": c.PostForm("colour"),
"name": c.PostForm("name"),
},
)
if err != nil {
c.Error(err)
addMessage(c, errorMessage{T(c, "An error occurred.")})
return
}
addMessage(c, successMessage{T(c, "Your custom role has been edited successfully!")})
}