added fetch messages.
This commit is contained in:
parent
2c758edf11
commit
068bc86828
56
channel.go
56
channel.go
@ -2,6 +2,8 @@ package revoltgo
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
// Channel struct.
|
||||
@ -24,8 +26,19 @@ type Channel struct {
|
||||
Permissions int `json:"permissions"`
|
||||
}
|
||||
|
||||
// Similar to message, but created for sendmessage function.
|
||||
type SendMessageStruct struct {
|
||||
Content string `json:"content"`
|
||||
Attachments []string `json:"attachments"`
|
||||
Nonce string `json:"nonce"`
|
||||
Replies []struct {
|
||||
Id string `json:"id"`
|
||||
Mention bool `json:"mention"`
|
||||
}
|
||||
}
|
||||
|
||||
// Send a message to the channel.
|
||||
func (c Channel) SendMessage(message *Message) (*Message, error) {
|
||||
func (c Channel) SendMessage(message *SendMessageStruct) (*Message, error) {
|
||||
if message.Nonce == "" {
|
||||
message.Nonce = genULID()
|
||||
}
|
||||
@ -49,6 +62,43 @@ func (c Channel) SendMessage(message *Message) (*Message, error) {
|
||||
return respMessage, err
|
||||
}
|
||||
|
||||
message.Client = c.Client
|
||||
return message, nil
|
||||
respMessage.Client = c.Client
|
||||
return respMessage, nil
|
||||
}
|
||||
|
||||
// Fetch messages from channel.
|
||||
// Check: https://developers.revolt.chat/api/#tag/Messaging/paths/~1channels~1:channel~1messages/get for map parameters.
|
||||
func (c Channel) FetchMessages(options map[string]interface{}) ([]*Message, error) {
|
||||
// Format url
|
||||
url := "/channels/" + c.Id + "/messages?"
|
||||
|
||||
for key, value := range options {
|
||||
if !reflect.ValueOf(value).IsZero() {
|
||||
url += fmt.Sprintf("%s=%v&", key, value)
|
||||
}
|
||||
}
|
||||
|
||||
url = url[:len(url)-1]
|
||||
|
||||
fetchedMsgs := []*Message{}
|
||||
|
||||
// Send request
|
||||
resp, err := c.Client.Request("GET", url, []byte{})
|
||||
|
||||
if err != nil {
|
||||
return fetchedMsgs, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(resp, &fetchedMsgs)
|
||||
|
||||
if err != nil {
|
||||
return fetchedMsgs, err
|
||||
}
|
||||
|
||||
// Add client to users & messages
|
||||
for _, msg := range fetchedMsgs {
|
||||
msg.Client = c.Client
|
||||
}
|
||||
|
||||
return fetchedMsgs, nil
|
||||
}
|
||||
|
@ -8,7 +8,8 @@ type Message struct {
|
||||
Nonce string `json:"nonce"`
|
||||
ChannelId string `json:"channel"`
|
||||
AuthorId string `json:"author"`
|
||||
Content string `json:"content"`
|
||||
Content interface{} `json:"content"`
|
||||
Embeds []*MessageEmbed `json:"embeds"`
|
||||
Attachments []*Attachment `json:"attachments"`
|
||||
Mentions []string `json:"mentions"`
|
||||
Replies []string `json:"replies"`
|
||||
@ -22,7 +23,6 @@ type Attachment struct {
|
||||
FileName string `json:"filename"`
|
||||
Metadata *AttachmentMetadata
|
||||
ContentType string `json:"content_type"`
|
||||
Embeds []*MessageEmbed
|
||||
}
|
||||
|
||||
// Attachment metadata struct.
|
||||
|
Loading…
x
Reference in New Issue
Block a user