added message update, delete events.
This commit is contained in:
		
							
								
								
									
										16
									
								
								client.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								client.go
									
									
									
									
									
								
							| @@ -19,8 +19,10 @@ type Client struct { | |||||||
| 	HTTP   *http.Client | 	HTTP   *http.Client | ||||||
|  |  | ||||||
| 	// Event Functions | 	// Event Functions | ||||||
| 	OnReadyFunction   func() | 	OnReadyFunction         func() | ||||||
| 	OnMessageFunction func(message *Message) | 	OnMessageFunction       func(message *Message) | ||||||
|  | 	OnMessageUpdateFunction func(channel_id, message_id string, payload map[string]interface{}) | ||||||
|  | 	OnMessageDeleteFunction func(channel_id, message_id string) | ||||||
| } | } | ||||||
|  |  | ||||||
| // 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. | ||||||
| @@ -33,6 +35,16 @@ func (c *Client) OnMessage(fn func(message *Message)) { | |||||||
| 	c.OnMessageFunction = fn | 	c.OnMessageFunction = fn | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // On message update event will run when someone updates a message. | ||||||
|  | func (c *Client) OnMessageUpdate(fn func(channel_id, message_id string, payload map[string]interface{})) { | ||||||
|  | 	c.OnMessageUpdateFunction = fn | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // On message delete event will run when someone deletes a message. | ||||||
|  | func (c *Client) OnMessageDelete(fn func(channel_id, message_id string)) { | ||||||
|  | 	c.OnMessageDeleteFunction = fn | ||||||
|  | } | ||||||
|  |  | ||||||
| // Fetch a channel by Id. | // Fetch a channel by Id. | ||||||
| func (c Client) FetchChannel(id string) (*Channel, error) { | func (c Client) FetchChannel(id string) (*Channel, error) { | ||||||
| 	channel := &Channel{} | 	channel := &Channel{} | ||||||
|   | |||||||
| @@ -9,6 +9,7 @@ type Message struct { | |||||||
| 	ChannelId   string          `json:"channel"` | 	ChannelId   string          `json:"channel"` | ||||||
| 	AuthorId    string          `json:"author"` | 	AuthorId    string          `json:"author"` | ||||||
| 	Content     interface{}     `json:"content"` | 	Content     interface{}     `json:"content"` | ||||||
|  | 	Edited      interface{}     `json:"edited"` | ||||||
| 	Embeds      []*MessageEmbed `json:"embeds"` | 	Embeds      []*MessageEmbed `json:"embeds"` | ||||||
| 	Attachments []*Attachment   `json:"attachments"` | 	Attachments []*Attachment   `json:"attachments"` | ||||||
| 	Mentions    []string        `json:"mentions"` | 	Mentions    []string        `json:"mentions"` | ||||||
|   | |||||||
							
								
								
									
										31
									
								
								websocket.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								websocket.go
									
									
									
									
									
								
							| @@ -37,8 +37,10 @@ func (c *Client) Start() { | |||||||
|  |  | ||||||
| 		// Check events | 		// Check events | ||||||
| 		if rawData.Type == "Ready" && c.OnReadyFunction != nil { | 		if rawData.Type == "Ready" && c.OnReadyFunction != nil { | ||||||
|  | 			// Ready Event | ||||||
| 			c.OnReadyFunction() | 			c.OnReadyFunction() | ||||||
| 		} else if rawData.Type == "Message" && c.OnMessageFunction != nil { | 		} else if rawData.Type == "Message" && c.OnMessageFunction != nil { | ||||||
|  | 			// Message Event | ||||||
| 			msgData := &Message{} | 			msgData := &Message{} | ||||||
| 			err := json.Unmarshal([]byte(message), msgData) | 			err := json.Unmarshal([]byte(message), msgData) | ||||||
|  |  | ||||||
| @@ -48,6 +50,35 @@ func (c *Client) Start() { | |||||||
|  |  | ||||||
| 			msgData.Client = c | 			msgData.Client = c | ||||||
| 			c.OnMessageFunction(msgData) | 			c.OnMessageFunction(msgData) | ||||||
|  | 		} else if rawData.Type == "MessageUpdate" && c.OnMessageUpdateFunction != 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) | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			c.OnMessageUpdateFunction(data.ChannelId, data.MessageId, data.Payload) | ||||||
|  | 		} else if rawData.Type == "MessageDelete" && c.OnMessageDeleteFunction != 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) | ||||||
|  | 			} | ||||||
|  |  | ||||||
|  | 			c.OnMessageDeleteFunction(data.ChannelId, data.MessageId) | ||||||
| 		} | 		} | ||||||
|  |  | ||||||
| 		fmt.Println(message) | 		fmt.Println(message) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user