added send message function.
This commit is contained in:
		
							
								
								
									
										35
									
								
								channel.go
									
									
									
									
									
								
							
							
						
						
									
										35
									
								
								channel.go
									
									
									
									
									
								
							| @@ -1,5 +1,9 @@ | ||||
| package revoltgo | ||||
|  | ||||
| import ( | ||||
| 	"encoding/json" | ||||
| ) | ||||
|  | ||||
| // Channel struct. | ||||
| type Channel struct { | ||||
| 	Client *Client | ||||
| @@ -10,7 +14,7 @@ type Channel struct { | ||||
| 	Nonce              string      `json:"nonce"` | ||||
| 	Active             bool        `json:"active"` | ||||
| 	Recipients         []string    `json:"recipients"` | ||||
| 	LastMessage        map[string]string `json:"last_message"` | ||||
| 	LastMessage        string      `json:"last_message"` | ||||
| 	Name               string      `json:"name"` | ||||
| 	OwnerId            string      `json:"owner"` | ||||
| 	Description        string      `json:"description"` | ||||
| @@ -19,3 +23,32 @@ type Channel struct { | ||||
| 	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 | ||||
| } | ||||
|   | ||||
| @@ -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
									
									
									
									
									
								
							
							
						
						
									
										22
									
								
								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 | ||||
|   | ||||
							
								
								
									
										14
									
								
								utils.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								utils.go
									
									
									
									
									
										Normal 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() | ||||
| } | ||||
| @@ -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) { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user