github.com/diamondburned/arikawa@v1.3.14/api/user.go (about)

     1  package api
     2  
     3  import (
     4  	"github.com/diamondburned/arikawa/discord"
     5  	"github.com/diamondburned/arikawa/utils/httputil"
     6  	"github.com/diamondburned/arikawa/utils/json/option"
     7  )
     8  
     9  var (
    10  	EndpointUsers = Endpoint + "users/"
    11  	EndpointMe    = EndpointUsers + "@me"
    12  )
    13  
    14  // User returns a user object for a given user ID.
    15  func (c *Client) User(userID discord.UserID) (*discord.User, error) {
    16  	var u *discord.User
    17  	return u, c.RequestJSON(&u, "GET", EndpointUsers+userID.String())
    18  }
    19  
    20  // Me returns the user object of the requester's account. For OAuth2, this
    21  // requires the identify scope, which will return the object without an email,
    22  // and optionally the email scope, which returns the object with an email.
    23  func (c *Client) Me() (*discord.User, error) {
    24  	var me *discord.User
    25  	return me, c.RequestJSON(&me, "GET", EndpointMe)
    26  }
    27  
    28  // https://discord.com/developers/docs/resources/user#modify-current-user-json-params
    29  type ModifySelfData struct {
    30  	// Username is the user's username, if changed may cause the user's
    31  	// discriminator to be randomized.
    32  	Username option.String `json:"username,omitempty"`
    33  	// Avatar modifies the user's avatar.
    34  	Avatar *Image `json:"image,omitempty"`
    35  }
    36  
    37  // ModifyMe modifies the requester's user account settings.
    38  func (c *Client) ModifyMe(data ModifySelfData) (*discord.User, error) {
    39  	var u *discord.User
    40  	return u, c.RequestJSON(&u, "PATCH", EndpointMe, httputil.WithJSONBody(data))
    41  }
    42  
    43  // ChangeOwnNickname modifies the nickname of the current user in a guild.
    44  //
    45  // Fires a Guild Member Update Gateway event.
    46  func (c *Client) ChangeOwnNickname(
    47  	guildID discord.GuildID, nick string) error {
    48  
    49  	var param struct {
    50  		Nick string `json:"nick"`
    51  	}
    52  
    53  	param.Nick = nick
    54  
    55  	return c.FastRequest(
    56  		"PATCH",
    57  		EndpointGuilds+guildID.String()+"/members/@me/nick",
    58  		httputil.WithJSONBody(param),
    59  	)
    60  }
    61  
    62  // PrivateChannels returns a list of DM channel objects. For bots, this is no
    63  // longer a supported method of getting recent DMs, and will return an empty
    64  // array.
    65  func (c *Client) PrivateChannels() ([]discord.Channel, error) {
    66  	var dms []discord.Channel
    67  	return dms, c.RequestJSON(&dms, "GET", EndpointMe+"/channels")
    68  }
    69  
    70  // CreatePrivateChannel creates a new DM channel with a user.
    71  func (c *Client) CreatePrivateChannel(recipientID discord.UserID) (*discord.Channel, error) {
    72  	var param struct {
    73  		RecipientID discord.UserID `json:"recipient_id"`
    74  	}
    75  
    76  	param.RecipientID = recipientID
    77  
    78  	var dm *discord.Channel
    79  	return dm, c.RequestJSON(&dm, "POST", EndpointMe+"/channels", httputil.WithJSONBody(param))
    80  }
    81  
    82  // UserConnections returns a list of connection objects. Requires the
    83  // connections OAuth2 scope.
    84  func (c *Client) UserConnections() ([]discord.Connection, error) {
    85  	var conn []discord.Connection
    86  	return conn, c.RequestJSON(&conn, "GET", EndpointMe+"/connections")
    87  }
    88  
    89  // SetNote sets a note for the user. This endpoint is undocumented and might
    90  // only work for user accounts.
    91  func (c *Client) SetNote(userID discord.UserID, note string) error {
    92  	var body = struct {
    93  		Note string `json:"note"`
    94  	}{
    95  		Note: note,
    96  	}
    97  
    98  	return c.FastRequest(
    99  		"PUT", EndpointMe+"/notes/"+userID.String(),
   100  		httputil.WithJSONBody(body),
   101  	)
   102  }
   103  
   104  // SetRelationship sets the relationship type between the current user and the
   105  // given user.
   106  func (c *Client) SetRelationship(userID discord.UserID, t discord.RelationshipType) error {
   107  	var body = struct {
   108  		Type discord.RelationshipType `json:"type"`
   109  	}{
   110  		Type: t,
   111  	}
   112  
   113  	return c.FastRequest(
   114  		"PUT", EndpointMe+"/relationships/"+userID.String(),
   115  		httputil.WithJSONBody(body),
   116  	)
   117  }
   118  
   119  // DeleteRelationship deletes the relationship between the current user and the
   120  // given user.
   121  func (c *Client) DeleteRelationship(userID discord.UserID) error {
   122  	return c.FastRequest("DELETE", EndpointMe+"/relationships/"+userID.String())
   123  }