-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.go
202 lines (157 loc) · 4.61 KB
/
channel.go
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package revolt
import (
"encoding/json"
"fmt"
"reflect"
"time"
"github.com/oklog/ulid/v2"
)
// Channel
type Channel struct {
Client *Client
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"`
NSFW bool `json:"nsfw"`
Icon *Attachment `json:"icon"`
DefaultPermissions uint `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
}
// Send a message to the channel.
func (c Channel) SendMessage(message *SendMessage) (*Message, error) {
if message.Nonce == "" {
message.CreateNonce()
}
respMessage := &Message{}
respMessage.Client = c.Client
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
}
if message.DeleteAfter != 0 {
go func() {
time.Sleep(time.Second * time.Duration(message.DeleteAfter))
respMessage.Delete()
}()
}
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{}) (*FetchedMessages, 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 := &FetchedMessages{}
// Send request
resp, err := c.Client.Request("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
}
}
// Add client to users & messages
for _, msg := range fetchedMsgs.Messages {
msg.Client = c.Client
}
if fetchedMsgs.Users != nil {
for _, msg := range fetchedMsgs.Users {
msg.Client = c.Client
}
}
return fetchedMsgs, nil
}
// Fetch a message from channel by Id.
func (c Channel) FetchMessage(id string) (*Message, error) {
msg := &Message{}
msg.Client = c.Client
resp, err := c.Client.Request("GET", "/channels/"+c.Id+"/messages/"+id, []byte{})
if err != nil {
return msg, err
}
err = json.Unmarshal(resp, msg)
return msg, err
}
// Edit channel.
func (c Channel) Edit(ec *EditChannel) error {
data, err := json.Marshal(ec)
if err != nil {
return err
}
_, err = c.Client.Request("PATCH", "/channels/"+c.Id, data)
return err
}
// Delete channel.
func (c Channel) Delete() error {
_, err := c.Client.Request("DELETE", "/channels/"+c.Id, []byte{})
return err
}
// Create a new invite.
// Returns a string (invite code) and error (nil if not exists).
func (c Channel) CreateInvite() (string, error) {
data, err := c.Client.Request("POST", "/channels/"+c.Id+"/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 Channel) SetPermissions(role_id string, permissions uint) error {
if role_id == "" {
role_id = "default"
}
_, err := c.Client.Request("PUT", "/channels/"+c.Id+"/permissions/"+role_id, []byte(fmt.Sprintf("{\"permissions\":%d}", permissions)))
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))
}