added channel events.
This commit is contained in:
		
							
								
								
									
										18
									
								
								client.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								client.go
									
									
									
									
									
								
							| @@ -23,6 +23,9 @@ type Client struct { | ||||
| 	OnMessageFunctions       []func(message *Message) | ||||
| 	OnMessageUpdateFunctions []func(channel_id, message_id string, payload map[string]interface{}) | ||||
| 	OnMessageDeleteFunctions []func(channel_id, message_id string) | ||||
| 	OnChannelCreateFunctions []func(channel *Channel) | ||||
| 	OnChannelUpdateFunctions []func(channel_id, clear string, payload map[string]interface{}) | ||||
| 	OnChannelDeleteFunctions []func(channel_id string) | ||||
| } | ||||
|  | ||||
| // On ready event will run when websocket connection is started and bot is ready to work. | ||||
| @@ -45,6 +48,21 @@ func (c *Client) OnMessageDelete(fn func(channel_id, message_id string)) { | ||||
| 	c.OnMessageDeleteFunctions = append(c.OnMessageDeleteFunctions, fn) | ||||
| } | ||||
|  | ||||
| // On channel create event will run when someone creates a channel. | ||||
| func (c *Client) OnChannelCreate(fn func(channel *Channel)) { | ||||
| 	c.OnChannelCreateFunctions = append(c.OnChannelCreateFunctions, fn) | ||||
| } | ||||
|  | ||||
| // On channel update event will run when someone updates a channel. | ||||
| func (c *Client) OnChannelUpdate(fn func(channel_id, clear string, payload map[string]interface{})) { | ||||
| 	c.OnChannelUpdateFunctions = append(c.OnChannelUpdateFunctions, fn) | ||||
| } | ||||
|  | ||||
| // On channel delete event will run when someone deletes a channel. | ||||
| func (c *Client) OnChannelDelete(fn func(channel_id string)) { | ||||
| 	c.OnChannelDeleteFunctions = append(c.OnChannelDeleteFunctions, fn) | ||||
| } | ||||
|  | ||||
| // Fetch a channel by Id. | ||||
| func (c *Client) FetchChannel(id string) (*Channel, error) { | ||||
| 	channel := &Channel{} | ||||
|   | ||||
							
								
								
									
										16
									
								
								other.go
									
									
									
									
									
								
							
							
						
						
									
										16
									
								
								other.go
									
									
									
									
									
								
							| @@ -2,13 +2,13 @@ package revoltgo | ||||
|  | ||||
| // Similar to message, but created for send message function. | ||||
| type SendMessage struct { | ||||
| 	Content     string   `json:"content"` | ||||
| 	Attachments []string `json:"attachments"` | ||||
| 	Nonce       string   `json:"nonce"` | ||||
| 	Content     string   `json:"content,omitempty"` | ||||
| 	Attachments []string `json:"attachments,omitempty"` | ||||
| 	Nonce       string   `json:"nonce,omitempty"` | ||||
| 	Replies     []struct { | ||||
| 		Id      string `json:"id"` | ||||
| 		Mention bool   `json:"mention"` | ||||
| 	} | ||||
| 		Id      string `json:"id,omitempty"` | ||||
| 		Mention bool   `json:"mention,omitempty"` | ||||
| 	} `json:"replies,omitempty"` | ||||
| } | ||||
|  | ||||
| // Set content. | ||||
| @@ -26,8 +26,8 @@ func (sms *SendMessage) AddAttachment(attachment string) *SendMessage { | ||||
| // Add a new reply. | ||||
| func (sms *SendMessage) AddReply(id string, mention bool) *SendMessage { | ||||
| 	sms.Replies = append(sms.Replies, struct { | ||||
| 		Id      string "json:\"id\"" | ||||
| 		Mention bool   "json:\"mention\"" | ||||
| 		Id      string "json:\"id,omitempty\"" | ||||
| 		Mention bool   "json:\"mention,omitempty\"" | ||||
| 	}{ | ||||
| 		Id:      id, | ||||
| 		Mention: mention, | ||||
|   | ||||
							
								
								
									
										47
									
								
								websocket.go
									
									
									
									
									
								
							
							
						
						
									
										47
									
								
								websocket.go
									
									
									
									
									
								
							| @@ -88,6 +88,53 @@ func (c *Client) Start() { | ||||
| 			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) | ||||
| 			} | ||||
| 		} | ||||
|  | ||||
| 		fmt.Println(message) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user