mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 16:43:31 +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
@@ -10,9 +10,7 @@ class Channel {
|
||||
this.client = client;
|
||||
this.typingMap = {};
|
||||
this.typingTimeouts = [];
|
||||
if (guild) {
|
||||
this.guild = guild;
|
||||
}
|
||||
if (guild) this.guild = guild;
|
||||
/**
|
||||
* The type of the channel, either:
|
||||
* * `dm` - a DM channel
|
||||
@@ -22,9 +20,7 @@ class Channel {
|
||||
* @type {string}
|
||||
*/
|
||||
this.type = null;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
if (data) this.setup(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
|
||||
@@ -25,7 +25,7 @@ class ClientUser extends User {
|
||||
* Set the username of the logged in Client.
|
||||
* <info>Changing usernames in Discord is heavily rate limited, with only 2 requests
|
||||
* every hour. Use this sparingly!</info>
|
||||
* @param {string} username the new username
|
||||
* @param {string} username The new username
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set username
|
||||
@@ -40,7 +40,7 @@ class ClientUser extends User {
|
||||
/**
|
||||
* If this user is a "self bot" or logged in using a normal user's details (which should be avoided), you can set the
|
||||
* email here.
|
||||
* @param {string} email the new email
|
||||
* @param {string} email The new email
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set email
|
||||
@@ -55,7 +55,7 @@ class ClientUser extends User {
|
||||
/**
|
||||
* If this user is a "self bot" or logged in using a normal user's details (which should be avoided), you can set the
|
||||
* password here.
|
||||
* @param {string} password the new password
|
||||
* @param {string} password The new password
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set password
|
||||
@@ -69,7 +69,7 @@ class ClientUser extends User {
|
||||
|
||||
/**
|
||||
* Set the avatar of the logged in Client.
|
||||
* @param {Base64Resolvable} avatar the new avatar
|
||||
* @param {Base64Resolvable} avatar The new avatar
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set avatar
|
||||
@@ -83,9 +83,9 @@ class ClientUser extends User {
|
||||
|
||||
/**
|
||||
* Set the status and playing game of the logged in client.
|
||||
* @param {string} [status] the status, can be `online` or `idle`.
|
||||
* @param {string|Object} [game] the game that is being played
|
||||
* @returns {Promise<ClientUser, Error>}
|
||||
* @param {string} [status] The status, can be `online` or `idle`
|
||||
* @param {string|Object} [game] The game that is being played
|
||||
* @returns {Promise<ClientUser>}
|
||||
* @example
|
||||
* // set status
|
||||
* client.user.setStatus('status', 'game')
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const User = require('./User');
|
||||
const Channel = require('./Channel');
|
||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||
const User = require('./User');
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
const Collection = require('../util/Collection');
|
||||
const Constants = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
/**
|
||||
* Represents a Custom Emoji
|
||||
@@ -44,16 +44,14 @@ class Emoji {
|
||||
}
|
||||
|
||||
/**
|
||||
* A collection of roles this emoji is active for (empty if all). Mapped by role ID.
|
||||
* A collection of roles this emoji is active for (empty if all), mapped by role ID.
|
||||
* @type {Collection<string, Role>}
|
||||
* @readonly
|
||||
*/
|
||||
get roles() {
|
||||
const roles = new Collection();
|
||||
for (const role of this.roleIDS) {
|
||||
if (this.guild.roles.get(role)) {
|
||||
roles.set(role, this.guild.roles.get(role));
|
||||
}
|
||||
if (this.guild.roles.get(role)) roles.set(role, this.guild.roles.get(role));
|
||||
}
|
||||
return roles;
|
||||
}
|
||||
|
||||
@@ -28,31 +28,18 @@ class EvaluatedPermissions {
|
||||
for (const permissionName in Constants.PermissionFlags) {
|
||||
serializedPermissions[permissionName] = this.hasPermission(permissionName);
|
||||
}
|
||||
|
||||
return serializedPermissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether a user has a certain permission, e.g. `READ_MESSAGES`.
|
||||
* @param {string} permission the permission to check for
|
||||
* @param {boolean} [explicit=false] whether the user should explicitly have the permission.
|
||||
* Checks whether the user has a certain permission, e.g. `READ_MESSAGES`.
|
||||
* @param {PermissionResolvable} permission The permission to check for
|
||||
* @param {boolean} [explicit=false] Whether to require the user to explicitly have the exact permission
|
||||
* @returns {boolean}
|
||||
*/
|
||||
hasPermission(permission, explicit = false) {
|
||||
if (typeof permission === 'string') {
|
||||
permission = Constants.PermissionFlags[permission];
|
||||
}
|
||||
|
||||
if (!permission) {
|
||||
throw Constants.Errors.NOT_A_PERMISSION;
|
||||
}
|
||||
|
||||
if (!explicit) {
|
||||
if ((this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
permission = this.member.client.resolver.resolvePermission(permission);
|
||||
if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
|
||||
return (this.permissions & permission) > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const Channel = require('./Channel');
|
||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||
const Collection = require('../util/Collection');
|
||||
const arraysEqual = require('../util/ArraysEqual');
|
||||
|
||||
/*
|
||||
{ type: 3,
|
||||
@@ -24,49 +25,31 @@ const Collection = require('../util/Collection');
|
||||
icon: null }
|
||||
*/
|
||||
|
||||
|
||||
function arraysEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
if (a.length !== b.length) return false;
|
||||
|
||||
for (const itemInd in a) {
|
||||
const item = a[itemInd];
|
||||
const ind = b.indexOf(item);
|
||||
if (ind) {
|
||||
b.splice(ind, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return b.length === 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a Group DM on Discord
|
||||
* @extends {Channel}
|
||||
* @implements {TextBasedChannel}
|
||||
*/
|
||||
class GroupDMChannel extends Channel {
|
||||
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
|
||||
this.messages = new Collection();
|
||||
}
|
||||
|
||||
equals(other) {
|
||||
const base = other &&
|
||||
const equal = other &&
|
||||
this.id === other.id &&
|
||||
this.name === other.name &&
|
||||
this.icon === other.icon &&
|
||||
this.owner.id === other.owner_id;
|
||||
|
||||
if (base) {
|
||||
if (equal) {
|
||||
const thisIDs = this.recipients.array().map(r => r.id);
|
||||
const otherIDs = other.recipients.map(r => r.id);
|
||||
return arraysEqual(thisIDs, otherIDs);
|
||||
}
|
||||
|
||||
return base;
|
||||
return equal;
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
@@ -86,6 +69,7 @@ class GroupDMChannel extends Channel {
|
||||
this.recipients.set(user.id, user);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The name of this Group DM, can be null if one isn't set.
|
||||
* @type {string}
|
||||
|
||||
@@ -1,25 +1,11 @@
|
||||
const User = require('./User');
|
||||
const Role = require('./Role');
|
||||
const Emoji = require('./Emoji');
|
||||
const GuildMember = require('./GuildMember');
|
||||
const Constants = require('../util/Constants');
|
||||
const cloneObject = require('../util/CloneObject');
|
||||
const Role = require('./Role');
|
||||
const Collection = require('../util/Collection');
|
||||
const Emoji = require('./Emoji');
|
||||
|
||||
function arraysEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
if (a.length !== b.length) return false;
|
||||
|
||||
for (const itemInd in a) {
|
||||
const item = a[itemInd];
|
||||
const ind = b.indexOf(item);
|
||||
if (ind) {
|
||||
b.splice(ind, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return b.length === 0;
|
||||
}
|
||||
const cloneObject = require('../util/CloneObject');
|
||||
const arraysEqual = require('../util/ArraysEqual');
|
||||
|
||||
/**
|
||||
* Represents a Guild (or a Server) on Discord.
|
||||
@@ -50,9 +36,7 @@ class Guild {
|
||||
*/
|
||||
this.roles = new Collection();
|
||||
|
||||
if (!data) {
|
||||
return;
|
||||
}
|
||||
if (!data) return;
|
||||
|
||||
if (data.unavailable) {
|
||||
/**
|
||||
@@ -100,12 +84,11 @@ class Guild {
|
||||
this.channels.get(voiceState.channel_id).members.set(member.user.id, member);
|
||||
}
|
||||
/**
|
||||
* Emitted whenever a user joins a guild.
|
||||
*
|
||||
* @event Client#guildMemberAdd
|
||||
* @param {Guild} guild the guild that the user has joined
|
||||
* @param {GuildMember} member the member that has joined
|
||||
*/
|
||||
* Emitted whenever a user joins a guild.
|
||||
* @event Client#guildMemberAdd
|
||||
* @param {Guild} guild The guild that the user has joined
|
||||
* @param {GuildMember} member The member that has joined
|
||||
*/
|
||||
if (this.client.ws.status === Constants.Status.READY && !noEvent) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member);
|
||||
}
|
||||
@@ -117,25 +100,22 @@ class Guild {
|
||||
_updateMember(member, data) {
|
||||
const oldMember = cloneObject(member);
|
||||
|
||||
if (data.roles) {
|
||||
member._roles = data.roles;
|
||||
} else {
|
||||
member.nickname = data.nick;
|
||||
}
|
||||
if (data.roles) member._roles = data.roles;
|
||||
else member.nickname = data.nick;
|
||||
|
||||
const notSame = member.nickname !== oldMember.nickname && !arraysEqual(member._roles, oldMember._roles);
|
||||
|
||||
if (this.client.ws.status === Constants.Status.READY && notSame) {
|
||||
/**
|
||||
* Emitted whenever a Guild Member changes - i.e. new role, removed role, nickname
|
||||
*
|
||||
* @event Client#guildMemberUpdate
|
||||
* @param {Guild} guild the guild that the update affects
|
||||
* @param {GuildMember} oldMember the member before the update
|
||||
* @param {GuildMember} newMember the member after the update
|
||||
*/
|
||||
* Emitted whenever a Guild Member changes - i.e. new role, removed role, nickname
|
||||
* @event Client#guildMemberUpdate
|
||||
* @param {Guild} guild The guild that the update affects
|
||||
* @param {GuildMember} oldMember The member before the update
|
||||
* @param {GuildMember} newMember The member after the update
|
||||
*/
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_UPDATE, this, oldMember, member);
|
||||
}
|
||||
|
||||
return {
|
||||
old: oldMember,
|
||||
mem: member,
|
||||
@@ -163,7 +143,7 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Returns the GuildMember form of a User object, if the User is present in the guild.
|
||||
* @param {UserResolvable} user the user that you want to obtain the GuildMember of.
|
||||
* @param {UserResolvable} user The user that you want to obtain the GuildMember of
|
||||
* @returns {GuildMember|null}
|
||||
* @example
|
||||
* // get the guild member of a user
|
||||
@@ -177,11 +157,11 @@ class Guild {
|
||||
* Whether this Guild equals another Guild. It compares all properties, so for most operations
|
||||
* it is advisable to just compare `guild.id === guild2.id` as it is much faster and is often
|
||||
* what most users need.
|
||||
* @param {Guild} guild the guild to compare
|
||||
* @param {Guild} guild The guild to compare
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(guild) {
|
||||
let base =
|
||||
let equal =
|
||||
guild &&
|
||||
this.id === guild.id &&
|
||||
this.available === !guild.unavailable &&
|
||||
@@ -196,17 +176,15 @@ class Guild {
|
||||
this.verificationLevel === guild.verification_level &&
|
||||
this.embedEnabled === guild.embed_enabled;
|
||||
|
||||
if (base) {
|
||||
if (equal) {
|
||||
if (this.embedChannel) {
|
||||
if (this.embedChannel.id !== guild.embed_channel_id) {
|
||||
base = false;
|
||||
}
|
||||
if (this.embedChannel.id !== guild.embed_channel_id) equal = false;
|
||||
} else if (guild.embed_channel_id) {
|
||||
base = false;
|
||||
equal = false;
|
||||
}
|
||||
}
|
||||
|
||||
return base;
|
||||
return equal;
|
||||
}
|
||||
|
||||
_memberSpeakUpdate(user, speaking) {
|
||||
@@ -216,8 +194,8 @@ class Guild {
|
||||
/**
|
||||
* Emitted once a Guild Member starts/stops speaking
|
||||
* @event Client#guildMemberSpeaking
|
||||
* @param {GuildMember} member the member that started/stopped speaking
|
||||
* @param {boolean} speaking whether or not the member is speaking
|
||||
* @param {GuildMember} member The member that started/stopped speaking
|
||||
* @param {boolean} speaking Whether or not the member is speaking
|
||||
*/
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_SPEAKING, member, speaking);
|
||||
}
|
||||
@@ -225,7 +203,7 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Sets up the Guild
|
||||
* @param {*} data the raw data of the guild
|
||||
* @param {*} data The raw data of the guild
|
||||
* @private
|
||||
*/
|
||||
setup(data) {
|
||||
@@ -264,12 +242,12 @@ class Guild {
|
||||
this.icon = data.icon;
|
||||
/**
|
||||
* An array of guild features.
|
||||
* @type {Array<Object>}
|
||||
* @type {Object[]}
|
||||
*/
|
||||
this.features = data.features;
|
||||
/**
|
||||
* An array of guild emojis.
|
||||
* @type {Array<Object>}
|
||||
* @type {Object[]}
|
||||
*/
|
||||
this.emojis = new Collection();
|
||||
for (const emoji of data.emojis) {
|
||||
@@ -351,6 +329,7 @@ class Guild {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The date at which the logged-in client joined the guild.
|
||||
* @type {Date}
|
||||
@@ -361,9 +340,9 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Creates a new Channel in the Guild.
|
||||
* @param {string} name the name of the new channel.
|
||||
* @param {string} type the type of the new channel, either `text` or `voice`.
|
||||
* @returns {Promise<TextChannel|VoiceChannel, Error>}
|
||||
* @param {string} name The name of the new channel
|
||||
* @param {string} type The type of the new channel, either `text` or `voice`
|
||||
* @returns {Promise<TextChannel|VoiceChannel>}
|
||||
* @example
|
||||
* // create a new text channel
|
||||
* guild.createChannel('new general', 'text')
|
||||
@@ -376,7 +355,7 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Creates a new role in the guild, as of now this is just a blank role.
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // create a new role
|
||||
* guild.createRole()
|
||||
@@ -389,7 +368,7 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Causes the Client to leave the guild.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // leave a guild
|
||||
* guild.leave()
|
||||
@@ -402,7 +381,7 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Causes the Client to delete the guild.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // delete a guild
|
||||
* guild.delete()
|
||||
@@ -415,8 +394,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Updates the Guild with new information - e.g. a new name.
|
||||
* @param {GuildEditData} data the data to update the guild with.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {GuildEditData} data The data to update the guild with
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // set the guild name and region
|
||||
* guild.edit({
|
||||
@@ -432,8 +411,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Edit the name of the Guild.
|
||||
* @param {string} name the new name of the Guild.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {string} name The new name of the Guild
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // edit the guild name
|
||||
* guild.setName('Discord Guild')
|
||||
@@ -446,8 +425,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Edit the region of the Guild.
|
||||
* @param {Region} region the new region of the guild.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {Region} region The new region of the guild.
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // edit the guild region
|
||||
* guild.setRegion('london')
|
||||
@@ -460,8 +439,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Edit the verification level of the Guild.
|
||||
* @param {VerificationLevel} verificationLevel the new verification level of the guild.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {VerificationLevel} verificationLevel The new verification level of the guild
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // edit the guild verification level
|
||||
* guild.setVerificationLevel(1)
|
||||
@@ -474,8 +453,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Edit the AFK channel of the Guild.
|
||||
* @param {GuildChannelResolvable} afkChannel the new AFK channel.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {GuildChannelResolvable} afkChannel The new AFK channel
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // edit the guild AFK channel
|
||||
* guild.setAFKChannel(channel)
|
||||
@@ -488,8 +467,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Edit the AFK timeout of the Guild.
|
||||
* @param {number} afkTimeout the time in seconds that a user must be idle to be considered AFK.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {number} afkTimeout The time in seconds that a user must be idle to be considered AFK
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // edit the guild AFK channel
|
||||
* guild.setAFKTimeout(60)
|
||||
@@ -502,8 +481,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Set a new Guild Icon.
|
||||
* @param {Base64Resolvable} icon the new icon of the guild.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {Base64Resolvable} icon The new icon of the guild
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // edit the guild icon
|
||||
* guild.setIcon(fs.readFileSync('./icon.png'))
|
||||
@@ -516,8 +495,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Sets a new owner of the Guild.
|
||||
* @param {GuildMemberResolvable} owner the new owner of the Guild.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {GuildMemberResolvable} owner The new owner of the Guild
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // edit the guild owner
|
||||
* guild.setOwner(guilds.members[0])
|
||||
@@ -530,8 +509,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Set a new Guild Splash Logo.
|
||||
* @param {Base64Resolvable} splash the new splash screen of the guild.
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @param {Base64Resolvable} splash The new splash screen of the guild
|
||||
* @returns {Promise<Guild>}
|
||||
* @example
|
||||
* // edit the guild splash
|
||||
* guild.setIcon(fs.readFileSync('./splash.png'))
|
||||
@@ -544,8 +523,8 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Unbans a member from the Guild
|
||||
* @param {UserResolvable} member the member to unban
|
||||
* @returns {Promise<User, Error>}
|
||||
* @param {UserResolvable} member The member to unban
|
||||
* @returns {Promise<User>}
|
||||
* @example
|
||||
* // unban a member
|
||||
* guild.unban('123123123123')
|
||||
@@ -558,7 +537,7 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Fetch a Collection of banned users in this Guild.
|
||||
* @returns {Promise<Collection<string, User>, Error>}
|
||||
* @returns {Promise<Collection<string, User>>}
|
||||
*/
|
||||
fetchBans() {
|
||||
return this.client.rest.methods.getGuildBans(this);
|
||||
@@ -566,7 +545,7 @@ class Guild {
|
||||
|
||||
/**
|
||||
* Fetch a Collection of invites to this Guild. Resolves with a Collection mapping invites by their codes.
|
||||
* @returns {Promise<Collection<string, Invite>, Error>}
|
||||
* @returns {Promise<Collection<string, Invite>>}
|
||||
*/
|
||||
fetchInvites() {
|
||||
return this.client.rest.methods.getGuildInvites(this);
|
||||
@@ -576,7 +555,7 @@ class Guild {
|
||||
* Fetches all the members in the Guild, even if they are offline. If the Guild has less than 250 members,
|
||||
* this should not be necessary.
|
||||
* @param {string} [query=''] An optional query to provide when fetching members
|
||||
* @returns {Promise<Guild, Error>}
|
||||
* @returns {Promise<Guild>}
|
||||
*/
|
||||
fetchMembers(query = '') {
|
||||
return new Promise((resolve, reject) => {
|
||||
@@ -607,9 +586,7 @@ class Guild {
|
||||
* @readonly
|
||||
*/
|
||||
get iconURL() {
|
||||
if (!this.icon) {
|
||||
return null;
|
||||
}
|
||||
if (!this.icon) return null;
|
||||
return Constants.Endpoints.guildIcon(this.id, this.icon);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,24 +1,10 @@
|
||||
const Channel = require('./Channel');
|
||||
const PermissionOverwrites = require('./PermissionOverwrites');
|
||||
const Role = require('./Role');
|
||||
const PermissionOverwrites = require('./PermissionOverwrites');
|
||||
const EvaluatedPermissions = require('./EvaluatedPermissions');
|
||||
const Constants = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
function arraysEqual(a, b) {
|
||||
if (a === b) return true;
|
||||
if (a.length !== b.length) return false;
|
||||
|
||||
for (const itemInd in a) {
|
||||
const item = a[itemInd];
|
||||
const ind = b.indexOf(item);
|
||||
if (ind) {
|
||||
b.splice(ind, 1);
|
||||
}
|
||||
}
|
||||
|
||||
return b.length === 0;
|
||||
}
|
||||
const arraysEqual = require('../util/ArraysEqual');
|
||||
|
||||
/**
|
||||
* Represents a Guild Channel (i.e. Text Channels and Voice Channels)
|
||||
@@ -62,67 +48,57 @@ class GuildChannel extends Channel {
|
||||
/**
|
||||
* Checks if this channel has the same type, topic, position, name, overwrites and ID as another channel.
|
||||
* In most cases, a simple `channel.id === channel2.id` will do, and is much faster too.
|
||||
* @param {GuildChannel} channel the channel to compare this channel to
|
||||
* @param {GuildChannel} channel The channel to compare this channel to
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(channel) {
|
||||
let base = channel &&
|
||||
let equal = channel &&
|
||||
this.type === channel.type &&
|
||||
this.topic === channel.topic &&
|
||||
this.position === channel.position &&
|
||||
this.name === channel.name &&
|
||||
this.id === channel.id;
|
||||
|
||||
if (base) {
|
||||
if (equal) {
|
||||
if (channel.permission_overwrites) {
|
||||
const thisIDSet = Array.from(this.permissionOverwrites.keys());
|
||||
const otherIDSet = channel.permission_overwrites.map(overwrite => overwrite.id);
|
||||
if (arraysEqual(thisIDSet, otherIDSet)) {
|
||||
base = true;
|
||||
} else {
|
||||
base = false;
|
||||
}
|
||||
equal = arraysEqual(thisIDSet, otherIDSet);
|
||||
} else {
|
||||
base = false;
|
||||
equal = false;
|
||||
}
|
||||
}
|
||||
|
||||
return base;
|
||||
return equal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the overall set of permissions for a user in this channel, taking into account roles and permission
|
||||
* overwrites.
|
||||
* @param {GuildMemberResolvable} member the user that you want to obtain the overall permissions for
|
||||
* @param {GuildMemberResolvable} member The user that you want to obtain the overall permissions for
|
||||
* @returns {?EvaluatedPermissions}
|
||||
*/
|
||||
permissionsFor(member) {
|
||||
member = this.client.resolver.resolveGuildMember(this.guild, member);
|
||||
if (member) {
|
||||
if (this.guild.owner.id === member.id) {
|
||||
return new EvaluatedPermissions(member, Constants.ALL_PERMISSIONS);
|
||||
}
|
||||
if (this.guild.owner.id === member.id) return new EvaluatedPermissions(member, Constants.ALL_PERMISSIONS);
|
||||
|
||||
const roles = member.roles;
|
||||
let permissions = 0;
|
||||
const overwrites = this.overwritesFor(member, true);
|
||||
|
||||
for (const role of roles.values()) {
|
||||
permissions |= role.permissions;
|
||||
}
|
||||
|
||||
for (const role of roles.values()) permissions |= role.permissions;
|
||||
for (const overwrite of overwrites.role.concat(overwrites.member)) {
|
||||
permissions &= ~overwrite.denyData;
|
||||
permissions |= overwrite.allowData;
|
||||
}
|
||||
|
||||
const admin = Boolean(permissions & (Constants.PermissionFlags.ADMINISTRATOR));
|
||||
if (admin) {
|
||||
permissions = Constants.ALL_PERMISSIONS;
|
||||
}
|
||||
if (admin) permissions = Constants.ALL_PERMISSIONS;
|
||||
|
||||
return new EvaluatedPermissions(member, permissions);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -165,9 +141,9 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Overwrites the permissions for a user or role in this channel.
|
||||
* @param {Role|UserResolvable} userOrRole the user or role to update
|
||||
* @param {PermissionOverwriteOptions} options the configuration for the update
|
||||
* @returns {Promise<null, Error>}
|
||||
* @param {Role|UserResolvable} userOrRole The user or role to update
|
||||
* @param {PermissionOverwriteOptions} options The configuration for the update
|
||||
* @returns {Promise}
|
||||
* @example
|
||||
* // overwrite permissions for a message author
|
||||
* message.channel.overwritePermissions(message.author, {
|
||||
@@ -187,9 +163,7 @@ class GuildChannel extends Channel {
|
||||
} else {
|
||||
userOrRole = this.client.resolver.resolveUser(userOrRole);
|
||||
payload.type = 'member';
|
||||
if (!userOrRole) {
|
||||
return Promise.reject('supplied parameter was neither a user or a role');
|
||||
}
|
||||
if (!userOrRole) return Promise.reject(new TypeError('supplied parameter was neither a user or a role'));
|
||||
}
|
||||
|
||||
payload.id = userOrRole.id;
|
||||
@@ -220,7 +194,7 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Set a new name for the Guild Channel
|
||||
* @param {string} name the new name for the guild channel
|
||||
* @param {string} name The new name for the guild channel
|
||||
* @returns {Promise<GuildChannel>}
|
||||
* @example
|
||||
* // set a new channel name
|
||||
@@ -234,7 +208,7 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Set a new position for the Guild Channel
|
||||
* @param {number} position the new position for the guild channel
|
||||
* @param {number} position The new position for the guild channel
|
||||
* @returns {Promise<GuildChannel>}
|
||||
* @example
|
||||
* // set a new channel position
|
||||
@@ -248,7 +222,7 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Set a new topic for the Guild Channel
|
||||
* @param {string} topic the new topic for the guild channel
|
||||
* @param {string} topic The new topic for the guild channel
|
||||
* @returns {Promise<GuildChannel>}
|
||||
* @example
|
||||
* // set a new channel topic
|
||||
@@ -288,8 +262,8 @@ class GuildChannel extends Channel {
|
||||
|
||||
/**
|
||||
* Create an invite to this Guild Channel
|
||||
* @param {InviteOptions} [options={}] the options to provide when creating the invite
|
||||
* @returns {Promise<Invite, Error>}
|
||||
* @param {InviteOptions} [options={}] The options for the invite
|
||||
* @returns {Promise<Invite>}
|
||||
*/
|
||||
createInvite(options = {}) {
|
||||
return this.client.rest.methods.createChannelInvite(this, options);
|
||||
|
||||
@@ -23,9 +23,7 @@ class GuildMember {
|
||||
*/
|
||||
this.user = {};
|
||||
this._roles = [];
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
if (data) this.setup(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
@@ -91,15 +89,11 @@ class GuildMember {
|
||||
const list = new Collection();
|
||||
const everyoneRole = this.guild.roles.get(this.guild.id);
|
||||
|
||||
if (everyoneRole) {
|
||||
list.set(everyoneRole.id, everyoneRole);
|
||||
}
|
||||
if (everyoneRole) list.set(everyoneRole.id, everyoneRole);
|
||||
|
||||
for (const roleID of this._roles) {
|
||||
const role = this.guild.roles.get(roleID);
|
||||
if (role) {
|
||||
list.set(role.id, role);
|
||||
}
|
||||
if (role) list.set(role.id, role);
|
||||
}
|
||||
|
||||
return list;
|
||||
@@ -143,8 +137,8 @@ class GuildMember {
|
||||
|
||||
/**
|
||||
* Mute/unmute a user
|
||||
* @param {boolean} mute whether or not the member should be muted
|
||||
* @returns {Promise<GuildMember, Error>}
|
||||
* @param {boolean} mute Whether or not the member should be muted
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
setMute(mute) {
|
||||
return this.edit({ mute });
|
||||
@@ -152,8 +146,8 @@ class GuildMember {
|
||||
|
||||
/**
|
||||
* Deafen/undeafen a user
|
||||
* @param {boolean} deaf whether or not the member should be deafened
|
||||
* @returns {Promise<GuildMember, Error>}
|
||||
* @param {boolean} deaf Whether or not the member should be deafened
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
setDeaf(deaf) {
|
||||
return this.edit({ deaf });
|
||||
@@ -161,8 +155,8 @@ class GuildMember {
|
||||
|
||||
/**
|
||||
* Moves the Guild Member to the given channel.
|
||||
* @param {ChannelResolvable} channel the channel to move the member to
|
||||
* @returns {Promise<GuildMember, Error>}
|
||||
* @param {ChannelResolvable} channel The channel to move the member to
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
setVoiceChannel(channel) {
|
||||
return this.edit({ channel });
|
||||
@@ -170,8 +164,8 @@ class GuildMember {
|
||||
|
||||
/**
|
||||
* Sets the Roles applied to the member.
|
||||
* @param {Collection<string, Role>|Array<Role>} roles the roles to apply
|
||||
* @returns {Promise<GuildMember, Error>}
|
||||
* @param {Collection<string, Role>|Role[]} roles The roles to apply
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
setRoles(roles) {
|
||||
return this.edit({ roles });
|
||||
@@ -179,8 +173,8 @@ class GuildMember {
|
||||
|
||||
/**
|
||||
* Set the nickname for the Guild Member
|
||||
* @param {string} nick the nickname for the Guild Member
|
||||
* @returns {Promise<GuildMember, Error>}
|
||||
* @param {string} nick The nickname for the Guild Member
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
setNickname(nick) {
|
||||
return this.edit({ nick });
|
||||
@@ -188,15 +182,15 @@ class GuildMember {
|
||||
|
||||
/**
|
||||
* Edit a Guild Member
|
||||
* @param {GuildmemberEditData} data the data to edit the member with
|
||||
* @returns {Promise<GuildMember, Error>}
|
||||
* @param {GuildmemberEditData} data The data to edit the member with
|
||||
* @returns {Promise<GuildMember>}
|
||||
*/
|
||||
edit(data) {
|
||||
return this.client.rest.methods.updateGuildMember(this, data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes any DM's with this Guild Member
|
||||
* Deletes any DMs with this Guild Member
|
||||
* @returns {Promise<DMChannel>}
|
||||
*/
|
||||
deleteDM() {
|
||||
@@ -215,7 +209,7 @@ class GuildMember {
|
||||
* Ban this Guild Member
|
||||
* @param {number} [deleteDays=0] The amount of days worth of messages from this member that should
|
||||
* also be deleted. Between `0` and `7`.
|
||||
* @returns {Promise<GuildMember, Error>}
|
||||
* @returns {Promise<GuildMember>}
|
||||
* @example
|
||||
* // ban a guild member
|
||||
* guildMember.ban(7);
|
||||
|
||||
@@ -98,7 +98,7 @@ class Invite {
|
||||
|
||||
/**
|
||||
* Deletes this invite
|
||||
* @returns {Promise<Invite, Error>}
|
||||
* @returns {Promise<Invite>}
|
||||
*/
|
||||
delete() {
|
||||
return this.client.rest.methods.deleteInvite(this);
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const Collection = require('../util/Collection');
|
||||
const Attachment = require('./MessageAttachment');
|
||||
const Embed = require('./MessageEmbed');
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
/**
|
||||
* Represents a Message on Discord
|
||||
*/
|
||||
@@ -26,9 +27,7 @@ class Message {
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = client;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
if (data) this.setup(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
@@ -69,7 +68,7 @@ class Message {
|
||||
this.nonce = data.nonce;
|
||||
/**
|
||||
* A list of embeds in the message - e.g. YouTube Player
|
||||
* @type {Array<Embed>}
|
||||
* @type {Embed[]}
|
||||
*/
|
||||
this.embeds = data.embeds.map(e => new Embed(this, e));
|
||||
/**
|
||||
@@ -77,17 +76,15 @@ class Message {
|
||||
* @type {Collection<string, MessageAttachment>}
|
||||
*/
|
||||
this.attachments = new Collection();
|
||||
for (const attachment of data.attachments) {
|
||||
this.attachments.set(attachment.id, new Attachment(this, attachment));
|
||||
}
|
||||
for (const attachment of data.attachments) this.attachments.set(attachment.id, new Attachment(this, attachment));
|
||||
/**
|
||||
* An object containing a further users, roles or channels collections
|
||||
* @type {Object}
|
||||
* @property {Collection<string, User>} mentions.users Mentioned users, maps their ID to the user object.
|
||||
* @property {Collection<string, Role>} mentions.roles Mentioned roles, maps their ID to the role object.
|
||||
* @property {Collection<string, GuildChannel>}
|
||||
* mentions.channels Mentioned channels, maps their ID to the channel object.
|
||||
* @property {boolean} mentions.everyone whether or not @everyone was mentioned.
|
||||
* @property {Collection<string, GuildChannel>} mentions.channels Mentioned channels,
|
||||
* maps their ID to the channel object.
|
||||
* @property {boolean} mentions.everyone Whether or not @everyone was mentioned.
|
||||
*/
|
||||
this.mentions = {
|
||||
users: new Collection(),
|
||||
@@ -114,9 +111,7 @@ class Message {
|
||||
if (data.mention_roles) {
|
||||
for (const mention of data.mention_roles) {
|
||||
const role = this.channel.guild.roles.get(mention);
|
||||
if (role) {
|
||||
this.mentions.roles.set(role.id, role);
|
||||
}
|
||||
if (role) this.mentions.roles.set(role.id, role);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -124,9 +119,7 @@ class Message {
|
||||
const channMentionsRaw = data.content.match(/<#([0-9]{14,20})>/g) || [];
|
||||
for (const raw of channMentionsRaw) {
|
||||
const chan = this.channel.guild.channels.get(raw.match(/([0-9]{14,20})/g)[0]);
|
||||
if (chan) {
|
||||
this.mentions.channels.set(chan.id, chan);
|
||||
}
|
||||
if (chan) this.mentions.channels.set(chan.id, chan);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,9 +128,7 @@ class Message {
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.system = false;
|
||||
if (data.type === 6) {
|
||||
this.system = true;
|
||||
}
|
||||
if (data.type === 6) this.system = true;
|
||||
}
|
||||
/**
|
||||
* When the message was sent
|
||||
@@ -158,31 +149,17 @@ class Message {
|
||||
patch(data) { // eslint-disable-line complexity
|
||||
if (data.author) {
|
||||
this.author = this.client.users.get(data.author.id);
|
||||
if (this.guild) {
|
||||
this.member = this.guild.member(this.author);
|
||||
}
|
||||
}
|
||||
if (data.content) {
|
||||
this.content = data.content;
|
||||
}
|
||||
if (data.timestamp) {
|
||||
this._timestamp = new Date(data.timestamp).getTime();
|
||||
if (this.guild) this.member = this.guild.member(this.author);
|
||||
}
|
||||
if (data.content) this.content = data.content;
|
||||
if (data.timestamp) this._timestamp = new Date(data.timestamp).getTime();
|
||||
if (data.edited_timestamp) {
|
||||
this._editedTimestamp = data.edited_timestamp ? new Date(data.edited_timestamp).getTime() : null;
|
||||
}
|
||||
if ('tts' in data) {
|
||||
this.tts = data.tts;
|
||||
}
|
||||
if ('mention_everyone' in data) {
|
||||
this.mentions.everyone = data.mention_everyone;
|
||||
}
|
||||
if (data.nonce) {
|
||||
this.nonce = data.nonce;
|
||||
}
|
||||
if (data.embeds) {
|
||||
this.embeds = data.embeds.map(e => new Embed(this, e));
|
||||
}
|
||||
if ('tts' in data) this.tts = data.tts;
|
||||
if ('mention_everyone' in data) this.mentions.everyone = data.mention_everyone;
|
||||
if (data.nonce) this.nonce = data.nonce;
|
||||
if (data.embeds) this.embeds = data.embeds.map(e => new Embed(this, e));
|
||||
if (data.type > -1) {
|
||||
this.system = false;
|
||||
if (data.type === 6) {
|
||||
@@ -214,9 +191,7 @@ class Message {
|
||||
}
|
||||
}
|
||||
}
|
||||
if (data.id) {
|
||||
this.id = data.id;
|
||||
}
|
||||
if (data.id) this.id = data.id;
|
||||
if (this.channel.guild && data.content) {
|
||||
const channMentionsRaw = data.content.match(/<#([0-9]{14,20})>/g) || [];
|
||||
for (const raw of channMentionsRaw) {
|
||||
@@ -237,14 +212,11 @@ class Message {
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(message, rawData) {
|
||||
if (!message) return false;
|
||||
const embedUpdate = !message.author && !message.attachments;
|
||||
if (embedUpdate) return this.id === message.id && this.embeds.length === message.embeds.length;
|
||||
|
||||
if (embedUpdate) {
|
||||
const base = this.id === message.id &&
|
||||
this.embeds.length === message.embeds.length;
|
||||
return base;
|
||||
}
|
||||
let base = this.id === message.id &&
|
||||
let equal = this.id === message.id &&
|
||||
this.author.id === message.author.id &&
|
||||
this.content === message.content &&
|
||||
this.tts === message.tts &&
|
||||
@@ -252,19 +224,19 @@ class Message {
|
||||
this.embeds.length === message.embeds.length &&
|
||||
this.attachments.length === message.attachments.length;
|
||||
|
||||
if (base && rawData) {
|
||||
base = this.mentions.everyone === message.mentions.everyone &&
|
||||
if (equal && rawData) {
|
||||
equal = this.mentions.everyone === message.mentions.everyone &&
|
||||
this._timestamp === new Date(rawData.timestamp).getTime() &&
|
||||
this._editedTimestamp === new Date(rawData.edited_timestamp).getTime();
|
||||
}
|
||||
|
||||
return base;
|
||||
return equal;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes the message
|
||||
* @param {number} [timeout=0] How long to wait to delete the message in milliseconds
|
||||
* @returns {Promise<Message, Error>}
|
||||
* @returns {Promise<Message>}
|
||||
* @example
|
||||
* // delete a message
|
||||
* message.delete()
|
||||
@@ -283,8 +255,8 @@ class Message {
|
||||
|
||||
/**
|
||||
* Edit the content of a message
|
||||
* @param {string} content the new content of a message
|
||||
* @returns {Promise<Message, Error>}
|
||||
* @param {string} content The new content for the message
|
||||
* @returns {Promise<Message>}
|
||||
* @example
|
||||
* // update the content of a message
|
||||
* message.edit('This is my new content!')
|
||||
@@ -297,9 +269,9 @@ class Message {
|
||||
|
||||
/**
|
||||
* Reply to a message
|
||||
* @param {string} content the content of the message
|
||||
* @param {MessageOptions} [options = {}] the options to provide
|
||||
* @returns {Promise<Message, Error>}
|
||||
* @param {string} content The content for the message
|
||||
* @param {MessageOptions} [options = {}] The options to provide
|
||||
* @returns {Promise<Message>}
|
||||
* @example
|
||||
* // reply to a message
|
||||
* message.reply('Hey, I'm a reply!')
|
||||
@@ -313,7 +285,7 @@ class Message {
|
||||
|
||||
/**
|
||||
* Pins this message to the channel's pinned messages
|
||||
* @returns {Promise<Message, Error>}
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
pin() {
|
||||
return this.client.rest.methods.pinMessage(this);
|
||||
@@ -321,7 +293,7 @@ class Message {
|
||||
|
||||
/**
|
||||
* Unpins this message from the channel's pinned messages
|
||||
* @returns {Promise<Message, Error>}
|
||||
* @returns {Promise<Message>}
|
||||
*/
|
||||
unpin() {
|
||||
return this.client.rest.methods.unpinMessage(this);
|
||||
|
||||
@@ -1,3 +1,59 @@
|
||||
/**
|
||||
* Represents an embed in an image - e.g. preview of image
|
||||
*/
|
||||
class MessageEmbed {
|
||||
constructor(message, data) {
|
||||
/**
|
||||
* The message this embed is part of
|
||||
* @type {Message}
|
||||
*/
|
||||
this.message = message;
|
||||
/**
|
||||
* The client that instantiated this embed
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = message.client;
|
||||
this.setup(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
/**
|
||||
* The title of this embed, if there is one
|
||||
* @type {?string}
|
||||
*/
|
||||
this.title = data.title;
|
||||
/**
|
||||
* The type of this embed
|
||||
* @type {string}
|
||||
*/
|
||||
this.type = data.type;
|
||||
/**
|
||||
* The description of this embed, if there is one
|
||||
* @type {?string}
|
||||
*/
|
||||
this.description = data.description;
|
||||
/**
|
||||
* The URL of this embed
|
||||
* @type {string}
|
||||
*/
|
||||
this.url = data.url;
|
||||
if (data.thumbnail) {
|
||||
/**
|
||||
* The thumbnail of this embed, if there is one
|
||||
* @type {MessageEmbedThumbnail}
|
||||
*/
|
||||
this.thumbnail = new MessageEmbedThumbnail(this, data.thumbnail);
|
||||
}
|
||||
if (data.provider) {
|
||||
/**
|
||||
* The provider of this embed, if there is one
|
||||
* @type {MessageEmbedProvider}
|
||||
*/
|
||||
this.provider = new MessageEmbedProvider(this, data.provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a thumbnail for a Message embed
|
||||
*/
|
||||
@@ -62,60 +118,4 @@ class MessageEmbedProvider {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents an embed in an image - e.g. preview of image
|
||||
*/
|
||||
class MessageEmbed {
|
||||
constructor(message, data) {
|
||||
/**
|
||||
* The message this embed is part of
|
||||
* @type {Message}
|
||||
*/
|
||||
this.message = message;
|
||||
/**
|
||||
* The client that instantiated this embed
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = message.client;
|
||||
this.setup(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
/**
|
||||
* The title of this embed, if there is one
|
||||
* @type {?string}
|
||||
*/
|
||||
this.title = data.title;
|
||||
/**
|
||||
* The type of this embed
|
||||
* @type {string}
|
||||
*/
|
||||
this.type = data.type;
|
||||
/**
|
||||
* The description of this embed, if there is one
|
||||
* @type {?string}
|
||||
*/
|
||||
this.description = data.description;
|
||||
/**
|
||||
* The URL of this embed
|
||||
* @type {string}
|
||||
*/
|
||||
this.url = data.url;
|
||||
if (data.thumbnail) {
|
||||
/**
|
||||
* The thumbnail of this embed, if there is one
|
||||
* @type {MessageEmbedThumbnail}
|
||||
*/
|
||||
this.thumbnail = new MessageEmbedThumbnail(this, data.thumbnail);
|
||||
}
|
||||
if (data.provider) {
|
||||
/**
|
||||
* The provider of this embed, if there is one
|
||||
* @type {MessageEmbedProvider}
|
||||
*/
|
||||
this.provider = new MessageEmbedProvider(this, data.provider);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageEmbed;
|
||||
|
||||
@@ -8,9 +8,7 @@ class PermissionOverwrites {
|
||||
* @type {GuildChannel}
|
||||
*/
|
||||
this.channel = guildChannel;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
if (data) this.setup(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
@@ -30,7 +28,7 @@ class PermissionOverwrites {
|
||||
|
||||
/**
|
||||
* Delete this Permission Overwrite.
|
||||
* @returns {Promise<PermissionOverwrites, Error>}
|
||||
* @returns {Promise<PermissionOverwrites>}
|
||||
*/
|
||||
delete() {
|
||||
return this.channel.client.rest.methods.deletePermissionOverwrites(this);
|
||||
|
||||
@@ -15,22 +15,18 @@ class Role {
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = guild.client;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
if (data) this.setup(data);
|
||||
}
|
||||
|
||||
equals(role) {
|
||||
return (
|
||||
role &&
|
||||
return role &&
|
||||
this.id === role.id &&
|
||||
this.name === role.name &&
|
||||
this.color === role.color &&
|
||||
this.hoist === role.hoist &&
|
||||
this.position === role.position &&
|
||||
this.permissions === role.permissions &&
|
||||
this.managed === role.managed
|
||||
);
|
||||
this.managed === role.managed;
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
@@ -73,7 +69,7 @@ class Role {
|
||||
|
||||
/**
|
||||
* Deletes the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // delete a role
|
||||
* role.delete()
|
||||
@@ -86,8 +82,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Edits the role
|
||||
* @param {RoleData} data the new data for the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {RoleData} data The new data for the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // edit a role
|
||||
* role.edit({name: 'new role'})
|
||||
@@ -100,8 +96,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set a new name for the role
|
||||
* @param {string} name the new name of the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {string} name The new name of the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the name of the role
|
||||
* role.setName('new role')
|
||||
@@ -114,8 +110,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set a new color for the role
|
||||
* @param {number|string} color the new color for the role, either a hex string or a base 10 number
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {number|string} color The new color for the role, either a hex string or a base 10 number
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the color of a role
|
||||
* role.setColor('#FF0000')
|
||||
@@ -128,8 +124,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set whether or not the role should be hoisted
|
||||
* @param {boolean} hoist whether or not to hoist the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {boolean} hoist Whether or not to hoist the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the hoist of the role
|
||||
* role.setHoist(true)
|
||||
@@ -142,8 +138,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set the position of the role
|
||||
* @param {number} position the position of the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {number} position The position of the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the position of the role
|
||||
* role.setPosition(1)
|
||||
@@ -156,8 +152,8 @@ class Role {
|
||||
|
||||
/**
|
||||
* Set the permissions of the role
|
||||
* @param {Array<string>} permissions the permissions of the role
|
||||
* @returns {Promise<Role, Error>}
|
||||
* @param {string[]} permissions The permissions of the role
|
||||
* @returns {Promise<Role>}
|
||||
* @example
|
||||
* // set the permissions of the role
|
||||
* role.setPermissions(['KICK_MEMBERS', 'BAN_MEMBERS'])
|
||||
@@ -180,14 +176,13 @@ class Role {
|
||||
for (const permissionName in Constants.PermissionFlags) {
|
||||
serializedPermissions[permissionName] = this.hasPermission(permissionName);
|
||||
}
|
||||
|
||||
return serializedPermissions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not the role includes the given permission
|
||||
* @param {string} permission the name of the permission to test
|
||||
* @param {boolean} [explicit=false] whether or not the inclusion of the permission is explicit
|
||||
* @param {PermissionResolvable} permission The name of the permission to test
|
||||
* @param {boolean} [explicit=false] Whether to require the role to explicitly have the exact permission
|
||||
* @returns {boolean}
|
||||
* @example
|
||||
* // see if a role can ban a member
|
||||
@@ -198,20 +193,8 @@ class Role {
|
||||
* }
|
||||
*/
|
||||
hasPermission(permission, explicit = false) {
|
||||
if (typeof permission === 'string') {
|
||||
permission = Constants.PermissionFlags[permission];
|
||||
}
|
||||
|
||||
if (!permission) {
|
||||
throw Constants.Errors.NOT_A_PERMISSION;
|
||||
}
|
||||
|
||||
if (!explicit) {
|
||||
if ((this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
permission = this.client.resolver.resolvePermission(permission);
|
||||
if (!explicit && (this.permissions & Constants.PermissionFlags.ADMINISTRATOR) > 0) return true;
|
||||
return (this.permissions & permission) > 0;
|
||||
}
|
||||
|
||||
@@ -230,9 +213,7 @@ class Role {
|
||||
*/
|
||||
get hexColor() {
|
||||
let col = this.color.toString(16);
|
||||
while (col.length < 6) {
|
||||
col = `0${col}`;
|
||||
}
|
||||
while (col.length < 6) col = `0${col}`;
|
||||
return `#${col}`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,7 +8,6 @@ const Collection = require('../util/Collection');
|
||||
* @implements {TextBasedChannel}
|
||||
*/
|
||||
class TextChannel extends GuildChannel {
|
||||
|
||||
constructor(guild, data) {
|
||||
super(guild, data);
|
||||
this.messages = new Collection();
|
||||
|
||||
@@ -8,9 +8,7 @@ const Constants = require('../util/Constants');
|
||||
class User {
|
||||
constructor(client, data) {
|
||||
this.client = client;
|
||||
if (data) {
|
||||
this.setup(data);
|
||||
}
|
||||
if (data) this.setup(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
@@ -72,9 +70,7 @@ class User {
|
||||
* @readonly
|
||||
*/
|
||||
get avatarURL() {
|
||||
if (!this.avatar) {
|
||||
return null;
|
||||
}
|
||||
if (!this.avatar) return null;
|
||||
return Constants.Endpoints.avatar(this.id, this.avatar);
|
||||
}
|
||||
|
||||
@@ -89,28 +85,23 @@ class User {
|
||||
/**
|
||||
* Checks if the user is equal to another. It compares username, ID, discriminator, status and the game being played.
|
||||
* It is recommended to compare equality by using `user.id === user2.id` unless you want to compare all properties.
|
||||
* @param {User} user the user to compare
|
||||
* @param {User} user The user to compare
|
||||
* @returns {boolean}
|
||||
*/
|
||||
equals(user) {
|
||||
let base = user &&
|
||||
let equal = user &&
|
||||
this.username === user.username &&
|
||||
this.id === user.id &&
|
||||
this.discriminator === user.discriminator &&
|
||||
this.avatar === user.avatar &&
|
||||
this.bot === Boolean(user.bot);
|
||||
|
||||
if (base) {
|
||||
if (user.status) {
|
||||
base = this.status === user.status;
|
||||
}
|
||||
|
||||
if (user.game) {
|
||||
base = this.game === user.game;
|
||||
}
|
||||
if (equal) {
|
||||
if (user.status) equal = this.status === user.status;
|
||||
if (equal && user.game) equal = this.game === user.game;
|
||||
}
|
||||
|
||||
return base;
|
||||
return equal;
|
||||
}
|
||||
|
||||
sendMessage() {
|
||||
|
||||
@@ -32,7 +32,7 @@ class VoiceChannel extends GuildChannel {
|
||||
|
||||
/**
|
||||
* Sets the bitrate of the channel
|
||||
* @param {number} bitrate the new bitrate
|
||||
* @param {number} bitrate The new bitrate
|
||||
* @returns {Promise<VoiceChannel>}
|
||||
* @example
|
||||
* // set the bitrate of a voice channel
|
||||
@@ -46,7 +46,7 @@ class VoiceChannel extends GuildChannel {
|
||||
|
||||
/**
|
||||
* Attempts to join this Voice Channel
|
||||
* @returns {Promise<VoiceConnection, Error>}
|
||||
* @returns {Promise<VoiceConnection>}
|
||||
* @example
|
||||
* // join a voice channel
|
||||
* voiceChannel.join()
|
||||
@@ -64,12 +64,8 @@ class VoiceChannel extends GuildChannel {
|
||||
* voiceChannel.leave();
|
||||
*/
|
||||
leave() {
|
||||
const exists = this.client.voice.connections.get(this.guild.id);
|
||||
if (exists) {
|
||||
if (exists.channel.id === this.id) {
|
||||
exists.disconnect();
|
||||
}
|
||||
}
|
||||
const connection = this.client.voice.connections.get(this.guild.id);
|
||||
if (connection && connection.channel.id === this.id) connection.disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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