added update, delete server events.

This commit is contained in:
5elenay 2021-08-27 11:44:24 +03:00
parent 8e9cf13cc3
commit dc094ceb03
2 changed files with 45 additions and 1 deletions

@ -32,6 +32,8 @@ type Client struct {
OnUnknownEventFunctions []func(message string)
OnChannelStartTypingFunctions []func(channel_id, user_id string)
OnChannelStopTypingFunctions []func(channel_id, user_id string)
OnServerUpdateFunctions []func(server_id, clear string, payload map[string]interface{})
OnServerDeleteFunctions []func(server_id string)
}
// Client cache struct.
@ -96,11 +98,21 @@ func (c *Client) OnChannelStartTyping(fn func(channel_id, user_id string)) {
c.OnChannelStartTypingFunctions = append(c.OnChannelStartTypingFunctions, fn)
}
// On channel stıp typing will run when someone stops the typing status.
// On channel stop typing will run when someone stops the typing status.
func (c *Client) OnChannelStopTyping(fn func(channel_id, user_id string)) {
c.OnChannelStopTypingFunctions = append(c.OnChannelStopTypingFunctions, fn)
}
// On server update will run when someone updates a server.
func (c *Client) OnServerUpdate(fn func(server_id, clear string, payload map[string]interface{})) {
c.OnServerUpdateFunctions = append(c.OnServerUpdateFunctions, fn)
}
// On server delete will run when someone deletes a server.
func (c *Client) OnServerDelete(fn func(server_id string)) {
c.OnServerDeleteFunctions = append(c.OnServerDeleteFunctions, fn)
}
// Fetch a channel by Id.
func (c *Client) FetchChannel(id string) (*Channel, error) {
channel := &Channel{}

@ -211,6 +211,38 @@ func (c *Client) handleEvents(rawData *struct {
for _, i := range c.OnChannelStopTypingFunctions {
i(data.ChannelId, data.UserId)
}
} else if rawData.Type == "ServerUpdate" && c.OnServerUpdateFunctions != nil {
// Server update event.
data := &struct {
ServerId 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.OnServerUpdateFunctions {
i(data.ServerId, data.Clear, data.Payload)
}
} else if rawData.Type == "ServerDelete" && c.OnServerDeleteFunctions != nil {
// Server delete event.
data := &struct {
ServerId string `json:"id"`
}{}
err := json.Unmarshal([]byte(message), data)
if err != nil {
fmt.Printf("Unexcepted Error: %s", err)
}
for _, i := range c.OnServerDeleteFunctions {
i(data.ServerId)
}
} else {
// Unknown event.
if c.OnUnknownEventFunctions != nil {