added simple cache system.
This commit is contained in:
		@@ -20,7 +20,7 @@ type Channel struct {
 | 
				
			|||||||
	Nonce              string      `json:"nonce"`
 | 
						Nonce              string      `json:"nonce"`
 | 
				
			||||||
	Active             bool        `json:"active"`
 | 
						Active             bool        `json:"active"`
 | 
				
			||||||
	Recipients         []string    `json:"recipients"`
 | 
						Recipients         []string    `json:"recipients"`
 | 
				
			||||||
	LastMessage        string      `json:"last_message"`
 | 
						LastMessage        interface{} `json:"last_message"`
 | 
				
			||||||
	Name               string      `json:"name"`
 | 
						Name               string      `json:"name"`
 | 
				
			||||||
	OwnerId            string      `json:"owner"`
 | 
						OwnerId            string      `json:"owner"`
 | 
				
			||||||
	Description        string      `json:"description"`
 | 
						Description        string      `json:"description"`
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -19,6 +19,7 @@ type Client struct {
 | 
				
			|||||||
	Token   string
 | 
						Token   string
 | 
				
			||||||
	Socket  gowebsocket.Socket
 | 
						Socket  gowebsocket.Socket
 | 
				
			||||||
	HTTP    *http.Client
 | 
						HTTP    *http.Client
 | 
				
			||||||
 | 
						Cache   *Cache
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Event Functions
 | 
						// Event Functions
 | 
				
			||||||
	OnReadyFunctions         []func()
 | 
						OnReadyFunctions         []func()
 | 
				
			||||||
@@ -30,6 +31,14 @@ type Client struct {
 | 
				
			|||||||
	OnChannelDeleteFunctions []func(channel_id string)
 | 
						OnChannelDeleteFunctions []func(channel_id string)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Client cache struct.
 | 
				
			||||||
 | 
					type Cache struct {
 | 
				
			||||||
 | 
						Users    []*User    `json:"users"`
 | 
				
			||||||
 | 
						Servers  []*Server  `json:"servers"`
 | 
				
			||||||
 | 
						Channels []*Channel `json:"channels"`
 | 
				
			||||||
 | 
						Members  []*Member  `json:"members"`
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Self bot struct.
 | 
					// Self bot struct.
 | 
				
			||||||
type SelfBot struct {
 | 
					type SelfBot struct {
 | 
				
			||||||
	Email        string `json:"-"`
 | 
						Email        string `json:"-"`
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										34
									
								
								websocket.go
									
									
									
									
									
								
							
							
						
						
									
										34
									
								
								websocket.go
									
									
									
									
									
								
							@@ -42,7 +42,7 @@ func (c *Client) Start() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
		// Handle events
 | 
							// Handle events
 | 
				
			||||||
		c.handleEvents(rawData, message)
 | 
							c.handleEvents(rawData, message)
 | 
				
			||||||
		fmt.Println(message)
 | 
							// fmt.Println(message)
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	// Start connection
 | 
						// Start connection
 | 
				
			||||||
@@ -75,11 +75,16 @@ func (c *Client) ping() {
 | 
				
			|||||||
func (c *Client) handleEvents(rawData *struct {
 | 
					func (c *Client) handleEvents(rawData *struct {
 | 
				
			||||||
	Type string `json:"type"`
 | 
						Type string `json:"type"`
 | 
				
			||||||
}, message string) {
 | 
					}, message string) {
 | 
				
			||||||
	if rawData.Type == "Ready" && c.OnReadyFunctions != nil {
 | 
						if rawData.Type == "Ready" {
 | 
				
			||||||
 | 
							// Add cache
 | 
				
			||||||
 | 
							c.handleCache(message)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Ready Event
 | 
							// Ready Event
 | 
				
			||||||
 | 
							if c.OnReadyFunctions != nil {
 | 
				
			||||||
			for _, i := range c.OnReadyFunctions {
 | 
								for _, i := range c.OnReadyFunctions {
 | 
				
			||||||
				i()
 | 
									i()
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	} else if rawData.Type == "Message" && c.OnMessageFunctions != nil {
 | 
						} else if rawData.Type == "Message" && c.OnMessageFunctions != nil {
 | 
				
			||||||
		// Message Event
 | 
							// Message Event
 | 
				
			||||||
		msgData := &Message{}
 | 
							msgData := &Message{}
 | 
				
			||||||
@@ -176,3 +181,28 @@ func (c *Client) handleEvents(rawData *struct {
 | 
				
			|||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Client) handleCache(data string) {
 | 
				
			||||||
 | 
						cache := &Cache{}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						err := json.Unmarshal([]byte(data), cache)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							fmt.Printf("Unexcepted Error: %s", err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Add client to users.
 | 
				
			||||||
 | 
						for _, i := range cache.Users {
 | 
				
			||||||
 | 
							i.Client = c
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, i := range cache.Servers {
 | 
				
			||||||
 | 
							i.Client = c
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for _, i := range cache.Channels {
 | 
				
			||||||
 | 
							i.Client = c
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						c.Cache = cache
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user