added create date calculator for channel.
This commit is contained in:
		
							
								
								
									
										18
									
								
								channel.go
									
									
									
									
									
								
							
							
						
						
									
										18
									
								
								channel.go
									
									
									
									
									
								
							| @@ -4,11 +4,15 @@ import ( | |||||||
| 	"encoding/json" | 	"encoding/json" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"reflect" | 	"reflect" | ||||||
|  | 	"time" | ||||||
|  |  | ||||||
|  | 	"github.com/oklog/ulid/v2" | ||||||
| ) | ) | ||||||
|  |  | ||||||
| // Channel struct. | // Channel struct. | ||||||
| type Channel struct { | type Channel struct { | ||||||
| 	Client *Client | 	Client    *Client | ||||||
|  | 	CreatedAt time.Time | ||||||
|  |  | ||||||
| 	Id                 string      `json:"_id"` | 	Id                 string      `json:"_id"` | ||||||
| 	ChannelType        string      `json:"channel_type"` | 	ChannelType        string      `json:"channel_type"` | ||||||
| @@ -32,6 +36,18 @@ type FetchedMessages struct { | |||||||
| 	Users    []*User    `json:"users"` | 	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. | // Send a message to the channel. | ||||||
| func (c Channel) SendMessage(message *SendMessage) (*Message, error) { | func (c Channel) SendMessage(message *SendMessage) (*Message, error) { | ||||||
| 	if message.Nonce == "" { | 	if message.Nonce == "" { | ||||||
|   | |||||||
							
								
								
									
										5
									
								
								http.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								http.go
									
									
									
									
									
								
							| @@ -2,6 +2,7 @@ package revoltgo | |||||||
|  |  | ||||||
| import ( | import ( | ||||||
| 	"bytes" | 	"bytes" | ||||||
|  | 	"fmt" | ||||||
| 	"io/ioutil" | 	"io/ioutil" | ||||||
| 	"net/http" | 	"net/http" | ||||||
| ) | ) | ||||||
| @@ -26,6 +27,10 @@ func (c Client) Request(method, path string, data []byte) ([]byte, error) { | |||||||
| 		return []byte{}, err | 		return []byte{}, err | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | 	if !(resp.StatusCode >= 200 && resp.StatusCode < 300) { | ||||||
|  | 		return []byte{}, fmt.Errorf(resp.Status) | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	defer resp.Body.Close() | 	defer resp.Body.Close() | ||||||
| 	body, err := ioutil.ReadAll(resp.Body) | 	body, err := ioutil.ReadAll(resp.Body) | ||||||
|  |  | ||||||
|   | |||||||
| @@ -1,8 +1,11 @@ | |||||||
| package revoltgo | package revoltgo | ||||||
|  |  | ||||||
|  | import "time" | ||||||
|  |  | ||||||
| // Message struct | // Message struct | ||||||
| type Message struct { | type Message struct { | ||||||
| 	Client *Client | 	Client    *Client | ||||||
|  | 	CreatedAt time.Time | ||||||
|  |  | ||||||
| 	Id          string          `json:"_id"` | 	Id          string          `json:"_id"` | ||||||
| 	Nonce       string          `json:"nonce"` | 	Nonce       string          `json:"nonce"` | ||||||
|   | |||||||
							
								
								
									
										8
									
								
								other.go
									
									
									
									
									
								
							
							
						
						
									
										8
									
								
								other.go
									
									
									
									
									
								
							| @@ -45,10 +45,10 @@ func (sms *SendMessage) CreateNonce() *SendMessage { | |||||||
| // Edit channel struct. | // Edit channel struct. | ||||||
| // Please see: https://developers.revolt.chat/api/#tag/Channel-Information/paths/~1channels~1:channel/patch for more information. | // Please see: https://developers.revolt.chat/api/#tag/Channel-Information/paths/~1channels~1:channel/patch for more information. | ||||||
| type EditChannel struct { | type EditChannel struct { | ||||||
| 	Name        string `json:"name"` | 	Name        string `json:"name,omitempty"` | ||||||
| 	Description string `json:"description"` | 	Description string `json:"description,omitempty"` | ||||||
| 	Icon        string `json:"icon"` | 	Icon        string `json:"icon,omitempty"` | ||||||
| 	Remove      string `json:"remove"` | 	Remove      string `json:"remove,omitempty"` | ||||||
| } | } | ||||||
|  |  | ||||||
| // Set name for struct. | // Set name for struct. | ||||||
|   | |||||||
							
								
								
									
										5
									
								
								user.go
									
									
									
									
									
								
							
							
						
						
									
										5
									
								
								user.go
									
									
									
									
									
								
							| @@ -1,8 +1,11 @@ | |||||||
| package revoltgo | package revoltgo | ||||||
|  |  | ||||||
|  | import "time" | ||||||
|  |  | ||||||
| // User struct. | // User struct. | ||||||
| type User struct { | type User struct { | ||||||
| 	Client *Client | 	Client    *Client | ||||||
|  | 	CreatedAt time.Time | ||||||
|  |  | ||||||
| 	Id             string           `json:"_id"` | 	Id             string           `json:"_id"` | ||||||
| 	Username       string           `json:"username"` | 	Username       string           `json:"username"` | ||||||
|   | |||||||
							
								
								
									
										1
									
								
								utils.go
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								utils.go
									
									
									
									
									
								
							| @@ -10,5 +10,6 @@ import ( | |||||||
| func genULID() string { | func genULID() string { | ||||||
| 	t := time.Now() | 	t := time.Now() | ||||||
| 	entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0) | 	entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0) | ||||||
|  |  | ||||||
| 	return ulid.MustNew(ulid.Timestamp(t), entropy).String() | 	return ulid.MustNew(ulid.Timestamp(t), entropy).String() | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user