This repository has been archived by the owner on Feb 23, 2023. It is now read-only.
forked from bwmarrin/discordgo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth2.go
127 lines (100 loc) · 3.69 KB
/
oauth2.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
// Discordgo - Discord bindings for Go
// Available at https://github.com/bwmarrin/discordgo
// Copyright 2015-2016 Bruce Marriner <bruce@sqls.net>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// This file contains functions related to Discord OAuth2 endpoints
package discordgo
// ------------------------------------------------------------------------------------------------
// Code specific to Discord OAuth2 Applications
// ------------------------------------------------------------------------------------------------
// An Application struct stores values for a Discord OAuth2 Application
type Application struct {
ID string `json:"id,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
Icon string `json:"icon,omitempty"`
Secret string `json:"secret,omitempty"`
RedirectURIs *[]string `json:"redirect_uris,omitempty"`
}
// Application returns an Application structure of a specific Application
// appID : The ID of an Application
func (s *Session) Application(appID string) (st *Application, err error) {
body, err := s.Request("GET", APPLICATION(appID), nil)
if err != nil {
return
}
err = unmarshal(body, &st)
return
}
// Applications returns all applications for the authenticated user
func (s *Session) Applications() (st []*Application, err error) {
body, err := s.Request("GET", APPLICATIONS, nil)
if err != nil {
return
}
err = unmarshal(body, &st)
return
}
// ApplicationCreate creates a new Application
// name : Name of Application / Bot
// uris : Redirect URIs (Not required)
func (s *Session) ApplicationCreate(ap *Application) (st *Application, err error) {
data := struct {
Name string `json:"name"`
Description string `json:"description"`
RedirectURIs *[]string `json:"redirect_uris,omitempty"`
}{ap.Name, ap.Description, ap.RedirectURIs}
body, err := s.Request("POST", APPLICATIONS, data)
if err != nil {
return
}
err = unmarshal(body, &st)
return
}
// ApplicationUpdate updates an existing Application
// var : desc
func (s *Session) ApplicationUpdate(appID string, ap *Application) (st *Application, err error) {
data := struct {
Name string `json:"name"`
Description string `json:"description"`
RedirectURIs *[]string `json:"redirect_uris,omitempty"`
}{ap.Name, ap.Description, ap.RedirectURIs}
body, err := s.Request("PUT", APPLICATION(appID), data)
if err != nil {
return
}
err = unmarshal(body, &st)
return
}
// ApplicationDelete deletes an existing Application
// appID : The ID of an Application
func (s *Session) ApplicationDelete(appID string) (err error) {
_, err = s.Request("DELETE", APPLICATION(appID), nil)
if err != nil {
return
}
return
}
// ------------------------------------------------------------------------------------------------
// Code specific to Discord OAuth2 Application Bots
// ------------------------------------------------------------------------------------------------
// ApplicationBotCreate creates an Application Bot Account
//
// appID : The ID of an Application
// token : The authentication Token for a user account to convert into
// a bot account. This is optional, if omited a new account
// is created using the name of the application.
//
// NOTE: func name may change, if I can think up something better.
func (s *Session) ApplicationBotCreate(appID, token string) (st *User, err error) {
data := struct {
Token string `json:"token,omitempty"`
}{token}
body, err := s.Request("POST", APPLICATIONS_BOT(appID), data)
if err != nil {
return
}
err = unmarshal(body, &st)
return
}