added message update, delete events.
This commit is contained in:
		
							
								
								
									
										12
									
								
								client.go
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								client.go
									
									
									
									
									
								
							| @@ -21,6 +21,8 @@ type Client struct { | ||||
| 	// Event Functions | ||||
| 	OnReadyFunction         func() | ||||
| 	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. | ||||
| @@ -33,6 +35,16 @@ func (c *Client) OnMessage(fn func(message *Message)) { | ||||
| 	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. | ||||
| func (c Client) FetchChannel(id string) (*Channel, error) { | ||||
| 	channel := &Channel{} | ||||
|   | ||||
| @@ -9,6 +9,7 @@ type Message struct { | ||||
| 	ChannelId   string          `json:"channel"` | ||||
| 	AuthorId    string          `json:"author"` | ||||
| 	Content     interface{}     `json:"content"` | ||||
| 	Edited      interface{}     `json:"edited"` | ||||
| 	Embeds      []*MessageEmbed `json:"embeds"` | ||||
| 	Attachments []*Attachment   `json:"attachments"` | ||||
| 	Mentions    []string        `json:"mentions"` | ||||
|   | ||||
							
								
								
									
										31
									
								
								websocket.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								websocket.go
									
									
									
									
									
								
							| @@ -37,8 +37,10 @@ func (c *Client) Start() { | ||||
|  | ||||
| 		// Check events | ||||
| 		if rawData.Type == "Ready" && c.OnReadyFunction != nil { | ||||
| 			// Ready Event | ||||
| 			c.OnReadyFunction() | ||||
| 		} else if rawData.Type == "Message" && c.OnMessageFunction != nil { | ||||
| 			// Message Event | ||||
| 			msgData := &Message{} | ||||
| 			err := json.Unmarshal([]byte(message), msgData) | ||||
|  | ||||
| @@ -48,6 +50,35 @@ func (c *Client) Start() { | ||||
|  | ||||
| 			msgData.Client = c | ||||
| 			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) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user