-
Notifications
You must be signed in to change notification settings - Fork 36
/
url.go
52 lines (44 loc) · 1.27 KB
/
url.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
package discord
import (
"fmt"
"net/url"
"strings"
"github.com/disgoorg/snowflake/v2"
)
// QueryValues holds key value pairs of query values
type QueryValues map[string]any
// Encode encodes the QueryValues into a string to append to the url
func (q QueryValues) Encode() string {
values := url.Values{}
for k, v := range q {
values.Set(k, fmt.Sprint(v))
}
return values.Encode()
}
func urlPrint(url string, params ...any) string {
for _, param := range params {
start := strings.Index(url, "{")
end := strings.Index(url, "}")
if start == -1 || end == -1 {
break
}
url = url[:start] + fmt.Sprint(param) + url[end+1:]
}
return url
}
// InviteURL formats the invite code as an url
func InviteURL(code string) string {
return urlPrint("https://discord.gg/{code}", code)
}
// WebhookURL returns the url over which the webhook can be called
func WebhookURL(webhookID snowflake.ID, webhookToken string) string {
return urlPrint("https://discord.com/api/webhooks/{webhook.id}/{webhook.token}", webhookID, webhookToken)
}
// AuthorizeURL returns the OAuth2 authorize url with the given query params
func AuthorizeURL(values QueryValues) string {
query := values.Encode()
if query != "" {
query = "?" + query
}
return "https://discord.com/oauth2/authorize" + query
}