mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
Cleanup Part 2: Electric Boogaloo (Reloaded) (#594)
* Cleanup Part 2: Electric Boogaloo (Reloaded) * Moar cleanup * Tweak NOT_A_PERMISSION error
This commit is contained in:
committed by
Amish Shah
parent
5a9c42061f
commit
0b908f5bce
@@ -1,128 +1,13 @@
|
||||
const Collection = require('../../util/Collection');
|
||||
const Message = require('../Message');
|
||||
const path = require('path');
|
||||
const EventEmitter = require('events').EventEmitter;
|
||||
|
||||
/**
|
||||
* A function that takes a Message object and a MessageCollector and returns a boolean.
|
||||
* ```js
|
||||
* function(message, collector) {
|
||||
* if (message.content.includes('discord')) {
|
||||
* return true; // passed the filter test
|
||||
* }
|
||||
* return false; // failed the filter test
|
||||
* }
|
||||
* ```
|
||||
* @typedef {function} CollectorFilterFunction
|
||||
*/
|
||||
|
||||
/**
|
||||
* An object containing options used to configure a MessageCollector. All properties are optional.
|
||||
* ```js
|
||||
* {
|
||||
* time: null, // time in milliseconds. If specified, the collector ends after this amount of time.
|
||||
* max: null, // the maximum amount of messages to handle before ending.
|
||||
* }
|
||||
* ```
|
||||
* @typedef {Object} CollectorOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Collects messages based on a specified filter, then emits them.
|
||||
* @extends {EventEmitter}
|
||||
*/
|
||||
class MessageCollector extends EventEmitter {
|
||||
constructor(channel, filter, options = {}) {
|
||||
super();
|
||||
/**
|
||||
* The channel this collector is operating on
|
||||
* @type {Channel}
|
||||
*/
|
||||
this.channel = channel;
|
||||
/**
|
||||
* A function used to filter messages that the collector collects.
|
||||
* @type {CollectorFilterFunction}
|
||||
*/
|
||||
this.filter = filter;
|
||||
/**
|
||||
* Options for the collecor.
|
||||
* @type {CollectorOptions}
|
||||
*/
|
||||
this.options = options;
|
||||
/**
|
||||
* Whether this collector has stopped collecting Messages.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.ended = false;
|
||||
this.listener = message => this.verify(message);
|
||||
this.channel.client.on('message', this.listener);
|
||||
/**
|
||||
* A collection of collected messages, mapped by message ID.
|
||||
* @type {Collection<string, Message>}
|
||||
*/
|
||||
this.collected = new Collection();
|
||||
if (options.time) {
|
||||
this.channel.client.setTimeout(() => this.stop('time'), options.time);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies a message against the filter and options
|
||||
* @private
|
||||
* @param {Message} message the message
|
||||
* @returns {boolean}
|
||||
*/
|
||||
verify(message) {
|
||||
if (this.channel ? this.channel.id !== message.channel.id : false) {
|
||||
return false;
|
||||
}
|
||||
if (this.filter(message, this)) {
|
||||
this.collected.set(message.id, message);
|
||||
/**
|
||||
* Emitted whenever the Collector receives a Message that passes the filter test.
|
||||
* @param {Message} message the received message
|
||||
* @param {MessageCollector} collector the collector the message passed through.
|
||||
* @event MessageCollector#message
|
||||
*/
|
||||
this.emit('message', message, this);
|
||||
if (this.options.max && this.collected.size === this.options.max) {
|
||||
this.stop('limit');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the collector and emits `end`.
|
||||
* @param {string} [reason='user'] an optional reason for stopping the collector.
|
||||
*/
|
||||
stop(reason = 'user') {
|
||||
if (this.ended) {
|
||||
return;
|
||||
}
|
||||
this.ended = true;
|
||||
this.channel.client.removeListener('message', this.listener);
|
||||
/**
|
||||
* Emitted when the Collector stops collecting.
|
||||
* @param {Collection<string, Message>} collection A collection of messages collected
|
||||
* during the lifetime of the Collector.
|
||||
* Mapped by the ID of the Messages.
|
||||
* @param {string} reason The reason for the end of the collector. If it ended because it reached the specified time
|
||||
* limit, this would be `time`. If you invoke `.stop()` without specifying a reason, this would be `user`. If it
|
||||
* ended because it reached its message limit, it will be `limit`.
|
||||
* @event MessageCollector#end
|
||||
*/
|
||||
this.emit('end', this.collected, reason);
|
||||
}
|
||||
}
|
||||
const Message = require('../Message');
|
||||
const Collection = require('../../util/Collection');
|
||||
|
||||
/**
|
||||
* Interface for classes that have text-channel-like features
|
||||
* @interface
|
||||
*/
|
||||
class TextBasedChannel {
|
||||
|
||||
constructor() {
|
||||
/**
|
||||
* A Collection containing the messages sent to this channel.
|
||||
@@ -133,19 +18,16 @@ class TextBasedChannel {
|
||||
|
||||
/**
|
||||
* Bulk delete a given Collection or Array of messages in one go. Returns the deleted messages after.
|
||||
* @param {Map<string, Message>|Array<Message>} messages the messages to delete
|
||||
* @param {Collection<string, Message>|Message[]} messages The messages to delete
|
||||
* @returns {Collection<string, Message>}
|
||||
*/
|
||||
bulkDelete(messages) {
|
||||
if (messages instanceof Map) {
|
||||
messages = messages.array();
|
||||
}
|
||||
if (!(messages instanceof Array)) {
|
||||
return Promise.reject('pass an array or map');
|
||||
}
|
||||
if (messages instanceof Collection) messages = messages.array();
|
||||
if (!(messages instanceof Array)) return Promise.reject(new TypeError('messages must be an array or collection'));
|
||||
const messageIDs = messages.map(m => m.id);
|
||||
return this.client.rest.methods.bulkDeleteMessages(this, messageIDs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Options that can be passed into sendMessage or sendTTSMessage:
|
||||
* ```js
|
||||
@@ -156,10 +38,11 @@ class TextBasedChannel {
|
||||
* ```
|
||||
* @typedef {Object} MessageOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* Send a message to this channel
|
||||
* @param {string} content the content to send
|
||||
* @param {MessageOptions} [options={}] the options to provide
|
||||
* @param {string} content The content to send
|
||||
* @param {MessageOptions} [options={}] The options to provide
|
||||
* @returns {Promise<Message>}
|
||||
* @example
|
||||
* // send a message
|
||||
@@ -170,10 +53,11 @@ class TextBasedChannel {
|
||||
sendMessage(content, options = {}) {
|
||||
return this.client.rest.methods.sendMessage(this, content, options.tts, options.nonce);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a text-to-speech message to this channel
|
||||
* @param {string} content the content to send
|
||||
* @param {MessageOptions} [options={}] the options to provide
|
||||
* @param {string} content The content to send
|
||||
* @param {MessageOptions} [options={}] The options to provide
|
||||
* @returns {Promise<Message>}
|
||||
* @example
|
||||
* // send a TTS message
|
||||
@@ -184,6 +68,7 @@ class TextBasedChannel {
|
||||
sendTTSMessage(content, options = {}) {
|
||||
return this.client.rest.methods.sendMessage(this, content, true, options.nonce);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a file to this channel
|
||||
* @param {FileResolvable} attachment The file to send
|
||||
@@ -201,16 +86,15 @@ class TextBasedChannel {
|
||||
}
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
this.client.resolver.resolveFile(attachment)
|
||||
.then(file => {
|
||||
this.client.resolver.resolveFile(attachment).then(file => {
|
||||
this.client.rest.methods.sendMessage(this, undefined, false, undefined, {
|
||||
file,
|
||||
name: fileName,
|
||||
}).then(resolve).catch(reject);
|
||||
})
|
||||
.catch(reject);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
|
||||
* `after` are mutually exclusive. All the parameters are optional.
|
||||
@@ -227,8 +111,8 @@ class TextBasedChannel {
|
||||
|
||||
/**
|
||||
* Gets the past messages sent in this channel. Resolves with a Collection mapping message ID's to Message objects.
|
||||
* @param {ChannelLogsQueryOptions} [options={}] the query parameters to pass in
|
||||
* @returns {Promise<Collection<string, Message>, Error>}
|
||||
* @param {ChannelLogsQueryOptions} [options={}] The query parameters to pass in
|
||||
* @returns {Promise<Collection<string, Message>>}
|
||||
* @example
|
||||
* // get messages
|
||||
* channel.fetchMessages({limit: 10})
|
||||
@@ -237,17 +121,15 @@ class TextBasedChannel {
|
||||
*/
|
||||
fetchMessages(options = {}) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.client.rest.methods.getChannelMessages(this, options)
|
||||
.then(data => {
|
||||
const messages = new Collection();
|
||||
for (const message of data) {
|
||||
const msg = new Message(this, message, this.client);
|
||||
messages.set(message.id, msg);
|
||||
this._cacheMessage(msg);
|
||||
}
|
||||
resolve(messages);
|
||||
})
|
||||
.catch(reject);
|
||||
this.client.rest.methods.getChannelMessages(this, options).then(data => {
|
||||
const messages = new Collection();
|
||||
for (const message of data) {
|
||||
const msg = new Message(this, message, this.client);
|
||||
messages.set(message.id, msg);
|
||||
this._cacheMessage(msg);
|
||||
}
|
||||
resolve(messages);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -278,7 +160,7 @@ class TextBasedChannel {
|
||||
* Stops the typing indicator in the channel.
|
||||
* The indicator will only stop if this is called as many times as startTyping().
|
||||
* <info>It can take a few seconds for the Client User to stop typing.</info>
|
||||
* @param {boolean} [force=false] whether or not to force the indicator to stop regardless of call count
|
||||
* @param {boolean} [force=false] Whether or not to reset the call count and force the indicator to stop
|
||||
* @example
|
||||
* // stop typing in a channel
|
||||
* channel.stopTyping();
|
||||
@@ -316,8 +198,8 @@ class TextBasedChannel {
|
||||
|
||||
/**
|
||||
* Creates a Message Collector
|
||||
* @param {CollectorFilterFunction} filter the filter to create the collector with
|
||||
* @param {CollectorOptions} [options={}] the options to pass to the collector
|
||||
* @param {CollectorFilterFunction} filter The filter to create the collector with
|
||||
* @param {CollectorOptions} [options={}] The options to pass to the collector
|
||||
* @returns {MessageCollector}
|
||||
* @example
|
||||
* // create a message collector
|
||||
@@ -329,8 +211,7 @@ class TextBasedChannel {
|
||||
* collector.on('end', collected => console.log(`Collected ${collected.size} items`));
|
||||
*/
|
||||
createCollector(filter, options = {}) {
|
||||
const collector = new MessageCollector(this, filter, options);
|
||||
return collector;
|
||||
return new MessageCollector(this, filter, options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -346,8 +227,8 @@ class TextBasedChannel {
|
||||
/**
|
||||
* Similar to createCollector but in Promise form. Resolves with a Collection of messages that pass the specified
|
||||
* filter.
|
||||
* @param {CollectorFilterFunction} filter the filter function to use
|
||||
* @param {AwaitMessagesOptions} [options={}] optional options to pass to the internal collector
|
||||
* @param {CollectorFilterFunction} filter The filter function to use
|
||||
* @param {AwaitMessagesOptions} [options={}] Optional options to pass to the internal collector
|
||||
* @returns {Promise<Collection<string, Message>>}
|
||||
* @example
|
||||
* // await !vote messages
|
||||
@@ -372,43 +253,140 @@ class TextBasedChannel {
|
||||
|
||||
_cacheMessage(message) {
|
||||
const maxSize = this.client.options.max_message_cache;
|
||||
if (maxSize === 0) {
|
||||
// saves on performance
|
||||
return null;
|
||||
}
|
||||
|
||||
if (this.messages.size >= maxSize) {
|
||||
this.messages.delete(this.messages.keys().next().value);
|
||||
}
|
||||
if (maxSize === 0) return null;
|
||||
if (this.messages.size >= maxSize) this.messages.delete(this.messages.keys().next().value);
|
||||
|
||||
this.messages.set(message.id, message);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the pinned messages of this Channel and returns a Collection of them.
|
||||
* @returns {Promise<Collection<string, Message>, Error>}
|
||||
* @returns {Promise<Collection<string, Message>>}
|
||||
*/
|
||||
fetchPinnedMessages() {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.client.rest.methods.getChannelPinnedMessages(this)
|
||||
.then(data => {
|
||||
const messages = new Collection();
|
||||
for (const message of data) {
|
||||
const msg = new Message(this, message, this.client);
|
||||
messages.set(message.id, msg);
|
||||
this._cacheMessage(msg);
|
||||
}
|
||||
resolve(messages);
|
||||
})
|
||||
.catch(reject);
|
||||
this.client.rest.methods.getChannelPinnedMessages(this).then(data => {
|
||||
const messages = new Collection();
|
||||
for (const message of data) {
|
||||
const msg = new Message(this, message, this.client);
|
||||
messages.set(message.id, msg);
|
||||
this._cacheMessage(msg);
|
||||
}
|
||||
resolve(messages);
|
||||
}).catch(reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function applyProp(structure, prop) {
|
||||
Object.defineProperty(structure.prototype, prop, Object.getOwnPropertyDescriptor(TextBasedChannel.prototype, prop));
|
||||
/**
|
||||
* Collects messages based on a specified filter, then emits them.
|
||||
* @extends {EventEmitter}
|
||||
*/
|
||||
class MessageCollector extends EventEmitter {
|
||||
/**
|
||||
* A function that takes a Message object and a MessageCollector and returns a boolean.
|
||||
* ```js
|
||||
* function(message, collector) {
|
||||
* if (message.content.includes('discord')) {
|
||||
* return true; // passed the filter test
|
||||
* }
|
||||
* return false; // failed the filter test
|
||||
* }
|
||||
* ```
|
||||
* @typedef {function} CollectorFilterFunction
|
||||
*/
|
||||
|
||||
/**
|
||||
* An object containing options used to configure a MessageCollector. All properties are optional.
|
||||
* ```js
|
||||
* {
|
||||
* time: null, // time in milliseconds. If specified, the collector ends after this amount of time.
|
||||
* max: null, // the maximum amount of messages to handle before ending.
|
||||
* }
|
||||
* ```
|
||||
* @typedef {Object} CollectorOptions
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {Channel} channel The channel to collect messages in
|
||||
* @param {CollectorFilterFunction} filter The filter function
|
||||
* @param {CollectorOptions} [options] Options for the collector
|
||||
*/
|
||||
constructor(channel, filter, options = {}) {
|
||||
super();
|
||||
/**
|
||||
* The channel this collector is operating on
|
||||
* @type {Channel}
|
||||
*/
|
||||
this.channel = channel;
|
||||
/**
|
||||
* A function used to filter messages that the collector collects.
|
||||
* @type {CollectorFilterFunction}
|
||||
*/
|
||||
this.filter = filter;
|
||||
/**
|
||||
* Options for the collecor.
|
||||
* @type {CollectorOptions}
|
||||
*/
|
||||
this.options = options;
|
||||
/**
|
||||
* Whether this collector has stopped collecting Messages.
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.ended = false;
|
||||
this.listener = message => this.verify(message);
|
||||
this.channel.client.on('message', this.listener);
|
||||
/**
|
||||
* A collection of collected messages, mapped by message ID.
|
||||
* @type {Collection<string, Message>}
|
||||
*/
|
||||
this.collected = new Collection();
|
||||
if (options.time) this.channel.client.setTimeout(() => this.stop('time'), options.time);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies a message against the filter and options
|
||||
* @private
|
||||
* @param {Message} message The message
|
||||
* @returns {boolean}
|
||||
*/
|
||||
verify(message) {
|
||||
if (this.channel ? this.channel.id !== message.channel.id : false) return false;
|
||||
if (this.filter(message, this)) {
|
||||
this.collected.set(message.id, message);
|
||||
/**
|
||||
* Emitted whenever the Collector receives a Message that passes the filter test.
|
||||
* @param {Message} message The received message
|
||||
* @param {MessageCollector} collector The collector the message passed through
|
||||
* @event MessageCollector#message
|
||||
*/
|
||||
this.emit('message', message, this);
|
||||
if (this.options.max && this.collected.size === this.options.max) this.stop('limit');
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops the collector and emits `end`.
|
||||
* @param {string} [reason='user'] An optional reason for stopping the collector
|
||||
*/
|
||||
stop(reason = 'user') {
|
||||
if (this.ended) return;
|
||||
this.ended = true;
|
||||
this.channel.client.removeListener('message', this.listener);
|
||||
/**
|
||||
* Emitted when the Collector stops collecting.
|
||||
* @param {Collection<string, Message>} collection A collection of messages collected
|
||||
* during the lifetime of the Collector, mapped by the ID of the Messages.
|
||||
* @param {string} reason The reason for the end of the collector. If it ended because it reached the specified time
|
||||
* limit, this would be `time`. If you invoke `.stop()` without specifying a reason, this would be `user`. If it
|
||||
* ended because it reached its message limit, it will be `limit`.
|
||||
* @event MessageCollector#end
|
||||
*/
|
||||
this.emit('end', this.collected, reason);
|
||||
}
|
||||
}
|
||||
|
||||
exports.applyToClass = (structure, full = false) => {
|
||||
@@ -425,7 +403,9 @@ exports.applyToClass = (structure, full = false) => {
|
||||
props.push('createCollector');
|
||||
props.push('awaitMessages');
|
||||
}
|
||||
for (const prop of props) {
|
||||
applyProp(structure, prop);
|
||||
}
|
||||
for (const prop of props) applyProp(structure, prop);
|
||||
};
|
||||
|
||||
function applyProp(structure, prop) {
|
||||
Object.defineProperty(structure.prototype, prop, Object.getOwnPropertyDescriptor(TextBasedChannel.prototype, prop));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user