From 026950ebcffa006e0344d19b2a968207e5c390aa Mon Sep 17 00:00:00 2001 From: 5elenay <5elenay@protonmail.com> Date: Tue, 24 Aug 2021 09:59:41 +0300 Subject: [PATCH] added auth function. --- client.go | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/client.go b/client.go index f60c72d..5c91a3d 100644 --- a/client.go +++ b/client.go @@ -2,6 +2,7 @@ package revoltgo import ( "encoding/json" + "fmt" "net/http" "github.com/sacOO7/gowebsocket" @@ -12,11 +13,12 @@ const ( API_URL = "https://api.revolt.chat" ) -// Client struct +// Client struct. type Client struct { - Token string - Socket gowebsocket.Socket - HTTP *http.Client + SelfBot *SelfBot + Token string + Socket gowebsocket.Socket + HTTP *http.Client // Event Functions OnReadyFunctions []func() @@ -28,6 +30,15 @@ type Client struct { OnChannelDeleteFunctions []func(channel_id string) } +// Self bot struct. +type SelfBot struct { + Email string `json:"-"` + Password string `json:"-"` + Id string `json:"id"` + UserId string `json:"user_id"` + SessionToken string `json:"session_token"` +} + // On ready event will run when websocket connection is started and bot is ready to work. func (c *Client) OnReady(fn func()) { c.OnReadyFunctions = append(c.OnReadyFunctions, fn) @@ -141,3 +152,24 @@ func (c *Client) CreateServer(name, description string) (*Server, error) { return server, nil } + +// Auth client user. +func (c *Client) Auth() error { + if c.SelfBot == nil { + return fmt.Errorf("can't auth user (not a self-bot.)") + } + + resp, err := c.Request("POST", "/auth/login", []byte("{\"email\":\""+c.SelfBot.Email+"\",\"password\":\""+c.SelfBot.Password+"\"}")) + + if err != nil { + return err + } + + err = json.Unmarshal(resp, c.SelfBot) + + if err != nil { + return err + } + + return nil +}