added auth function.

This commit is contained in:
5elenay 2021-08-24 09:59:41 +03:00
parent b527ca2758
commit 026950ebcf

View File

@ -2,6 +2,7 @@ package revoltgo
import ( import (
"encoding/json" "encoding/json"
"fmt"
"net/http" "net/http"
"github.com/sacOO7/gowebsocket" "github.com/sacOO7/gowebsocket"
@ -12,11 +13,12 @@ const (
API_URL = "https://api.revolt.chat" API_URL = "https://api.revolt.chat"
) )
// Client struct // Client struct.
type Client struct { type Client struct {
Token string SelfBot *SelfBot
Socket gowebsocket.Socket Token string
HTTP *http.Client Socket gowebsocket.Socket
HTTP *http.Client
// Event Functions // Event Functions
OnReadyFunctions []func() OnReadyFunctions []func()
@ -28,6 +30,15 @@ type Client struct {
OnChannelDeleteFunctions []func(channel_id string) 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. // On ready event will run when websocket connection is started and bot is ready to work.
func (c *Client) OnReady(fn func()) { func (c *Client) OnReady(fn func()) {
c.OnReadyFunctions = append(c.OnReadyFunctions, fn) c.OnReadyFunctions = append(c.OnReadyFunctions, fn)
@ -141,3 +152,24 @@ func (c *Client) CreateServer(name, description string) (*Server, error) {
return server, nil 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
}