added send message function.
This commit is contained in:
parent
a77f3d097d
commit
d025726680
61
channel.go
61
channel.go
@ -1,21 +1,54 @@
|
|||||||
package revoltgo
|
package revoltgo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
)
|
||||||
|
|
||||||
// Channel struct.
|
// Channel struct.
|
||||||
type Channel struct {
|
type Channel struct {
|
||||||
Client *Client
|
Client *Client
|
||||||
|
|
||||||
Id string `json:"_id"`
|
Id string `json:"_id"`
|
||||||
ChannelType string `json:"channel_type"`
|
ChannelType string `json:"channel_type"`
|
||||||
UserId string `json:"user"`
|
UserId string `json:"user"`
|
||||||
Nonce string `json:"nonce"`
|
Nonce string `json:"nonce"`
|
||||||
Active bool `json:"active"`
|
Active bool `json:"active"`
|
||||||
Recipients []string `json:"recipients"`
|
Recipients []string `json:"recipients"`
|
||||||
LastMessage map[string]string `json:"last_message"`
|
LastMessage string `json:"last_message"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
OwnerId string `json:"owner"`
|
OwnerId string `json:"owner"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
Icon *Attachment `json:"icon"`
|
Icon *Attachment `json:"icon"`
|
||||||
DefaultPermissions int `json:"default_permissions"`
|
DefaultPermissions int `json:"default_permissions"`
|
||||||
RolePermissions int `json:"role_permissions"`
|
RolePermissions int `json:"role_permissions"`
|
||||||
Permissions int `json:"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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
"github.com/sacOO7/gowebsocket"
|
"github.com/sacOO7/gowebsocket"
|
||||||
)
|
)
|
||||||
@ -15,6 +16,7 @@ const (
|
|||||||
type Client struct {
|
type Client struct {
|
||||||
Token string
|
Token string
|
||||||
Socket gowebsocket.Socket
|
Socket gowebsocket.Socket
|
||||||
|
HTTP *http.Client
|
||||||
|
|
||||||
// Event Functions
|
// Event Functions
|
||||||
OnReadyFunction func()
|
OnReadyFunction func()
|
||||||
@ -32,10 +34,10 @@ func (c *Client) OnMessage(fn func(message *Message)) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Fetch a channel by Id.
|
// Fetch a channel by Id.
|
||||||
func (c *Client) FetchChannel(id string) (*Channel, error) {
|
func (c Client) FetchChannel(id string) (*Channel, error) {
|
||||||
channel := &Channel{}
|
channel := &Channel{}
|
||||||
|
|
||||||
data, err := c.Request("GET", "/channels/"+id, "")
|
data, err := c.Request("GET", "/channels/"+id, []byte{})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return channel, err
|
return channel, err
|
||||||
@ -47,5 +49,6 @@ func (c *Client) FetchChannel(id string) (*Channel, error) {
|
|||||||
return channel, err
|
return channel, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
channel.Client = &c
|
||||||
return channel, nil
|
return channel, nil
|
||||||
}
|
}
|
||||||
|
22
http.go
22
http.go
@ -4,43 +4,33 @@ import (
|
|||||||
"bytes"
|
"bytes"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Send http request
|
// Send http request
|
||||||
func (c *Client) Request(method, path, data string) ([]byte, error) {
|
func (c Client) Request(method, path string, data []byte) ([]byte, error) {
|
||||||
var reqBody *bytes.Buffer
|
reqBody := bytes.NewBuffer(data)
|
||||||
|
|
||||||
// Check method
|
|
||||||
if strings.EqualFold(method, "get") {
|
|
||||||
reqBody = bytes.NewBuffer([]byte(data))
|
|
||||||
} else {
|
|
||||||
reqBody = nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Prepare request
|
// Prepare request
|
||||||
req, err := http.NewRequest(method, API_URL+path, reqBody)
|
req, err := http.NewRequest(method, API_URL+path, reqBody)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte(""), err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
req.Header.Set("X-Bot-Token", c.Token)
|
req.Header.Set("X-Bot-Token", c.Token)
|
||||||
req.Header.Set("Content-Type", "application/json")
|
req.Header.Set("Content-Type", "application/json")
|
||||||
|
|
||||||
client := &http.Client{}
|
|
||||||
|
|
||||||
// Send request
|
// Send request
|
||||||
resp, err := client.Do(req)
|
resp, err := c.HTTP.Do(req)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte(""), err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
body, err := ioutil.ReadAll(resp.Body)
|
body, err := ioutil.ReadAll(resp.Body)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []byte(""), err
|
return []byte{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return body, nil
|
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 (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/sacOO7/gowebsocket"
|
"github.com/sacOO7/gowebsocket"
|
||||||
@ -16,6 +17,7 @@ type GatewayType struct {
|
|||||||
func (c *Client) Start() {
|
func (c *Client) Start() {
|
||||||
// Create new socket
|
// Create new socket
|
||||||
c.Socket = gowebsocket.New(WS_URL)
|
c.Socket = gowebsocket.New(WS_URL)
|
||||||
|
c.HTTP = &http.Client{}
|
||||||
|
|
||||||
// Send auth when connected
|
// Send auth when connected
|
||||||
c.Socket.OnConnected = func(_ gowebsocket.Socket) {
|
c.Socket.OnConnected = func(_ gowebsocket.Socket) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user