added send message function.

This commit is contained in:
5elenay 2021-08-21 00:22:45 +03:00
parent a77f3d097d
commit d025726680
5 changed files with 74 additions and 32 deletions

View File

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

View File

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

22
http.go
View File

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

14
utils.go Normal file
View File

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

View File

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