github.com/diamondburned/arikawa/v2@v2.1.0/api/user.go (about)

     1  package api
     2  
     3  import (
     4  	"github.com/diamondburned/arikawa/v2/discord"
     5  	"github.com/diamondburned/arikawa/v2/utils/httputil"
     6  	"github.com/diamondburned/arikawa/v2/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  // Note gets the note for the given user. This endpoint is undocumented and
    90  // might only work for user accounts.
    91  func (c *Client) Note(userID discord.UserID) (string, error) {
    92  	var body struct {
    93  		Note string `json:"note"`
    94  	}
    95  
    96  	return body.Note, c.RequestJSON(&body, "GET", EndpointMe+"/notes/"+userID.String())
    97  }
    98  
    99  // SetNote sets a note for the user. This endpoint is undocumented and might
   100  // only work for user accounts.
   101  func (c *Client) SetNote(userID discord.UserID, note string) error {
   102  	var body = struct {
   103  		Note string `json:"note"`
   104  	}{
   105  		Note: note,
   106  	}
   107  
   108  	return c.FastRequest(
   109  		"PUT", EndpointMe+"/notes/"+userID.String(),
   110  		httputil.WithJSONBody(body),
   111  	)
   112  }
   113  
   114  // SetRelationship sets the relationship type between the current user and the
   115  // given user.
   116  func (c *Client) SetRelationship(userID discord.UserID, t discord.RelationshipType) error {
   117  	var body = struct {
   118  		Type discord.RelationshipType `json:"type"`
   119  	}{
   120  		Type: t,
   121  	}
   122  
   123  	return c.FastRequest(
   124  		"PUT", EndpointMe+"/relationships/"+userID.String(),
   125  		httputil.WithJSONBody(body),
   126  	)
   127  }
   128  
   129  // DeleteRelationship deletes the relationship between the current user and the
   130  // given user.
   131  func (c *Client) DeleteRelationship(userID discord.UserID) error {
   132  	return c.FastRequest("DELETE", EndpointMe+"/relationships/"+userID.String())
   133  }