forked from diamondburned/arikawa
/
user.go
133 lines (111 loc) · 4 KB
/
user.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
package api
import (
"github.com/germanoeich/arikawa/v3/discord"
"github.com/germanoeich/arikawa/v3/utils/httputil"
"github.com/germanoeich/arikawa/v3/utils/json/option"
)
var (
EndpointUsers = Endpoint + "users/"
EndpointMe = EndpointUsers + "@me"
)
// User returns a user object for a given user ID.
func (c *Client) User(userID discord.UserID) (*discord.User, error) {
var u *discord.User
return u, c.RequestJSON(&u, "GET", EndpointUsers+userID.String())
}
// Me returns the user object of the requester's account. For OAuth2, this
// requires the identify scope, which will return the object without an email,
// and optionally the email scope, which returns the object with an email.
func (c *Client) Me() (*discord.User, error) {
var me *discord.User
return me, c.RequestJSON(&me, "GET", EndpointMe)
}
// https://discord.com/developers/docs/resources/user#modify-current-user-json-params
type ModifySelfData struct {
// Username is the user's username, if changed may cause the user's
// discriminator to be randomized.
Username option.String `json:"username,omitempty"`
// Avatar modifies the user's avatar.
Avatar *Image `json:"image,omitempty"`
}
// ModifyMe modifies the requester's user account settings.
func (c *Client) ModifyMe(data ModifySelfData) (*discord.User, error) {
var u *discord.User
return u, c.RequestJSON(&u, "PATCH", EndpointMe, httputil.WithJSONBody(data))
}
// ChangeOwnNickname modifies the nickname of the current user in a guild.
//
// Fires a Guild Member Update Gateway event.
func (c *Client) ChangeOwnNickname(
guildID discord.GuildID, nick string) error {
var param struct {
Nick string `json:"nick"`
}
param.Nick = nick
return c.FastRequest(
"PATCH",
EndpointGuilds+guildID.String()+"/members/@me/nick",
httputil.WithJSONBody(param),
)
}
// PrivateChannels returns a list of DM channel objects. For bots, this is no
// longer a supported method of getting recent DMs, and will return an empty
// array.
func (c *Client) PrivateChannels() ([]discord.Channel, error) {
var dms []discord.Channel
return dms, c.RequestJSON(&dms, "GET", EndpointMe+"/channels")
}
// CreatePrivateChannel creates a new DM channel with a user.
func (c *Client) CreatePrivateChannel(recipientID discord.UserID) (*discord.Channel, error) {
var param struct {
RecipientID discord.UserID `json:"recipient_id"`
}
param.RecipientID = recipientID
var dm *discord.Channel
return dm, c.RequestJSON(&dm, "POST", EndpointMe+"/channels", httputil.WithJSONBody(param))
}
// UserConnections returns a list of connection objects. Requires the
// connections OAuth2 scope.
func (c *Client) UserConnections() ([]discord.Connection, error) {
var conn []discord.Connection
return conn, c.RequestJSON(&conn, "GET", EndpointMe+"/connections")
}
// Note gets the note for the given user. This endpoint is undocumented and
// might only work for user accounts.
func (c *Client) Note(userID discord.UserID) (string, error) {
var body struct {
Note string `json:"note"`
}
return body.Note, c.RequestJSON(&body, "GET", EndpointMe+"/notes/"+userID.String())
}
// SetNote sets a note for the user. This endpoint is undocumented and might
// only work for user accounts.
func (c *Client) SetNote(userID discord.UserID, note string) error {
var body = struct {
Note string `json:"note"`
}{
Note: note,
}
return c.FastRequest(
"PUT", EndpointMe+"/notes/"+userID.String(),
httputil.WithJSONBody(body),
)
}
// SetRelationship sets the relationship type between the current user and the
// given user.
func (c *Client) SetRelationship(userID discord.UserID, t discord.RelationshipType) error {
var body = struct {
Type discord.RelationshipType `json:"type"`
}{
Type: t,
}
return c.FastRequest(
"PUT", EndpointMe+"/relationships/"+userID.String(),
httputil.WithJSONBody(body),
)
}
// DeleteRelationship deletes the relationship between the current user and the
// given user.
func (c *Client) DeleteRelationship(userID discord.UserID) error {
return c.FastRequest("DELETE", EndpointMe+"/relationships/"+userID.String())
}