added self-bot support.
This commit is contained in:
parent
026950ebcf
commit
48123c8295
@ -159,7 +159,7 @@ func (c *Client) Auth() error {
|
|||||||
return fmt.Errorf("can't auth user (not a self-bot.)")
|
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+"\"}"))
|
resp, err := c.Request("POST", "/auth/login", []byte("{\"email\":\""+c.SelfBot.Email+"\",\"password\":\""+c.SelfBot.Password+"\",\"captcha\": \"\"}"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
12
http.go
12
http.go
@ -17,8 +17,15 @@ func (c Client) Request(method, path string, data []byte) ([]byte, error) {
|
|||||||
return []byte{}, err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("X-Bot-Token", c.Token)
|
req.Header.Set("content-type", "application/json")
|
||||||
req.Header.Set("Content-Type", "application/json")
|
|
||||||
|
// Set auth headers
|
||||||
|
if c.SelfBot == nil {
|
||||||
|
req.Header.Set("x-bot-token", c.Token)
|
||||||
|
} else if c.SelfBot.SessionToken != "" && c.SelfBot.UserId != "" {
|
||||||
|
req.Header.Set("x-user-id", c.SelfBot.UserId)
|
||||||
|
req.Header.Set("x-session-token", c.SelfBot.SessionToken)
|
||||||
|
}
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
resp, err := c.HTTP.Do(req)
|
resp, err := c.HTTP.Do(req)
|
||||||
@ -29,6 +36,7 @@ func (c Client) Request(method, path string, data []byte) ([]byte, error) {
|
|||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
fmt.Println(string(body))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte{}, err
|
return []byte{}, err
|
||||||
|
62
websocket.go
62
websocket.go
@ -14,9 +14,14 @@ func (c *Client) Start() {
|
|||||||
c.Socket = gowebsocket.New(WS_URL)
|
c.Socket = gowebsocket.New(WS_URL)
|
||||||
c.HTTP = &http.Client{}
|
c.HTTP = &http.Client{}
|
||||||
|
|
||||||
|
// Auth the user if self-bot.
|
||||||
|
// if c.SelfBot != nil {
|
||||||
|
// c.Auth()
|
||||||
|
// }
|
||||||
|
|
||||||
// Send auth when connected
|
// Send auth when connected
|
||||||
c.Socket.OnConnected = func(_ gowebsocket.Socket) {
|
c.Socket.OnConnected = func(_ gowebsocket.Socket) {
|
||||||
c.Socket.SendText(fmt.Sprintf("{\"type\": \"Authenticate\", \"token\": \"%s\"}", c.Token))
|
c.handleWebsocketAuth()
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Socket.OnTextMessage = func(message string, _ gowebsocket.Socket) {
|
c.Socket.OnTextMessage = func(message string, _ gowebsocket.Socket) {
|
||||||
@ -35,7 +40,41 @@ func (c *Client) Start() {
|
|||||||
go c.ping()
|
go c.ping()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check events
|
// Handle events
|
||||||
|
c.handleEvents(rawData, message)
|
||||||
|
fmt.Println(message)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Start connection
|
||||||
|
c.Socket.Connect()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle on connected.
|
||||||
|
func (c *Client) handleWebsocketAuth() {
|
||||||
|
if c.SelfBot == nil {
|
||||||
|
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Authenticate\",\"token\":\"%s\"}", c.Token))
|
||||||
|
} else {
|
||||||
|
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Authenticate\",\"id\":\"%s\",\"session_token\":\"%s\",\"user_id\":\"%s\"}", c.SelfBot.Id, c.SelfBot.SessionToken, c.SelfBot.UserId))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Destroy the websocket.
|
||||||
|
func (c *Client) Destroy() {
|
||||||
|
c.Socket.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ping websocket.
|
||||||
|
func (c *Client) ping() {
|
||||||
|
for {
|
||||||
|
time.Sleep(30 * time.Second)
|
||||||
|
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Ping\",\"time\":%d}", time.Now().Unix()))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle events.
|
||||||
|
func (c *Client) handleEvents(rawData *struct {
|
||||||
|
Type string `json:"type"`
|
||||||
|
}, message string) {
|
||||||
if rawData.Type == "Ready" && c.OnReadyFunctions != nil {
|
if rawData.Type == "Ready" && c.OnReadyFunctions != nil {
|
||||||
// Ready Event
|
// Ready Event
|
||||||
for _, i := range c.OnReadyFunctions {
|
for _, i := range c.OnReadyFunctions {
|
||||||
@ -136,23 +175,4 @@ func (c *Client) Start() {
|
|||||||
i(data.ChannelId)
|
i(data.ChannelId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(message)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Start connection
|
|
||||||
c.Socket.Connect()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Destroy the websocket.
|
|
||||||
func (c *Client) Destroy() {
|
|
||||||
c.Socket.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// Ping websocket.
|
|
||||||
func (c *Client) ping() {
|
|
||||||
for {
|
|
||||||
time.Sleep(30 * time.Second)
|
|
||||||
c.Socket.SendText(fmt.Sprintf("{\"type\":\"Ping\",\"time\":%d}", time.Now().Unix()))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user