@zygout/eris 0.0.0-stage → 0.19.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +108 -2
- package/esm.mjs +72 -0
- package/index.d.ts +4407 -0
- package/index.js +75 -0
- package/lib/Client.js +4708 -0
- package/lib/Constants.d.ts +990 -0
- package/lib/Constants.js +1163 -0
- package/lib/command/Command.js +552 -0
- package/lib/command/CommandClient.js +460 -0
- package/lib/errors/DiscordHTTPError.js +63 -0
- package/lib/errors/DiscordRESTError.js +77 -0
- package/lib/gateway/Shard.js +3029 -0
- package/lib/gateway/ShardManager.js +164 -0
- package/lib/rest/Endpoints.js +172 -0
- package/lib/rest/RequestHandler.js +459 -0
- package/lib/structures/ApplicationCommand.js +125 -0
- package/lib/structures/AutoModerationRule.js +143 -0
- package/lib/structures/AutocompleteInteraction.js +80 -0
- package/lib/structures/Base.js +81 -0
- package/lib/structures/CategoryChannel.js +77 -0
- package/lib/structures/Channel.js +98 -0
- package/lib/structures/CommandInteraction.js +473 -0
- package/lib/structures/ComponentInteraction.js +538 -0
- package/lib/structures/DMChannel.js +278 -0
- package/lib/structures/Entitlement.js +77 -0
- package/lib/structures/ExtendedUser.js +46 -0
- package/lib/structures/ForumChannel.js +24 -0
- package/lib/structures/GroupChannel.js +106 -0
- package/lib/structures/Guild.js +1677 -0
- package/lib/structures/GuildAuditLogEntry.js +190 -0
- package/lib/structures/GuildChannel.js +128 -0
- package/lib/structures/GuildIntegration.js +99 -0
- package/lib/structures/GuildPreview.js +97 -0
- package/lib/structures/GuildScheduledEvent.js +176 -0
- package/lib/structures/GuildTemplate.js +76 -0
- package/lib/structures/GuildTextableChannel.js +260 -0
- package/lib/structures/Interaction.js +133 -0
- package/lib/structures/Invite.js +143 -0
- package/lib/structures/Lobby.js +108 -0
- package/lib/structures/LobbyMember.js +59 -0
- package/lib/structures/MediaChannel.js +216 -0
- package/lib/structures/Member.js +371 -0
- package/lib/structures/Message.js +708 -0
- package/lib/structures/ModalSubmitInteraction.js +478 -0
- package/lib/structures/NewsChannel.js +34 -0
- package/lib/structures/NewsThreadChannel.js +12 -0
- package/lib/structures/Permission.js +80 -0
- package/lib/structures/PermissionOverwrite.js +32 -0
- package/lib/structures/PingInteraction.js +39 -0
- package/lib/structures/PrivateThreadChannel.js +25 -0
- package/lib/structures/PublicThreadChannel.js +26 -0
- package/lib/structures/Role.js +163 -0
- package/lib/structures/SKU.js +80 -0
- package/lib/structures/SoundboardSound.js +79 -0
- package/lib/structures/StageChannel.js +59 -0
- package/lib/structures/StageInstance.js +55 -0
- package/lib/structures/Subscription.js +69 -0
- package/lib/structures/TextChannel.js +202 -0
- package/lib/structures/ThreadChannel.js +143 -0
- package/lib/structures/ThreadMember.js +59 -0
- package/lib/structures/UnavailableGuild.js +26 -0
- package/lib/structures/UnknownInteraction.js +502 -0
- package/lib/structures/User.js +296 -0
- package/lib/structures/VoiceChannel.js +166 -0
- package/lib/structures/VoiceState.js +84 -0
- package/lib/util/BrowserWebSocket.js +76 -0
- package/lib/util/Bucket.js +89 -0
- package/lib/util/Collection.js +202 -0
- package/lib/util/MultipartData.js +70 -0
- package/lib/util/Opus.js +33 -0
- package/lib/util/SequentialBucket.js +83 -0
- package/lib/util/emitDeprecation.js +26 -0
- package/lib/voice/Piper.js +300 -0
- package/lib/voice/SharedStream.js +238 -0
- package/lib/voice/VoiceConnection.js +1440 -0
- package/lib/voice/VoiceConnectionManager.js +151 -0
- package/lib/voice/VoiceDataStream.js +22 -0
- package/lib/voice/streams/BaseTransformer.js +40 -0
- package/lib/voice/streams/DCAOpusTransformer.js +78 -0
- package/lib/voice/streams/FFmpegDuplex.js +192 -0
- package/lib/voice/streams/FFmpegOggTransformer.js +35 -0
- package/lib/voice/streams/FFmpegPCMTransformer.js +26 -0
- package/lib/voice/streams/OggOpusTransformer.js +108 -0
- package/lib/voice/streams/PCMOpusTransformer.js +61 -0
- package/lib/voice/streams/VolumeTransformer.js +50 -0
- package/lib/voice/streams/WebmOpusTransformer.js +258 -0
- package/package.json +81 -4
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const util = require("util");
|
|
4
|
+
const Base = require("../structures/Base");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Handle ratelimiting something
|
|
8
|
+
* @prop {Number} interval How long (in ms) to wait between clearing used tokens
|
|
9
|
+
* @prop {Number} lastReset Timestamp of last token clearing
|
|
10
|
+
* @prop {Number} lastSend Timestamp of last token consumption
|
|
11
|
+
* @prop {Number} tokenLimit The max number tokens the bucket can consume per interval
|
|
12
|
+
* @prop {Number} tokens How many tokens the bucket has consumed in this interval
|
|
13
|
+
*/
|
|
14
|
+
class Bucket {
|
|
15
|
+
/**
|
|
16
|
+
* Construct a Bucket
|
|
17
|
+
* @arg {Number} tokenLimit The max number of tokens the bucket can consume per interval
|
|
18
|
+
* @arg {Number} interval How long (in ms) to wait between clearing used tokens
|
|
19
|
+
* @arg {Object} [options] Optional parameters
|
|
20
|
+
* @arg {Object} options.latencyRef A latency reference object
|
|
21
|
+
* @arg {Number} options.latencyRef.latency Interval between consuming tokens
|
|
22
|
+
* @arg {Number} options.reservedTokens How many tokens to reserve for priority operations
|
|
23
|
+
*/
|
|
24
|
+
constructor(tokenLimit, interval, options = {}) {
|
|
25
|
+
this.tokenLimit = tokenLimit;
|
|
26
|
+
this.interval = interval;
|
|
27
|
+
this.latencyRef = options.latencyRef || { latency: 0 };
|
|
28
|
+
this.lastReset = this.tokens = this.lastSend = 0;
|
|
29
|
+
this.reservedTokens = options.reservedTokens || 0;
|
|
30
|
+
this._queue = [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
check() {
|
|
34
|
+
if (this.timeout || this._queue.length === 0) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (this.lastReset + this.interval + this.tokenLimit * this.latencyRef.latency < Date.now()) {
|
|
38
|
+
this.lastReset = Date.now();
|
|
39
|
+
this.tokens = Math.max(0, this.tokens - this.tokenLimit);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let val;
|
|
43
|
+
let tokensAvailable = this.tokens < this.tokenLimit;
|
|
44
|
+
let unreservedTokensAvailable = this.tokens < (this.tokenLimit - this.reservedTokens);
|
|
45
|
+
while (this._queue.length > 0 && (unreservedTokensAvailable || (tokensAvailable && this._queue[0].priority))) {
|
|
46
|
+
this.tokens++;
|
|
47
|
+
tokensAvailable = this.tokens < this.tokenLimit;
|
|
48
|
+
unreservedTokensAvailable = this.tokens < (this.tokenLimit - this.reservedTokens);
|
|
49
|
+
const item = this._queue.shift();
|
|
50
|
+
val = this.latencyRef.latency - Date.now() + this.lastSend;
|
|
51
|
+
if (this.latencyRef.latency === 0 || val <= 0) {
|
|
52
|
+
item.func();
|
|
53
|
+
this.lastSend = Date.now();
|
|
54
|
+
} else {
|
|
55
|
+
setTimeout(() => {
|
|
56
|
+
item.func();
|
|
57
|
+
}, val);
|
|
58
|
+
this.lastSend = Date.now() + val;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (this._queue.length > 0 && !this.timeout) {
|
|
63
|
+
this.timeout = setTimeout(() => {
|
|
64
|
+
this.timeout = null;
|
|
65
|
+
this.check();
|
|
66
|
+
}, this.tokens < this.tokenLimit ? this.latencyRef.latency : Math.max(0, this.lastReset + this.interval + this.tokenLimit * this.latencyRef.latency - Date.now()));
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Queue something in the Bucket
|
|
72
|
+
* @arg {Function} func A callback to call when a token can be consumed
|
|
73
|
+
* @arg {Boolean} [priority=false] Whether or not the callback should use reserved tokens
|
|
74
|
+
*/
|
|
75
|
+
queue(func, priority = false) {
|
|
76
|
+
if (priority) {
|
|
77
|
+
this._queue.unshift({ func, priority });
|
|
78
|
+
} else {
|
|
79
|
+
this._queue.push({ func, priority });
|
|
80
|
+
}
|
|
81
|
+
this.check();
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
[util.inspect.custom]() {
|
|
85
|
+
return Base.prototype[util.inspect.custom].call(this);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
module.exports = Bucket;
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hold a bunch of something
|
|
5
|
+
* @extends Map
|
|
6
|
+
* @prop {Class} baseObject The base class for all items
|
|
7
|
+
* @prop {Number?} limit Max number of items to hold
|
|
8
|
+
*/
|
|
9
|
+
class Collection extends Map {
|
|
10
|
+
/**
|
|
11
|
+
* Construct a Collection
|
|
12
|
+
* @arg {Class} baseObject The base class for all items
|
|
13
|
+
* @arg {Number} [limit] Max number of items to hold
|
|
14
|
+
*/
|
|
15
|
+
constructor(baseObject, limit) {
|
|
16
|
+
super();
|
|
17
|
+
this.baseObject = baseObject;
|
|
18
|
+
this.limit = limit;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Update an object
|
|
23
|
+
* @arg {Object} obj The updated object data
|
|
24
|
+
* @arg {String} obj.id The ID of the object
|
|
25
|
+
* @arg {Class} [extra] An extra parameter the constructor may need
|
|
26
|
+
* @arg {Boolean} [replace] Whether to replace an existing object with the same ID
|
|
27
|
+
* @returns {Class} The updated object
|
|
28
|
+
*/
|
|
29
|
+
update(obj, extra, replace) {
|
|
30
|
+
if (!obj.id && obj.id !== 0) {
|
|
31
|
+
throw new Error("Missing object id");
|
|
32
|
+
}
|
|
33
|
+
const item = this.get(obj.id);
|
|
34
|
+
if (!item) {
|
|
35
|
+
return this.add(obj, extra, replace);
|
|
36
|
+
}
|
|
37
|
+
item.update(obj, extra);
|
|
38
|
+
return item;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Add an object
|
|
43
|
+
* @arg {Object} obj The object data
|
|
44
|
+
* @arg {String} obj.id The ID of the object
|
|
45
|
+
* @arg {Class} [extra] An extra parameter the constructor may need
|
|
46
|
+
* @arg {Boolean} [replace] Whether to replace an existing object with the same ID
|
|
47
|
+
* @returns {Class} The existing or newly created object
|
|
48
|
+
*/
|
|
49
|
+
add(obj, extra, replace) {
|
|
50
|
+
if (this.limit === 0) {
|
|
51
|
+
return (obj instanceof this.baseObject || obj.constructor.name === this.baseObject.name) ? obj : new this.baseObject(obj, extra);
|
|
52
|
+
}
|
|
53
|
+
if (obj.id == null) {
|
|
54
|
+
throw new Error("Missing object id");
|
|
55
|
+
}
|
|
56
|
+
const existing = this.get(obj.id);
|
|
57
|
+
if (existing && !replace) {
|
|
58
|
+
return existing;
|
|
59
|
+
}
|
|
60
|
+
if (!(obj instanceof this.baseObject || obj.constructor.name === this.baseObject.name)) {
|
|
61
|
+
obj = new this.baseObject(obj, extra);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
this.set(obj.id, obj);
|
|
65
|
+
|
|
66
|
+
if (this.limit && this.size > this.limit) {
|
|
67
|
+
const iter = this.keys();
|
|
68
|
+
while (this.size > this.limit) {
|
|
69
|
+
this.delete(iter.next().value);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
return obj;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns true if all elements satisfy the condition
|
|
77
|
+
* @arg {Function} func A function that takes an object and returns true or false
|
|
78
|
+
* @returns {Boolean} Whether or not all elements satisfied the condition
|
|
79
|
+
*/
|
|
80
|
+
every(func) {
|
|
81
|
+
for (const item of this.values()) {
|
|
82
|
+
if (!func(item)) {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
return true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Return all the objects that make the function evaluate true
|
|
91
|
+
* @arg {Function} func A function that takes an object and returns true if it matches
|
|
92
|
+
* @returns {Array<Class>} An array containing all the objects that matched
|
|
93
|
+
*/
|
|
94
|
+
filter(func) {
|
|
95
|
+
const arr = [];
|
|
96
|
+
for (const item of this.values()) {
|
|
97
|
+
if (func(item)) {
|
|
98
|
+
arr.push(item);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
return arr;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Return the first object to make the function evaluate true
|
|
106
|
+
* @arg {Function} func A function that takes an object and returns true if it matches
|
|
107
|
+
* @returns {Class?} The first matching object, or undefined if no match
|
|
108
|
+
*/
|
|
109
|
+
find(func) {
|
|
110
|
+
for (const item of this.values()) {
|
|
111
|
+
if (func(item)) {
|
|
112
|
+
return item;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return undefined;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Return an array with the results of applying the given function to each element
|
|
120
|
+
* @arg {Function} func A function that takes an object and returns something
|
|
121
|
+
* @returns {Array} An array containing the results
|
|
122
|
+
*/
|
|
123
|
+
map(func) {
|
|
124
|
+
const arr = [];
|
|
125
|
+
for (const item of this.values()) {
|
|
126
|
+
arr.push(func(item));
|
|
127
|
+
}
|
|
128
|
+
return arr;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Get a random object from the Collection
|
|
133
|
+
* @returns {Class?} The random object, or undefined if there is no match
|
|
134
|
+
*/
|
|
135
|
+
random() {
|
|
136
|
+
const index = Math.floor(Math.random() * this.size);
|
|
137
|
+
const iter = this.values();
|
|
138
|
+
for (let i = 0; i < index; ++i) {
|
|
139
|
+
iter.next();
|
|
140
|
+
}
|
|
141
|
+
return iter.next().value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Returns a value resulting from applying a function to every element of the collection
|
|
146
|
+
* @arg {Function} func A function that takes the previous value and the next item and returns a new value
|
|
147
|
+
* @arg {any} [initialValue] The initial value passed to the function
|
|
148
|
+
* @returns {any} The final result
|
|
149
|
+
*/
|
|
150
|
+
reduce(func, initialValue) {
|
|
151
|
+
const iter = this.values();
|
|
152
|
+
let val;
|
|
153
|
+
let result = initialValue === undefined ? iter.next().value : initialValue;
|
|
154
|
+
while ((val = iter.next().value) !== undefined) {
|
|
155
|
+
result = func(result, val);
|
|
156
|
+
}
|
|
157
|
+
return result;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Remove an object
|
|
162
|
+
* @arg {Object} obj The object
|
|
163
|
+
* @arg {String} obj.id The ID of the object
|
|
164
|
+
* @returns {Class?} The removed object, or null if nothing was removed
|
|
165
|
+
*/
|
|
166
|
+
remove(obj) {
|
|
167
|
+
const item = this.get(obj.id);
|
|
168
|
+
if (!item) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
this.delete(obj.id);
|
|
172
|
+
return item;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Returns true if at least one element satisfies the condition
|
|
177
|
+
* @arg {Function} func A function that takes an object and returns true or false
|
|
178
|
+
* @returns {Boolean} Whether or not at least one element satisfied the condition
|
|
179
|
+
*/
|
|
180
|
+
some(func) {
|
|
181
|
+
for (const item of this.values()) {
|
|
182
|
+
if (func(item)) {
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return false;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
toString() {
|
|
190
|
+
return `[Collection<${this.baseObject.name}>]`;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
toJSON() {
|
|
194
|
+
const json = {};
|
|
195
|
+
for (const item of this.values()) {
|
|
196
|
+
json[item.id] = item;
|
|
197
|
+
}
|
|
198
|
+
return json;
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
module.exports = Collection;
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
class MultipartData {
|
|
4
|
+
constructor() {
|
|
5
|
+
this.boundary = "----------------Eris";
|
|
6
|
+
this.bufs = [];
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
attach(fieldName, data, filename) {
|
|
10
|
+
if (data === undefined) {
|
|
11
|
+
return;
|
|
12
|
+
}
|
|
13
|
+
let str = "\r\n--" + this.boundary + "\r\nContent-Disposition: form-data; name=\"" + fieldName + "\"";
|
|
14
|
+
let contentType;
|
|
15
|
+
if (filename) {
|
|
16
|
+
str += "; filename=\"" + filename + "\"";
|
|
17
|
+
const extension = filename.match(/\.(png|apng|gif|jpg|jpeg|webp|svg|json|csv)$/i);
|
|
18
|
+
if (extension) {
|
|
19
|
+
let ext = extension[1].toLowerCase();
|
|
20
|
+
switch (ext) {
|
|
21
|
+
case "png":
|
|
22
|
+
case "apng":
|
|
23
|
+
case "gif":
|
|
24
|
+
case "jpg":
|
|
25
|
+
case "jpeg":
|
|
26
|
+
case "webp":
|
|
27
|
+
case "svg": {
|
|
28
|
+
if (ext === "svg") {
|
|
29
|
+
ext = "svg+xml";
|
|
30
|
+
}
|
|
31
|
+
contentType = "image/";
|
|
32
|
+
break;
|
|
33
|
+
}
|
|
34
|
+
case "json": {
|
|
35
|
+
contentType = "application/";
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
case "csv": {
|
|
39
|
+
contentType = "text/";
|
|
40
|
+
break;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
contentType += ext;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (contentType) {
|
|
48
|
+
str += `\r\nContent-Type: ${contentType}`;
|
|
49
|
+
} else if (ArrayBuffer.isView(data)) {
|
|
50
|
+
str += "\r\nContent-Type: application/octet-stream";
|
|
51
|
+
if (!(data instanceof Uint8Array)) {
|
|
52
|
+
data = new Uint8Array(data.buffer, data.byteOffset, data.byteLength);
|
|
53
|
+
}
|
|
54
|
+
} else if (typeof data === "object") {
|
|
55
|
+
str += "\r\nContent-Type: application/json";
|
|
56
|
+
data = Buffer.from(JSON.stringify(data));
|
|
57
|
+
} else {
|
|
58
|
+
data = Buffer.from("" + data);
|
|
59
|
+
}
|
|
60
|
+
this.bufs.push(Buffer.from(str + "\r\n\r\n"));
|
|
61
|
+
this.bufs.push(data);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
finish() {
|
|
65
|
+
this.bufs.push(Buffer.from("\r\n--" + this.boundary + "--"));
|
|
66
|
+
return this.bufs;
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
module.exports = MultipartData;
|
package/lib/util/Opus.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
let NativeOpus;
|
|
4
|
+
let OpusScript;
|
|
5
|
+
|
|
6
|
+
module.exports.createOpus = function createOpus(samplingRate, channels, bitrate) {
|
|
7
|
+
if (!NativeOpus && !OpusScript) {
|
|
8
|
+
try {
|
|
9
|
+
NativeOpus = require("@discordjs/opus");
|
|
10
|
+
} catch {
|
|
11
|
+
try {
|
|
12
|
+
OpusScript = require("opusscript");
|
|
13
|
+
} catch {} // eslint-disable-line no-empty
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
let opus;
|
|
18
|
+
if (NativeOpus) {
|
|
19
|
+
opus = new NativeOpus.OpusEncoder(samplingRate, channels);
|
|
20
|
+
} else if (OpusScript) {
|
|
21
|
+
opus = new OpusScript(samplingRate, channels, OpusScript.Application.AUDIO);
|
|
22
|
+
} else {
|
|
23
|
+
throw new Error("No opus encoder found, playing non-opus audio will not work.");
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (opus.setBitrate) {
|
|
27
|
+
opus.setBitrate(bitrate);
|
|
28
|
+
} else if (opus.encoderCTL) {
|
|
29
|
+
opus.encoderCTL(4002, bitrate);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return opus;
|
|
33
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const util = require("util");
|
|
4
|
+
const Base = require("../structures/Base");
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Ratelimit requests and release in sequence
|
|
8
|
+
* TODO: add latencyref
|
|
9
|
+
* @prop {Number} limit How many tokens the bucket can consume in the current interval
|
|
10
|
+
* @prop {Boolean} processing Whether the queue is being processed
|
|
11
|
+
* @prop {Number} remaining How many tokens the bucket has left in the current interval
|
|
12
|
+
* @prop {Number} reset Timestamp of next reset
|
|
13
|
+
*/
|
|
14
|
+
class SequentialBucket {
|
|
15
|
+
/**
|
|
16
|
+
* Construct a SequentialBucket
|
|
17
|
+
* @arg {Number} limit The max number of tokens the bucket can consume per interval
|
|
18
|
+
* @arg {Object} [latencyRef] An object
|
|
19
|
+
* @arg {Number} latencyRef.latency Interval between consuming tokens
|
|
20
|
+
*/
|
|
21
|
+
constructor(limit, latencyRef = { latency: 0 }) {
|
|
22
|
+
this.limit = this.remaining = limit;
|
|
23
|
+
this.reset = 0;
|
|
24
|
+
this.processing = false;
|
|
25
|
+
this.latencyRef = latencyRef;
|
|
26
|
+
this._queue = [];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
check(override) {
|
|
30
|
+
if (this._queue.length === 0) {
|
|
31
|
+
if (this.processing) {
|
|
32
|
+
clearTimeout(this.processing);
|
|
33
|
+
this.processing = false;
|
|
34
|
+
}
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
if (this.processing && !override) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
const now = Date.now();
|
|
41
|
+
const offset = this.latencyRef.latency;
|
|
42
|
+
if (!this.reset || this.reset < now - offset) {
|
|
43
|
+
this.reset = now - offset;
|
|
44
|
+
this.remaining = this.limit;
|
|
45
|
+
}
|
|
46
|
+
this.last = now;
|
|
47
|
+
if (this.remaining <= 0) {
|
|
48
|
+
this.processing = setTimeout(() => {
|
|
49
|
+
this.processing = false;
|
|
50
|
+
this.check(true);
|
|
51
|
+
}, Math.max(0, (this.reset || 0) - now + offset) + 1);
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
--this.remaining;
|
|
55
|
+
this.processing = true;
|
|
56
|
+
this._queue.shift()(() => {
|
|
57
|
+
if (this._queue.length > 0) {
|
|
58
|
+
this.check(true);
|
|
59
|
+
} else {
|
|
60
|
+
this.processing = false;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Queue something in the SequentialBucket
|
|
67
|
+
* @arg {Function} func A function to call when a token can be consumed. The function will be passed a callback argument, which must be called to allow the bucket to continue to work
|
|
68
|
+
*/
|
|
69
|
+
queue(func, short) {
|
|
70
|
+
if (short) {
|
|
71
|
+
this._queue.unshift(func);
|
|
72
|
+
} else {
|
|
73
|
+
this._queue.push(func);
|
|
74
|
+
}
|
|
75
|
+
this.check();
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
[util.inspect.custom]() {
|
|
79
|
+
return Base.prototype[util.inspect.custom].call(this);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
module.exports = SequentialBucket;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const warningMessages = {
|
|
2
|
+
CREATE_CHANNEL_OPTIONS: "Passing parentID or reason string arguments directly to createChannel() is deprecated. Use an options object instead.",
|
|
3
|
+
CREATE_THREAD_WITHOUT_MESSAGE: "createThreadWithoutMessage() is deprecated. Use createThread() instead.",
|
|
4
|
+
DEFAULT_PERMISSION: "Passing a defaultPermission for application commands is deprecated. Use defaultMemberPermissions instead.",
|
|
5
|
+
DELETE_MESSAGE_DAYS: "Passing the deleteMessageDays parameter to banGuildMember is deprecated. Use an options object with deleteMessageSeconds instead.",
|
|
6
|
+
DM_REACTION_REMOVE: "Passing a userID when using removeMessageReaction() in a DMChannel is deprecated. This behavior is no longer supported by Discord.",
|
|
7
|
+
GET_CHANNEL_PINS: "getPins() returning an array of messages is deprecated and will be removed in future versions. Use an options object instead.",
|
|
8
|
+
GET_INVITE_WITH_EXPIRATION: "Passing withExpiration to getInvite() is deprecated. This is because Discord now always sends an expiration date when fetching an invite.",
|
|
9
|
+
GET_REACTION_BEFORE: "Passing the before parameter to getMessageReaction() is deprecated. Discord no longer supports this parameter.",
|
|
10
|
+
GET_NITRO_STICKER_PACKS: "getNitroStickerPacks() is deprecated because standard sticker packs are no longer Nitro locked. Use getStickerPacks() instead.",
|
|
11
|
+
MEMBER_PERMISSION: "Member#permission is deprecated. Use Member#permissions instead.",
|
|
12
|
+
MESSAGE_REFERENCE: "Passing the content.messageReferenceID option to createMessage() is deprecated. Use the content.messageReference option instead.",
|
|
13
|
+
REACTION_USER: "Passing a userID other than \"@me\" to addMessageReaction() is deprecated. Discord no longer supports this parameter.",
|
|
14
|
+
PRIVATE_CHANNEL: "Using PrivateChannel is deprecated. Use DMChannel instead, and rename all relevant properties to dmChannel or otherwise where necessary.",
|
|
15
|
+
};
|
|
16
|
+
const unknownCodeMessage = "You have triggered a deprecated behavior whose warning was implemented improperly. Please report this issue.";
|
|
17
|
+
|
|
18
|
+
const emittedCodes = [];
|
|
19
|
+
|
|
20
|
+
module.exports = function emitDeprecation(code) {
|
|
21
|
+
if (emittedCodes.includes(code)) {
|
|
22
|
+
return;
|
|
23
|
+
}
|
|
24
|
+
emittedCodes.push(code);
|
|
25
|
+
process.emitWarning(warningMessages[code] || unknownCodeMessage, "DeprecationWarning", `eris:${code}`);
|
|
26
|
+
};
|