added message update, delete events.

This commit is contained in:
5elenay 2021-08-22 04:03:35 +03:00
parent 2fbd2fe896
commit 2c3446fcd8
3 changed files with 46 additions and 2 deletions

View File

@ -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{}

View File

@ -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"`

View File

@ -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)