mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
change maps to Collections
This commit is contained in:
@@ -7,6 +7,7 @@ const ClientManager = require('./ClientManager');
|
|||||||
const ClientDataResolver = require('./ClientDataResolver');
|
const ClientDataResolver = require('./ClientDataResolver');
|
||||||
const WebSocketManager = require('./websocket/WebSocketManager');
|
const WebSocketManager = require('./websocket/WebSocketManager');
|
||||||
const ActionsManager = require('./actions/ActionsManager');
|
const ActionsManager = require('./actions/ActionsManager');
|
||||||
|
const Collection = require('../util/Collection');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The starting point for making a Discord Bot.
|
* The starting point for making a Discord Bot.
|
||||||
@@ -59,20 +60,20 @@ class Client extends EventEmitter {
|
|||||||
this.actions = new ActionsManager(this);
|
this.actions = new ActionsManager(this);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A map of the Client's stored users
|
* A Collection of the Client's stored users
|
||||||
* @type {Map<String, User>}
|
* @type {Collection<String, User>}
|
||||||
*/
|
*/
|
||||||
this.users = new Map();
|
this.users = new Collection();
|
||||||
/**
|
/**
|
||||||
* A map of the Client's stored guilds
|
* A Collection of the Client's stored guilds
|
||||||
* @type {Map<String, Guild>}
|
* @type {Collection<String, Guild>}
|
||||||
*/
|
*/
|
||||||
this.guilds = new Map();
|
this.guilds = new Collection();
|
||||||
/**
|
/**
|
||||||
* A map of the Client's stored channels
|
* A Collection of the Client's stored channels
|
||||||
* @type {Map<String, Channel>}
|
* @type {Collection<String, Channel>}
|
||||||
*/
|
*/
|
||||||
this.channels = new Map();
|
this.channels = new Collection();
|
||||||
/**
|
/**
|
||||||
* The authorization token for the logged in user/bot.
|
* The authorization token for the logged in user/bot.
|
||||||
* @type {?String}
|
* @type {?String}
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
const Channel = require('./Channel');
|
const Channel = require('./Channel');
|
||||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||||
const User = require('./User');
|
const User = require('./User');
|
||||||
|
const Collection = require('../util/Collection');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Direct Message Channel between two users.
|
* Represents a Direct Message Channel between two users.
|
||||||
@@ -10,7 +11,7 @@ const User = require('./User');
|
|||||||
class DMChannel extends Channel {
|
class DMChannel extends Channel {
|
||||||
constructor(client, data) {
|
constructor(client, data) {
|
||||||
super(client, data);
|
super(client, data);
|
||||||
this.messages = new Map();
|
this.messages = new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
setup(data) {
|
setup(data) {
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ const User = require('./User');
|
|||||||
const GuildMember = require('./GuildMember');
|
const GuildMember = require('./GuildMember');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const Role = require('./Role');
|
const Role = require('./Role');
|
||||||
|
const Collection = require('../util/Collection');
|
||||||
|
|
||||||
function arraysEqual(a, b) {
|
function arraysEqual(a, b) {
|
||||||
if (a === b) return true;
|
if (a === b) return true;
|
||||||
@@ -30,22 +31,22 @@ class Guild {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Map of members that are in this Guild. The key is the member's ID, the value is the member.
|
* A Collection of members that are in this Guild. The key is the member's ID, the value is the member.
|
||||||
* @type {Map<String, GuildMember>}
|
* @type {Collection<String, GuildMember>}
|
||||||
*/
|
*/
|
||||||
this.members = new Map();
|
this.members = new Collection();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Map of channels that are in this Guild. The key is the channel's ID, the value is the channel.
|
* A Collection of channels that are in this Guild. The key is the channel's ID, the value is the channel.
|
||||||
* @type {Map<String, GuildChannel>}
|
* @type {Collection<String, GuildChannel>}
|
||||||
*/
|
*/
|
||||||
this.channels = new Map();
|
this.channels = new Collection();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A Map of roles that are in this Guild. The key is the role's ID, the value is the role.
|
* A Collection of roles that are in this Guild. The key is the role's ID, the value is the role.
|
||||||
* @type {Map<String, Role>}
|
* @type {Collection<String, Role>}
|
||||||
*/
|
*/
|
||||||
this.roles = new Map();
|
this.roles = new Collection();
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return;
|
return;
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ const PermissionOverwrites = require('./PermissionOverwrites');
|
|||||||
const Role = require('./Role');
|
const Role = require('./Role');
|
||||||
const EvaluatedPermissions = require('./EvaluatedPermissions');
|
const EvaluatedPermissions = require('./EvaluatedPermissions');
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
|
const Collection = require('../util/Collection');
|
||||||
|
|
||||||
function arraysEqual(a, b) {
|
function arraysEqual(a, b) {
|
||||||
if (a === b) return true;
|
if (a === b) return true;
|
||||||
@@ -53,9 +54,9 @@ class GuildChannel extends Channel {
|
|||||||
this.ow = data.permission_overwrites;
|
this.ow = data.permission_overwrites;
|
||||||
/**
|
/**
|
||||||
* A map of permission overwrites in this channel for roles and users.
|
* A map of permission overwrites in this channel for roles and users.
|
||||||
* @type {Map<String, PermissionOverwrites>}
|
* @type {Collection<String, PermissionOverwrites>}
|
||||||
*/
|
*/
|
||||||
this.permissionOverwrites = new Map();
|
this.permissionOverwrites = new Collection();
|
||||||
if (data.permission_overwrites) {
|
if (data.permission_overwrites) {
|
||||||
for (const overwrite of data.permission_overwrites) {
|
for (const overwrite of data.permission_overwrites) {
|
||||||
this.permissionOverwrites.set(overwrite.id, new PermissionOverwrites(this, overwrite));
|
this.permissionOverwrites.set(overwrite.id, new PermissionOverwrites(this, overwrite));
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
const Collection = require('../util/Collection');
|
||||||
/**
|
/**
|
||||||
* Represents a Message on Discord
|
* Represents a Message on Discord
|
||||||
*/
|
*/
|
||||||
@@ -79,16 +80,16 @@ class Message {
|
|||||||
*/
|
*/
|
||||||
this.attachments = data.attachments;
|
this.attachments = data.attachments;
|
||||||
/**
|
/**
|
||||||
* An object containing a further users or roles map
|
* An object containing a further users, roles or channels collections
|
||||||
* @type {Object}
|
* @type {Object}
|
||||||
* @property {Map<String, User>} mentions.users Mentioned users, maps their ID to the user object.
|
* @property {Collection<String, User>} mentions.users Mentioned users, maps their ID to the user object.
|
||||||
* @property {Map<String, Role>} mentions.roles Mentioned roles, maps their ID to the role object.
|
* @property {Collection<String, Role>} mentions.roles Mentioned roles, maps their ID to the role object.
|
||||||
* @property {Map<String, GuildChannel>} mentions.channels Mentioned channels, maps their ID to the channel object.
|
* @property {Collection<String, GuildChannel>} mentions.channels Mentioned channels, maps their ID to the channel object.
|
||||||
*/
|
*/
|
||||||
this.mentions = {
|
this.mentions = {
|
||||||
users: new Map(),
|
users: new Collection(),
|
||||||
roles: new Map(),
|
roles: new Collection(),
|
||||||
channels: new Map(),
|
channels: new Collection(),
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* The ID of the message (unique in the channel it was sent)
|
* The ID of the message (unique in the channel it was sent)
|
||||||
@@ -174,7 +175,7 @@ class Message {
|
|||||||
if (data.id) {
|
if (data.id) {
|
||||||
this.id = data.id;
|
this.id = data.id;
|
||||||
}
|
}
|
||||||
if (this.channel.guild) {
|
if (this.channel.guild && data.content) {
|
||||||
const channMentionsRaw = data.content.match(/<#([0-9]{14,20})>/g) || [];
|
const channMentionsRaw = data.content.match(/<#([0-9]{14,20})>/g) || [];
|
||||||
for (const raw of channMentionsRaw) {
|
for (const raw of channMentionsRaw) {
|
||||||
const chan = this.channel.guild.channels.get(raw.match(/([0-9]{14,20})/g)[0]);
|
const chan = this.channel.guild.channels.get(raw.match(/([0-9]{14,20})/g)[0]);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const GuildChannel = require('./GuildChannel');
|
const GuildChannel = require('./GuildChannel');
|
||||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||||
|
const Collection = require('../util/Collection');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Server Text Channel on Discord.
|
* Represents a Server Text Channel on Discord.
|
||||||
@@ -10,7 +11,7 @@ class TextChannel extends GuildChannel {
|
|||||||
|
|
||||||
constructor(guild, data) {
|
constructor(guild, data) {
|
||||||
super(guild, data);
|
super(guild, data);
|
||||||
this.messages = new Map();
|
this.messages = new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
setup(data) {
|
setup(data) {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
const GuildChannel = require('./GuildChannel');
|
const GuildChannel = require('./GuildChannel');
|
||||||
|
const Collection = require('../util/Collection');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents a Server Voice Channel on Discord.
|
* Represents a Server Voice Channel on Discord.
|
||||||
@@ -9,9 +10,9 @@ class VoiceChannel extends GuildChannel {
|
|||||||
super(guild, data);
|
super(guild, data);
|
||||||
/**
|
/**
|
||||||
* The members in this Voice Channel.
|
* The members in this Voice Channel.
|
||||||
* @type {Map<String, GuildMember>}
|
* @type {Collection<String, GuildMember>}
|
||||||
*/
|
*/
|
||||||
this.members = new Map();
|
this.members = new Collection();
|
||||||
}
|
}
|
||||||
|
|
||||||
setup(data) {
|
setup(data) {
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
const Collection = require('../../util/Collection');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Interface for classes that have text-channel-like features
|
* Interface for classes that have text-channel-like features
|
||||||
* @interface
|
* @interface
|
||||||
@@ -6,10 +8,10 @@ class TextBasedChannel {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
/**
|
/**
|
||||||
* A Map containing the messages sent to this channel.
|
* A Collection containing the messages sent to this channel.
|
||||||
* @type {Map<String, Message>}
|
* @type {Collection<String, Message>}
|
||||||
*/
|
*/
|
||||||
this.messages = new Map();
|
this.messages = new Collection();
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Send a message to this channel
|
* Send a message to this channel
|
||||||
|
|||||||
109
src/util/Collection.js
Normal file
109
src/util/Collection.js
Normal file
@@ -0,0 +1,109 @@
|
|||||||
|
/**
|
||||||
|
* A utility class to help make it easier to access the data stores
|
||||||
|
* @extends {Map}
|
||||||
|
*/
|
||||||
|
class Collection extends Map {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an ordered array of the values of this collection.
|
||||||
|
* @returns {Array}
|
||||||
|
* @example
|
||||||
|
* // identical to:
|
||||||
|
* Array.from(collection.values());
|
||||||
|
*/
|
||||||
|
array() {
|
||||||
|
return Array.from(this.values());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the first item in this collection.
|
||||||
|
* @returns {Object}
|
||||||
|
* @example
|
||||||
|
* // identical to:
|
||||||
|
* Array.from(collection.values())[0];
|
||||||
|
*/
|
||||||
|
first() {
|
||||||
|
return this.array()[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the last item in this collection.
|
||||||
|
* @returns {Object}
|
||||||
|
*/
|
||||||
|
last() {
|
||||||
|
const arr = this.array();
|
||||||
|
return arr[arr.length - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a random item from this collection.
|
||||||
|
* @returns {Object}
|
||||||
|
*/
|
||||||
|
random() {
|
||||||
|
const arr = this.array();
|
||||||
|
return arr[Math.floor(Math.random() * arr.length)];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The length (size) of this collection.
|
||||||
|
* @readonly
|
||||||
|
*/
|
||||||
|
get length() {
|
||||||
|
return this.size;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of items where `item[key] === value` of the collection
|
||||||
|
* @param {String} key the key to filter bby
|
||||||
|
* @param {any} value the expected value
|
||||||
|
* @returns {Array<Object>}
|
||||||
|
* @example
|
||||||
|
* collection.getAll('username', 'Bob');
|
||||||
|
*/
|
||||||
|
findAll(key, value) {
|
||||||
|
const results = [];
|
||||||
|
for (const item of this.array()) {
|
||||||
|
if (item[key] === value) {
|
||||||
|
results.push(item);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a single item where `item[key] === value`
|
||||||
|
* @param {String} key the key to filter bby
|
||||||
|
* @param {any} value the expected value
|
||||||
|
* @returns {Object}
|
||||||
|
* @example
|
||||||
|
* collection.get('id', '123123...');
|
||||||
|
*/
|
||||||
|
find(key, value) {
|
||||||
|
for (const item of this.array()) {
|
||||||
|
if (item[key] === value) {
|
||||||
|
return item;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns true if the collection has an item where `item[key] === value`
|
||||||
|
* @param {String} key the key to filter bby
|
||||||
|
* @param {any} value the expected value
|
||||||
|
* @returns {Object}
|
||||||
|
* @example
|
||||||
|
* if (collection.exists('id', '123123...')) {
|
||||||
|
* console.log('user here!');
|
||||||
|
* }
|
||||||
|
*/
|
||||||
|
exists(key, value) {
|
||||||
|
return Boolean(this.get(key, value));
|
||||||
|
}
|
||||||
|
|
||||||
|
_arrayMethod(method, args) {
|
||||||
|
return Array.prototype[method].apply(this.array(), args);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = Collection;
|
||||||
Reference in New Issue
Block a user