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.)")
|
||||
}
|
||||
|
||||
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 {
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
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()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
fmt.Println(string(body))
|
||||
|
||||
if err != nil {
|
||||
return []byte{}, err
|
||||
|
226
websocket.go
226
websocket.go
@ -14,9 +14,14 @@ func (c *Client) Start() {
|
||||
c.Socket = gowebsocket.New(WS_URL)
|
||||
c.HTTP = &http.Client{}
|
||||
|
||||
// Auth the user if self-bot.
|
||||
// if c.SelfBot != nil {
|
||||
// c.Auth()
|
||||
// }
|
||||
|
||||
// Send auth when connected
|
||||
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) {
|
||||
@ -35,108 +40,8 @@ func (c *Client) Start() {
|
||||
go c.ping()
|
||||
}
|
||||
|
||||
// Check events
|
||||
if rawData.Type == "Ready" && c.OnReadyFunctions != nil {
|
||||
// Ready Event
|
||||
for _, i := range c.OnReadyFunctions {
|
||||
i()
|
||||
}
|
||||
} else if rawData.Type == "Message" && c.OnMessageFunctions != nil {
|
||||
// Message Event
|
||||
msgData := &Message{}
|
||||
err := json.Unmarshal([]byte(message), msgData)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
msgData.Client = c
|
||||
|
||||
for _, i := range c.OnMessageFunctions {
|
||||
i(msgData)
|
||||
}
|
||||
} else if rawData.Type == "MessageUpdate" && c.OnMessageUpdateFunctions != nil {
|
||||
// Message Update Event
|
||||
data := &struct {
|
||||
ChannelId string `json:"channel"`
|
||||
MessageId string `json:"id"`
|
||||
Payload map[string]interface{} `json:"data"`
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), data)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
for _, i := range c.OnMessageUpdateFunctions {
|
||||
i(data.ChannelId, data.MessageId, data.Payload)
|
||||
}
|
||||
} else if rawData.Type == "MessageDelete" && c.OnMessageDeleteFunctions != nil {
|
||||
// Message Delete Event
|
||||
data := &struct {
|
||||
ChannelId string `json:"channel"`
|
||||
MessageId string `json:"id"`
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), data)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
for _, i := range c.OnMessageDeleteFunctions {
|
||||
i(data.ChannelId, data.MessageId)
|
||||
}
|
||||
} else if rawData.Type == "ChannelCreate" && c.OnChannelCreateFunctions != nil {
|
||||
// Channel create event.
|
||||
channelData := &Channel{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), channelData)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
channelData.Client = c
|
||||
|
||||
for _, i := range c.OnChannelCreateFunctions {
|
||||
i(channelData)
|
||||
}
|
||||
} else if rawData.Type == "ChannelUpdate" && c.OnChannelUpdateFunctions != nil {
|
||||
// Channel update event.
|
||||
data := &struct {
|
||||
ChannelId string `json:"id"`
|
||||
Clear string `json:"clear"`
|
||||
Payload map[string]interface{} `json:"data"`
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), data)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
for _, i := range c.OnChannelUpdateFunctions {
|
||||
i(data.ChannelId, data.Clear, data.Payload)
|
||||
}
|
||||
} else if rawData.Type == "ChannelDelete" && c.OnChannelDeleteFunctions != nil {
|
||||
// Channel delete event.
|
||||
data := &struct {
|
||||
ChannelId string `json:"id"`
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), data)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
for _, i := range c.OnChannelDeleteFunctions {
|
||||
i(data.ChannelId)
|
||||
}
|
||||
}
|
||||
|
||||
// Handle events
|
||||
c.handleEvents(rawData, message)
|
||||
fmt.Println(message)
|
||||
}
|
||||
|
||||
@ -144,6 +49,15 @@ func (c *Client) Start() {
|
||||
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()
|
||||
@ -156,3 +70,109 @@ func (c *Client) ping() {
|
||||
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 {
|
||||
// Ready Event
|
||||
for _, i := range c.OnReadyFunctions {
|
||||
i()
|
||||
}
|
||||
} else if rawData.Type == "Message" && c.OnMessageFunctions != nil {
|
||||
// Message Event
|
||||
msgData := &Message{}
|
||||
err := json.Unmarshal([]byte(message), msgData)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
msgData.Client = c
|
||||
|
||||
for _, i := range c.OnMessageFunctions {
|
||||
i(msgData)
|
||||
}
|
||||
} else if rawData.Type == "MessageUpdate" && c.OnMessageUpdateFunctions != nil {
|
||||
// Message Update Event
|
||||
data := &struct {
|
||||
ChannelId string `json:"channel"`
|
||||
MessageId string `json:"id"`
|
||||
Payload map[string]interface{} `json:"data"`
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), data)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
for _, i := range c.OnMessageUpdateFunctions {
|
||||
i(data.ChannelId, data.MessageId, data.Payload)
|
||||
}
|
||||
} else if rawData.Type == "MessageDelete" && c.OnMessageDeleteFunctions != nil {
|
||||
// Message Delete Event
|
||||
data := &struct {
|
||||
ChannelId string `json:"channel"`
|
||||
MessageId string `json:"id"`
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), data)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
for _, i := range c.OnMessageDeleteFunctions {
|
||||
i(data.ChannelId, data.MessageId)
|
||||
}
|
||||
} else if rawData.Type == "ChannelCreate" && c.OnChannelCreateFunctions != nil {
|
||||
// Channel create event.
|
||||
channelData := &Channel{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), channelData)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
channelData.Client = c
|
||||
|
||||
for _, i := range c.OnChannelCreateFunctions {
|
||||
i(channelData)
|
||||
}
|
||||
} else if rawData.Type == "ChannelUpdate" && c.OnChannelUpdateFunctions != nil {
|
||||
// Channel update event.
|
||||
data := &struct {
|
||||
ChannelId string `json:"id"`
|
||||
Clear string `json:"clear"`
|
||||
Payload map[string]interface{} `json:"data"`
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), data)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
for _, i := range c.OnChannelUpdateFunctions {
|
||||
i(data.ChannelId, data.Clear, data.Payload)
|
||||
}
|
||||
} else if rawData.Type == "ChannelDelete" && c.OnChannelDeleteFunctions != nil {
|
||||
// Channel delete event.
|
||||
data := &struct {
|
||||
ChannelId string `json:"id"`
|
||||
}{}
|
||||
|
||||
err := json.Unmarshal([]byte(message), data)
|
||||
|
||||
if err != nil {
|
||||
fmt.Printf("Unexcepted Error: %s", err)
|
||||
}
|
||||
|
||||
for _, i := range c.OnChannelDeleteFunctions {
|
||||
i(data.ChannelId)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user