added create date calculator for channel.

This commit is contained in:
5elenay 2021-08-22 17:45:45 +03:00
parent f80532ef53
commit b7cc1c0d5f
6 changed files with 35 additions and 7 deletions

View File

@ -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 == "" {

View File

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

View File

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

View File

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

View File

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

View File

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