diff --git a/channel.go b/channel.go index a576d8a..5be2a79 100644 --- a/channel.go +++ b/channel.go @@ -4,11 +4,15 @@ import ( "encoding/json" "fmt" "reflect" + "time" + + "github.com/oklog/ulid/v2" ) // Channel struct. type Channel struct { - Client *Client + Client *Client + CreatedAt time.Time Id string `json:"_id"` ChannelType string `json:"channel_type"` @@ -32,6 +36,18 @@ type FetchedMessages struct { Users []*User `json:"users"` } +// Calculate creation date and edit the struct. +func (c *Channel) CalculateCreationDate() error { + ulid, err := ulid.Parse(c.Id) + + if err != nil { + return err + } + + c.CreatedAt = time.UnixMilli(int64(ulid.Time())) + return nil +} + // Send a message to the channel. func (c Channel) SendMessage(message *SendMessage) (*Message, error) { if message.Nonce == "" { diff --git a/http.go b/http.go index d8b0e13..06e31e3 100644 --- a/http.go +++ b/http.go @@ -2,6 +2,7 @@ package revoltgo import ( "bytes" + "fmt" "io/ioutil" "net/http" ) @@ -26,6 +27,10 @@ func (c Client) Request(method, path string, data []byte) ([]byte, error) { return []byte{}, err } + if !(resp.StatusCode >= 200 && resp.StatusCode < 300) { + return []byte{}, fmt.Errorf(resp.Status) + } + defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) diff --git a/message.go b/message.go index e46d9b3..d3c06fe 100644 --- a/message.go +++ b/message.go @@ -1,8 +1,11 @@ package revoltgo +import "time" + // Message struct type Message struct { - Client *Client + Client *Client + CreatedAt time.Time Id string `json:"_id"` Nonce string `json:"nonce"` diff --git a/other.go b/other.go index 6896621..bac0a07 100644 --- a/other.go +++ b/other.go @@ -45,10 +45,10 @@ func (sms *SendMessage) CreateNonce() *SendMessage { // Edit channel struct. // Please see: https://developers.revolt.chat/api/#tag/Channel-Information/paths/~1channels~1:channel/patch for more information. type EditChannel struct { - Name string `json:"name"` - Description string `json:"description"` - Icon string `json:"icon"` - Remove string `json:"remove"` + Name string `json:"name,omitempty"` + Description string `json:"description,omitempty"` + Icon string `json:"icon,omitempty"` + Remove string `json:"remove,omitempty"` } // Set name for struct. diff --git a/user.go b/user.go index 39264b9..cc927e1 100644 --- a/user.go +++ b/user.go @@ -1,8 +1,11 @@ package revoltgo +import "time" + // User struct. type User struct { - Client *Client + Client *Client + CreatedAt time.Time Id string `json:"_id"` Username string `json:"username"` diff --git a/utils.go b/utils.go index 2d7d9a1..55e2ada 100644 --- a/utils.go +++ b/utils.go @@ -10,5 +10,6 @@ import ( func genULID() string { t := time.Now() entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0) + return ulid.MustNew(ulid.Timestamp(t), entropy).String() }