diff --git a/channel.go b/channel.go index 1608bc2..fdb2bfd 100644 --- a/channel.go +++ b/channel.go @@ -1,21 +1,54 @@ package revoltgo +import ( + "encoding/json" +) + // Channel struct. type Channel struct { Client *Client - Id string `json:"_id"` - ChannelType string `json:"channel_type"` - UserId string `json:"user"` - Nonce string `json:"nonce"` - Active bool `json:"active"` - Recipients []string `json:"recipients"` - LastMessage map[string]string `json:"last_message"` - Name string `json:"name"` - OwnerId string `json:"owner"` - Description string `json:"description"` - Icon *Attachment `json:"icon"` - DefaultPermissions int `json:"default_permissions"` - RolePermissions int `json:"role_permissions"` - Permissions int `json:"permissions"` + Id string `json:"_id"` + ChannelType string `json:"channel_type"` + UserId string `json:"user"` + Nonce string `json:"nonce"` + Active bool `json:"active"` + Recipients []string `json:"recipients"` + LastMessage string `json:"last_message"` + Name string `json:"name"` + OwnerId string `json:"owner"` + Description string `json:"description"` + Icon *Attachment `json:"icon"` + DefaultPermissions int `json:"default_permissions"` + RolePermissions int `json:"role_permissions"` + Permissions int `json:"permissions"` +} + +// Send a message to the channel. +func (c Channel) SendMessage(message *Message) (*Message, error) { + if message.Nonce == "" { + message.Nonce = genULID() + } + + respMessage := &Message{} + msgData, err := json.Marshal(message) + + if err != nil { + return respMessage, err + } + + resp, err := c.Client.Request("POST", "/channels/"+c.Id+"/messages", msgData) + + if err != nil { + return respMessage, err + } + + err = json.Unmarshal(resp, respMessage) + + if err != nil { + return respMessage, err + } + + message.Client = c.Client + return message, nil } diff --git a/client.go b/client.go index 89e438c..7fa85c4 100644 --- a/client.go +++ b/client.go @@ -2,6 +2,7 @@ package revoltgo import ( "encoding/json" + "net/http" "github.com/sacOO7/gowebsocket" ) @@ -15,6 +16,7 @@ const ( type Client struct { Token string Socket gowebsocket.Socket + HTTP *http.Client // Event Functions OnReadyFunction func() @@ -32,10 +34,10 @@ func (c *Client) OnMessage(fn func(message *Message)) { } // Fetch a channel by Id. -func (c *Client) FetchChannel(id string) (*Channel, error) { +func (c Client) FetchChannel(id string) (*Channel, error) { channel := &Channel{} - data, err := c.Request("GET", "/channels/"+id, "") + data, err := c.Request("GET", "/channels/"+id, []byte{}) if err != nil { return channel, err @@ -47,5 +49,6 @@ func (c *Client) FetchChannel(id string) (*Channel, error) { return channel, err } + channel.Client = &c return channel, nil } diff --git a/http.go b/http.go index 53cdee8..d8b0e13 100644 --- a/http.go +++ b/http.go @@ -4,43 +4,33 @@ import ( "bytes" "io/ioutil" "net/http" - "strings" ) // Send http request -func (c *Client) Request(method, path, data string) ([]byte, error) { - var reqBody *bytes.Buffer - - // Check method - if strings.EqualFold(method, "get") { - reqBody = bytes.NewBuffer([]byte(data)) - } else { - reqBody = nil - } +func (c Client) Request(method, path string, data []byte) ([]byte, error) { + reqBody := bytes.NewBuffer(data) // Prepare request req, err := http.NewRequest(method, API_URL+path, reqBody) if err != nil { - return []byte(""), err + return []byte{}, err } req.Header.Set("X-Bot-Token", c.Token) req.Header.Set("Content-Type", "application/json") - client := &http.Client{} - // Send request - resp, err := client.Do(req) + resp, err := c.HTTP.Do(req) if err != nil { - return []byte(""), err + return []byte{}, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - return []byte(""), err + return []byte{}, err } return body, nil diff --git a/utils.go b/utils.go new file mode 100644 index 0000000..2d7d9a1 --- /dev/null +++ b/utils.go @@ -0,0 +1,14 @@ +package revoltgo + +import ( + "math/rand" + "time" + + "github.com/oklog/ulid/v2" +) + +func genULID() string { + t := time.Now() + entropy := ulid.Monotonic(rand.New(rand.NewSource(t.UnixNano())), 0) + return ulid.MustNew(ulid.Timestamp(t), entropy).String() +} diff --git a/websocket.go b/websocket.go index 5ad8493..05bbf0e 100644 --- a/websocket.go +++ b/websocket.go @@ -3,6 +3,7 @@ package revoltgo import ( "encoding/json" "fmt" + "net/http" "time" "github.com/sacOO7/gowebsocket" @@ -16,6 +17,7 @@ type GatewayType struct { func (c *Client) Start() { // Create new socket c.Socket = gowebsocket.New(WS_URL) + c.HTTP = &http.Client{} // Send auth when connected c.Socket.OnConnected = func(_ gowebsocket.Socket) {