-
Notifications
You must be signed in to change notification settings - Fork 1
/
messages_query.go
162 lines (152 loc) · 3.49 KB
/
messages_query.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
package api
import (
"context"
"encoding/json"
"fmt"
"net/url"
"github.com/BOOMfinity/bfcord/discord"
"github.com/andersfylling/snowflake/v5"
)
type _data_ struct {
Around snowflake.ID `json:"around,omitempty"`
After snowflake.ID `json:"after,omitempty"`
Before snowflake.ID `json:"before,omitempty"`
Limit uint16 `json:"limit,omitempty"`
}
type ChannelMessagesQuery struct {
api *Client
channel snowflake.ID
}
func (v ChannelMessagesQuery) Around(id snowflake.ID, limit uint16) (msgs []discord.Message, err error) {
if !id.Valid() {
return nil, nil
}
if limit > 100 {
limit = 100
}
if limit < 1 {
limit = 50
}
raw, err := json.Marshal(_data_{
Limit: limit,
Around: id,
})
if err != nil {
return
}
req := v.api.New(true)
req.SetHost(fmt.Sprintf(FullApiUrl+"/channels/%v/messages", v.channel))
req.SetBody(raw)
err = v.api.DoResult(req, &msgs)
for i := range msgs {
msgs[i].Patch()
}
return
}
func (v ChannelMessagesQuery) After(ctx context.Context, id snowflake.ID, limit uint16) (msgs []discord.Message, err error) {
if !id.Valid() {
return nil, nil
}
if limit < 1 {
limit = 1000
}
fetchLimit := limit
if fetchLimit > 100 {
fetchLimit = 100
}
if fetchLimit < 1 {
fetchLimit = 100
}
params := url.Values{}
params.Set("limit", fmt.Sprint(fetchLimit))
temp := make([]discord.Message, 0, 100)
for uint16(len(msgs)) < limit {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
params.Set("after", id.String())
req := v.api.New(true)
req.SetRequestURI(fmt.Sprintf("%v/channels/%v/messages?%v", FullApiUrl, v.channel.String(), params.Encode()))
err = v.api.DoResult(req, &temp)
if err != nil {
return nil, err
}
id = temp[len(temp)-1].ID
for i := range temp {
temp[i].Patch()
}
msgs = append(msgs, temp...)
if len(temp) < 100 {
break
}
temp = make([]discord.Message, 0, 100)
}
}
return
}
func (v ChannelMessagesQuery) Before(ctx context.Context, id snowflake.ID, limit uint16) (msgs []discord.Message, err error) {
if !id.Valid() {
return nil, nil
}
if limit < 1 {
limit = 1000
}
fetchLimit := limit
if fetchLimit > 100 {
fetchLimit = 100
}
if fetchLimit < 1 {
fetchLimit = 100
}
params := url.Values{}
params.Set("limit", fmt.Sprint(fetchLimit))
temp := make([]discord.Message, 0, 100)
for uint16(len(msgs)) < limit {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
params.Set("before", id.String())
req := v.api.New(true)
req.SetRequestURI(fmt.Sprintf("%v/channels/%v/messages?%v", FullApiUrl, v.channel.String(), params.Encode()))
err = v.api.DoResult(req, &temp)
if err != nil {
return nil, err
}
id = temp[len(temp)-1].ID
for i := range temp {
temp[i].Patch()
}
msgs = append(msgs, temp...)
if len(temp) < 100 {
break
}
temp = make([]discord.Message, 0, 100)
}
}
return
}
func (v ChannelMessagesQuery) Latest(limit uint16) (msgs []discord.Message, err error) {
if limit == 0 {
limit = 50
}
req := v.api.New(true)
req.SetRequestURI(fmt.Sprintf("%v/channels/%v/messages?limit=%v", FullApiUrl, v.channel, limit))
err = v.api.DoResult(req, &msgs)
for i := range msgs {
msgs[i].Patch()
}
return
}
func (v ChannelMessagesQuery) ID() snowflake.ID {
return v.channel
}
func NewChannelMessagesQuery(client *Client, channel snowflake.ID) ChannelMessagesQuery {
data := ChannelMessagesQuery{
channel: channel,
api: client,
}
//data.emptyOptions = emptyOptions[discord.MessageQuery]{data: data}
return data
}