added self-bot support.
This commit is contained in:
		
							
								
								
									
										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) | ||||
| 		} | ||||
| 	} | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user