1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
package revolt
import (
"context"
"encoding/json"
"fmt"
"net/url"
"time"
"github.com/oklog/ulid/v2"
)
// Channel struct.
type Channel struct {
CreatedAt time.Time
Id string `json:"_id"`
Nonce string `json:"nonce"`
OwnerId string `json:"owner"`
Name string `json:"name"`
Active bool `json:"active"`
Recipients []string `json:"recipients"`
LastMessage interface{} `json:"last_message"`
Description string `json:"description"`
Icon *Attachment `json:"icon"`
DefaultPermissions interface{} `json:"default_permissions"`
RolePermissions interface{} `json:"role_permissions"`
Permissions uint `json:"permissions"`
}
// Fetched messages struct.
type FetchedMessages struct {
Messages []*Message `json:"messages"`
Users []*User `json:"users"`
}
// Calculate creation date and edit the struct.
func (c *Channel) CalculateCreationDate() error {
ulid, err := ulid.Parse(c.Id)
if err != nil {
return err
}
c.CreatedAt = time.UnixMilli(int64(ulid.Time()))
return nil
}
// SendMessage sends a message to a channel.
func (c *Client) ChannelSendMessage(ctx context.Context, channelID string, message *SendMessage) (*Message, error) {
data, err := json.Marshal(message)
if err != nil {
return nil, err
}
resp, err := c.Request(ctx, "POST", "/channels/"+channelID+"/messages", data)
if err != nil {
return nil, err
}
msg := &Message{}
err = json.Unmarshal(resp, msg)
if err != nil {
return nil, err
}
if message.DeleteAfter != 0 {
go func() {
time.Sleep(time.Second * time.Duration(message.DeleteAfter))
c.MessageDelete(ctx, channelID, msg.ID)
}()
}
return msg, nil
}
// Fetch messages from channel.
// Check: https://developers.revolt.chat/api/#tag/Messaging/paths/~1channels~1:channel~1messages/get for map parameters.
func (c *Client) ChannelFetchMessages(ctx context.Context, channelID string, options url.Values) (*FetchedMessages, error) {
// Format url
url := "/channels/" + channelID + "/messages?" + options.Encode()
fetchedMsgs := &FetchedMessages{}
// Send request
resp, err := c.Request(ctx, "GET", url, []byte{})
if err != nil {
return fetchedMsgs, err
}
err = json.Unmarshal(resp, &fetchedMsgs)
if err != nil {
err = json.Unmarshal([]byte(fmt.Sprintf("{\"messages\": %s}", resp)), &fetchedMsgs)
if err != nil {
return fetchedMsgs, err
}
}
return fetchedMsgs, nil
}
// Fetch a message from channel by Id.
func (c *Client) ChannelFetchMessage(ctx context.Context, channelID, id string) (*Message, error) {
msg := &Message{}
resp, err := c.Request(ctx, "GET", "/channels/"+channelID+"/messages/"+id, []byte{})
if err != nil {
return msg, err
}
err = json.Unmarshal(resp, msg)
return msg, err
}
// Edit channel.
func (c *Client) ChannelEdit(ctx context.Context, channelID string, ec *EditChannel) error {
data, err := json.Marshal(ec)
if err != nil {
return err
}
_, err = c.Request(ctx, "PATCH", "/channels/"+channelID, data)
return err
}
// Delete channel.
func (c *Client) ChannelDelete(ctx context.Context, channelID string) error {
_, err := c.Request(ctx, "DELETE", "/channels/"+channelID, []byte{})
return err
}
// Create a new invite.
// Returns a string (invite code) and error (nil if not exists).
func (c *Client) ChannelCreateInvite(ctx context.Context, channelID string) (string, error) {
data, err := c.Request(ctx, "POST", "/channels/"+channelID+"/invites", []byte{})
if err != nil {
return "", err
}
dataStruct := &struct {
InviteCode string `json:"code"`
}{}
err = json.Unmarshal(data, dataStruct)
return dataStruct.InviteCode, err
}
// Set channel permissions for a role.
// Leave role field empty if you want to edit default permissions
func (c *Client) ChannelSetPermissions(ctx context.Context, channelID, roleID string, permissions uint) error {
if roleID == "" {
roleID = "default"
}
data, err := json.Marshal(struct {
Permissions uint `json:"permissions"`
}{Permissions: permissions})
if err != nil {
return err
}
_, err = c.Request(ctx, "PUT", "/channels/"+channelID+"/permissions/"+roleID, data)
return err
}
// // Send a typing start event to the channel.
// func (c *Channel) BeginTyping() {
// c.Client.Socket.SendText(fmt.Sprintf("{\"type\":\"BeginTyping\",\"channel\":\"%s\"}", c.Id))
// }
// // End the typing event in the channel.
// func (c *Channel) EndTyping() {
// c.Client.Socket.SendText(fmt.Sprintf("{\"type\":\"EndTyping\",\"channel\":\"%s\"}", c.Id))
// }
|