diff --git a/client.go b/client.go index 6cc3e40..a806056 100644 --- a/client.go +++ b/client.go @@ -19,8 +19,10 @@ type Client struct { HTTP *http.Client // Event Functions - OnReadyFunction func() - OnMessageFunction func(message *Message) + 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{} diff --git a/message.go b/message.go index be6784a..e46d9b3 100644 --- a/message.go +++ b/message.go @@ -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"` diff --git a/websocket.go b/websocket.go index 7417d8f..84c1162 100644 --- a/websocket.go +++ b/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)