added multiple event listen support.

This commit is contained in:
5elenay 2021-08-22 04:07:14 +03:00
parent 2c3446fcd8
commit cadd1c6413
2 changed files with 25 additions and 16 deletions

View File

@ -19,30 +19,30 @@ type Client struct {
HTTP *http.Client HTTP *http.Client
// Event Functions // Event Functions
OnReadyFunction func() OnReadyFunctions []func()
OnMessageFunction func(message *Message) OnMessageFunctions []func(message *Message)
OnMessageUpdateFunction func(channel_id, message_id string, payload map[string]interface{}) OnMessageUpdateFunctions []func(channel_id, message_id string, payload map[string]interface{})
OnMessageDeleteFunction func(channel_id, message_id string) OnMessageDeleteFunctions []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.
func (c *Client) OnReady(fn func()) { func (c *Client) OnReady(fn func()) {
c.OnReadyFunction = fn c.OnReadyFunctions = append(c.OnReadyFunctions, fn)
} }
// On message event will run when someone sends a message. // On message event will run when someone sends a message.
func (c *Client) OnMessage(fn func(message *Message)) { func (c *Client) OnMessage(fn func(message *Message)) {
c.OnMessageFunction = fn c.OnMessageFunctions = append(c.OnMessageFunctions, fn)
} }
// On message update event will run when someone updates a message. // 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{})) { func (c *Client) OnMessageUpdate(fn func(channel_id, message_id string, payload map[string]interface{})) {
c.OnMessageUpdateFunction = fn c.OnMessageUpdateFunctions = append(c.OnMessageUpdateFunctions, fn)
} }
// On message delete event will run when someone deletes a message. // On message delete event will run when someone deletes a message.
func (c *Client) OnMessageDelete(fn func(channel_id, message_id string)) { func (c *Client) OnMessageDelete(fn func(channel_id, message_id string)) {
c.OnMessageDeleteFunction = fn c.OnMessageDeleteFunctions = append(c.OnMessageDeleteFunctions, fn)
} }
// Fetch a channel by Id. // Fetch a channel by Id.

View File

@ -36,10 +36,12 @@ func (c *Client) Start() {
} }
// Check events // Check events
if rawData.Type == "Ready" && c.OnReadyFunction != nil { if rawData.Type == "Ready" && c.OnReadyFunctions != nil {
// Ready Event // Ready Event
c.OnReadyFunction() for _, i := range c.OnReadyFunctions {
} else if rawData.Type == "Message" && c.OnMessageFunction != nil { i()
}
} else if rawData.Type == "Message" && c.OnMessageFunctions != nil {
// Message Event // Message Event
msgData := &Message{} msgData := &Message{}
err := json.Unmarshal([]byte(message), msgData) err := json.Unmarshal([]byte(message), msgData)
@ -49,8 +51,11 @@ func (c *Client) Start() {
} }
msgData.Client = c msgData.Client = c
c.OnMessageFunction(msgData)
} else if rawData.Type == "MessageUpdate" && c.OnMessageUpdateFunction != nil { for _, i := range c.OnMessageFunctions {
i(msgData)
}
} else if rawData.Type == "MessageUpdate" && c.OnMessageUpdateFunctions != nil {
// Message Update Event // Message Update Event
data := &struct { data := &struct {
ChannelId string `json:"channel"` ChannelId string `json:"channel"`
@ -64,8 +69,10 @@ func (c *Client) Start() {
fmt.Printf("Unexcepted Error: %s", err) fmt.Printf("Unexcepted Error: %s", err)
} }
c.OnMessageUpdateFunction(data.ChannelId, data.MessageId, data.Payload) for _, i := range c.OnMessageUpdateFunctions {
} else if rawData.Type == "MessageDelete" && c.OnMessageDeleteFunction != nil { i(data.ChannelId, data.MessageId, data.Payload)
}
} else if rawData.Type == "MessageDelete" && c.OnMessageDeleteFunctions != nil {
// Message Delete Event // Message Delete Event
data := &struct { data := &struct {
ChannelId string `json:"channel"` ChannelId string `json:"channel"`
@ -78,7 +85,9 @@ func (c *Client) Start() {
fmt.Printf("Unexcepted Error: %s", err) fmt.Printf("Unexcepted Error: %s", err)
} }
c.OnMessageDeleteFunction(data.ChannelId, data.MessageId) for _, i := range c.OnMessageDeleteFunctions {
i(data.ChannelId, data.MessageId)
}
} }
fmt.Println(message) fmt.Println(message)