1
0
Fork 0
mirror of https://github.com/diamondburned/arikawa.git synced 2024-11-28 09:42:58 +00:00
arikawa/api/user.go

124 lines
3.7 KiB
Go
Raw Normal View History

2020-01-06 03:48:39 +00:00
package api
2020-01-06 05:22:26 +00:00
import (
2020-10-28 22:39:59 +00:00
"github.com/diamondburned/arikawa/v2/discord"
"github.com/diamondburned/arikawa/v2/utils/httputil"
"github.com/diamondburned/arikawa/v2/utils/json/option"
2020-01-06 05:22:26 +00:00
)
2020-01-06 03:48:39 +00:00
var (
EndpointUsers = Endpoint + "users/"
EndpointMe = EndpointUsers + "@me"
)
2020-01-06 03:48:39 +00:00
2020-05-11 22:06:19 +00:00
// User returns a user object for a given user ID.
func (c *Client) User(userID discord.UserID) (*discord.User, error) {
2020-01-06 03:48:39 +00:00
var u *discord.User
2020-05-12 02:05:08 +00:00
return u, c.RequestJSON(&u, "GET", EndpointUsers+userID.String())
2020-01-06 03:48:39 +00:00
}
2020-05-11 22:06:19 +00:00
// 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.
2020-01-06 03:48:39 +00:00
func (c *Client) Me() (*discord.User, error) {
var me *discord.User
return me, c.RequestJSON(&me, "GET", EndpointMe)
}
2020-05-11 22:06:19 +00:00
// https://discord.com/developers/docs/resources/user#modify-current-user-json-params
2020-01-06 03:48:39 +00:00
type ModifySelfData struct {
2020-05-11 22:06:19 +00:00
// 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"`
2020-01-06 03:48:39 +00:00
}
2020-05-11 22:06:19 +00:00
// ModifyMe modifies the requester's user account settings.
2020-01-06 03:48:39 +00:00
func (c *Client) ModifyMe(data ModifySelfData) (*discord.User, error) {
var u *discord.User
2020-05-11 22:06:19 +00:00
return u, c.RequestJSON(&u, "PATCH", EndpointMe, httputil.WithJSONBody(data))
2020-01-06 03:48:39 +00:00
}
2020-05-15 17:29:04 +00:00
// 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 {
2020-05-15 17:29:04 +00:00
var param struct {
Nick string `json:"nick"`
}
param.Nick = nick
return c.FastRequest(
"PATCH",
EndpointGuilds+guildID.String()+"/members/@me/nick",
httputil.WithJSONBody(param),
)
}
2020-05-11 22:06:19 +00:00
// 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")
}
2020-05-11 22:06:19 +00:00
// 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"`
}
2020-05-11 22:06:19 +00:00
param.RecipientID = recipientID
var dm *discord.Channel
2020-05-12 02:05:08 +00:00
return dm, c.RequestJSON(&dm, "POST", EndpointMe+"/channels", httputil.WithJSONBody(param))
}
2020-05-15 17:31:23 +00:00
// 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")
2020-05-15 18:38:22 +00:00
}
2020-07-11 21:27:03 +00:00
// 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 {
2020-07-11 21:27:03 +00:00
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())
}