mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 00:23:30 +01:00
Store and Model Refactor (#1618)
* UserStore refactor * Create ChannelStore, remove redundant methods in ClientDataManager * Create GuildStore * Emoji stuff * Use a Base class where possible to reduce code duplication * Remove unnecessary comments from ChannelStore * Add Base._clone(); * Remove unused ClientDataManager methods * Refactor some more stuff * ESLint * Move Client#fetchUser to client.users.fetch * Remove .has checks and just see if .get is truthy * Fix guild member chunk error * ESLint * Fix typo * Fix channel storing for user bots * Remove ClientDataManager * GuildChannelStore * Reduce use of Util.cloneObject * and this one too * update typings * Fix MessageUpdate handling (#1507) * Fix role updates (probably fixes #1525) * fix for eslint * Address some of appell's comments * Use debug constant * start message store crap if it's ugly tell me later k * fix that * message store but works™️ * clean up guild stuff * clean up channel store stuff * clean up channel event handling * does this message stuff work? find out soon in the next episode of dIsCoRd.Js * eslint * emojis * emojis and reactions * hi my name is eslint and im A LIL SHIT * so i forgot this huh * user stuff * Fix @class * Fix message stuff * Fix user store docs * Document all the bases * fix the super things * tidy up remove * fix textbasedchannel * fix that too * fix emoji store * make voice state stuff less ugly * make voice states even less ugly * make members less bad * fix bug * fix that too * fix reactions * how was this broken for so long * role store * remove super._patch from UserConnection * Rename UserProfile#setup to _patch * remove unnecessary super calls * update docgen dep (pls fix travis thx) * doc messagestore * fix docs * message store docs * things * DOCS PLS * more things * Document TextBasedChannel#messages as a MessageStore * Rebase * All the stores!
This commit is contained in:
28
src/structures/Base.js
Normal file
28
src/structures/Base.js
Normal file
@@ -0,0 +1,28 @@
|
||||
/**
|
||||
* Represents a data model that is identifiable by a Snowflake (i.e. Discord API data models).
|
||||
*/
|
||||
class Base {
|
||||
constructor(client) {
|
||||
/**
|
||||
* The client that instantiated this
|
||||
* @name Base#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
}
|
||||
|
||||
_clone() {
|
||||
return Object.assign(Object.create(this), this);
|
||||
}
|
||||
|
||||
_patch(data) { return data; }
|
||||
|
||||
_update(data) {
|
||||
const clone = this._clone();
|
||||
this._patch(data);
|
||||
return clone;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Base;
|
||||
@@ -1,18 +1,14 @@
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Base = require('./Base');
|
||||
const Constants = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* Represents any channel on Discord.
|
||||
* @extends {Base}
|
||||
*/
|
||||
class Channel {
|
||||
class Channel extends Base {
|
||||
constructor(client, data) {
|
||||
/**
|
||||
* The client that instantiated the Channel
|
||||
* @name Channel#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
super(client);
|
||||
|
||||
const type = Object.keys(Constants.ChannelTypes)[data.type];
|
||||
/**
|
||||
@@ -26,10 +22,10 @@ class Channel {
|
||||
*/
|
||||
this.type = type ? type.toLowerCase() : 'unknown';
|
||||
|
||||
if (data) this.setup(data);
|
||||
if (data) this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* The unique ID of the channel
|
||||
* @type {Snowflake}
|
||||
|
||||
@@ -1,23 +1,18 @@
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Constants = require('../util/Constants');
|
||||
const Base = require('./Base');
|
||||
|
||||
/**
|
||||
* Represents a Client OAuth2 Application.
|
||||
* @extends {Base}
|
||||
*/
|
||||
class ClientApplication {
|
||||
class ClientApplication extends Base {
|
||||
constructor(client, data) {
|
||||
/**
|
||||
* The client that instantiated the application
|
||||
* @name ClientApplication#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
|
||||
this.setup(data);
|
||||
super(client);
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* The ID of the app
|
||||
* @type {Snowflake}
|
||||
@@ -101,7 +96,7 @@ class ClientApplication {
|
||||
* The owner of this OAuth application
|
||||
* @type {?User}
|
||||
*/
|
||||
this.owner = this.client.dataManager.newUser(data.owner);
|
||||
this.owner = this.client.users.create(data.owner);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,8 +14,8 @@ const { TypeError } = require('../errors');
|
||||
* @extends {User}
|
||||
*/
|
||||
class ClientUser extends User {
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
_patch(data) {
|
||||
super._patch(data);
|
||||
|
||||
/**
|
||||
* Whether or not this account has been verified
|
||||
@@ -82,8 +82,8 @@ class ClientUser extends User {
|
||||
|
||||
/**
|
||||
* All of the user's guild settings
|
||||
* @type {Collection<Snowflake, ClientUserGuildSettings>}
|
||||
* <warn>This is only filled when using a user account.</warn>
|
||||
* @type {Collection<Snowflake, ClientUserGuildSettings>}
|
||||
*/
|
||||
this.guildSettings = new Collection();
|
||||
if (data.user_guild_settings) {
|
||||
@@ -341,7 +341,7 @@ class ClientUser extends User {
|
||||
|
||||
const timeout = this.client.setTimeout(() => {
|
||||
this.client.removeListener(Constants.Events.GUILD_CREATE, handleGuild);
|
||||
resolve(this.client.dataManager.newGuild(data));
|
||||
resolve(this.client.guilds.create(data));
|
||||
}, 10000);
|
||||
return undefined;
|
||||
}, reject)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
const Channel = require('./Channel');
|
||||
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
||||
const Collection = require('../util/Collection');
|
||||
const MessageStore = require('../stores/MessageStore');
|
||||
|
||||
/**
|
||||
* Represents a direct message channel between two users.
|
||||
@@ -10,18 +10,18 @@ const Collection = require('../util/Collection');
|
||||
class DMChannel extends Channel {
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
this.messages = new Collection();
|
||||
this.messages = new MessageStore(this);
|
||||
this._typing = new Map();
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
_patch(data) {
|
||||
super._patch(data);
|
||||
|
||||
/**
|
||||
* The recipient on the other end of the DM
|
||||
* @type {User}
|
||||
*/
|
||||
this.recipient = this.client.dataManager.newUser(data.recipients[0]);
|
||||
this.recipient = this.client.users.create(data.recipients[0]);
|
||||
|
||||
this.lastMessageID = data.last_message_id;
|
||||
}
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const Collection = require('../util/Collection');
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Base = require('./Base');
|
||||
|
||||
/**
|
||||
* Represents a custom emoji.
|
||||
* @extends {Base}
|
||||
*/
|
||||
class Emoji {
|
||||
class Emoji extends Base {
|
||||
constructor(guild, data) {
|
||||
/**
|
||||
* The client that instantiated this object
|
||||
* @name Emoji#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: guild.client });
|
||||
super(guild.client);
|
||||
|
||||
/**
|
||||
* The guild this emoji is part of
|
||||
@@ -21,10 +17,10 @@ class Emoji {
|
||||
*/
|
||||
this.guild = guild;
|
||||
|
||||
this.setup(data);
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* The ID of the emoji
|
||||
* @type {Snowflake}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
const Channel = require('./Channel');
|
||||
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
||||
const Collection = require('../util/Collection');
|
||||
const MessageStore = require('../stores/MessageStore');
|
||||
const Constants = require('../util/Constants');
|
||||
|
||||
/*
|
||||
@@ -33,12 +34,12 @@ const Constants = require('../util/Constants');
|
||||
class GroupDMChannel extends Channel {
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
this.messages = new Collection();
|
||||
this.messages = new MessageStore(this);
|
||||
this._typing = new Map();
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
_patch(data) {
|
||||
super._patch(data);
|
||||
|
||||
/**
|
||||
* The name of this Group DM, can be null if one isn't set
|
||||
@@ -88,7 +89,7 @@ class GroupDMChannel extends Channel {
|
||||
|
||||
if (data.recipients) {
|
||||
for (const recipient of data.recipients) {
|
||||
const user = this.client.dataManager.newUser(recipient);
|
||||
const user = this.client.users.create(recipient);
|
||||
this.recipients.set(user.id, user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const Long = require('long');
|
||||
const User = require('./User');
|
||||
const Role = require('./Role');
|
||||
const Emoji = require('./Emoji');
|
||||
const Invite = require('./Invite');
|
||||
@@ -15,40 +14,40 @@ const Util = require('../util/Util');
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Shared = require('./shared');
|
||||
const GuildMemberStore = require('../stores/GuildMemberStore');
|
||||
const RoleStore = require('../stores/RoleStore');
|
||||
const EmojiStore = require('../stores/EmojiStore');
|
||||
const GuildChannelStore = require('../stores/GuildChannelStore');
|
||||
const Base = require('./Base');
|
||||
const { Error, TypeError } = require('../errors');
|
||||
|
||||
/**
|
||||
* Represents a guild (or a server) on Discord.
|
||||
* <info>It's recommended to see if a guild is available before performing operations or reading data from it. You can
|
||||
* check this with `guild.available`.</info>
|
||||
* @extends {Base}
|
||||
*/
|
||||
class Guild {
|
||||
class Guild extends Base {
|
||||
constructor(client, data) {
|
||||
/**
|
||||
* The client that created the instance of the guild
|
||||
* @name Guild#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
super(client);
|
||||
|
||||
/**
|
||||
* A collection of members that are in this guild. The key is the member's ID, the value is the member
|
||||
* @type {Collection<Snowflake, GuildMember>}
|
||||
*/
|
||||
this.members = new Collection();
|
||||
this.members = new GuildMemberStore(this);
|
||||
|
||||
/**
|
||||
* A collection of channels that are in this guild. The key is the channel's ID, the value is the channel
|
||||
* @type {Collection<Snowflake, GuildChannel>}
|
||||
* @type {GuildChannelStore<Snowflake, GuildChannel>}
|
||||
*/
|
||||
this.channels = new Collection();
|
||||
this.channels = new GuildChannelStore(this);
|
||||
|
||||
/**
|
||||
* A collection of roles that are in this guild. The key is the role's ID, the value is the role
|
||||
* @type {Collection<Snowflake, Role>}
|
||||
*/
|
||||
this.roles = new Collection();
|
||||
this.roles = new RoleStore(this);
|
||||
|
||||
/**
|
||||
* A collection of presences in this guild
|
||||
@@ -70,7 +69,7 @@ class Guild {
|
||||
*/
|
||||
this.id = data.id;
|
||||
} else {
|
||||
this.setup(data);
|
||||
this._patch(data);
|
||||
if (!data.channels) this.available = false;
|
||||
}
|
||||
}
|
||||
@@ -80,7 +79,7 @@ class Guild {
|
||||
* @param {*} data The raw data of the guild
|
||||
* @private
|
||||
*/
|
||||
setup(data) { // eslint-disable-line complexity
|
||||
_patch(data) {
|
||||
/**
|
||||
* The name of the guild
|
||||
* @type {string}
|
||||
@@ -177,7 +176,7 @@ class Guild {
|
||||
|
||||
if (data.members) {
|
||||
this.members.clear();
|
||||
for (const guildUser of data.members) this._addMember(guildUser, false);
|
||||
for (const guildUser of data.members) this.members.create(guildUser);
|
||||
}
|
||||
|
||||
if (data.owner_id) {
|
||||
@@ -190,15 +189,14 @@ class Guild {
|
||||
|
||||
if (data.channels) {
|
||||
this.channels.clear();
|
||||
for (const channel of data.channels) this.client.dataManager.newChannel(channel, this);
|
||||
for (const rawChannel of data.channels) {
|
||||
this.client.channels.create(rawChannel, this);
|
||||
}
|
||||
}
|
||||
|
||||
if (data.roles) {
|
||||
this.roles.clear();
|
||||
for (const role of data.roles) {
|
||||
const newRole = new Role(this, role);
|
||||
this.roles.set(newRole.id, newRole);
|
||||
}
|
||||
for (const role of data.roles) this.roles.create(role);
|
||||
}
|
||||
|
||||
if (data.presences) {
|
||||
@@ -207,31 +205,18 @@ class Guild {
|
||||
}
|
||||
}
|
||||
|
||||
this._rawVoiceStates = new Collection();
|
||||
this.voiceStates = new VoiceStateCollection(this);
|
||||
if (data.voice_states) {
|
||||
for (const voiceState of data.voice_states) {
|
||||
this._rawVoiceStates.set(voiceState.user_id, voiceState);
|
||||
const member = this.members.get(voiceState.user_id);
|
||||
if (member) {
|
||||
member.serverMute = voiceState.mute;
|
||||
member.serverDeaf = voiceState.deaf;
|
||||
member.selfMute = voiceState.self_mute;
|
||||
member.selfDeaf = voiceState.self_deaf;
|
||||
member.voiceSessionID = voiceState.session_id;
|
||||
member.voiceChannelID = voiceState.channel_id;
|
||||
this.channels.get(voiceState.channel_id).members.set(member.user.id, member);
|
||||
}
|
||||
}
|
||||
for (const voiceState of data.voice_states) this.voiceStates.set(voiceState.user_id, voiceState);
|
||||
}
|
||||
|
||||
if (!this.emojis) {
|
||||
/**
|
||||
* A collection of emojis that are in this guild
|
||||
* The key is the emoji's ID, the value is the emoji
|
||||
* @type {Collection<Snowflake, Emoji>}
|
||||
* A collection of emojis that are in this guild. The key is the emoji's ID, the value is the emoji.
|
||||
* @type {EmojiStore<Snowflake, Emoji>}
|
||||
*/
|
||||
this.emojis = new Collection();
|
||||
if (data.emojis) for (const emoji of data.emojis) this.emojis.set(emoji.id, new Emoji(this, emoji));
|
||||
this.emojis = new EmojiStore(this);
|
||||
for (const emoji of data.emojis) this.emojis.create(emoji);
|
||||
} else {
|
||||
this.client.actions.GuildEmojisUpdate.handle({
|
||||
guild_id: this.id,
|
||||
@@ -460,7 +445,7 @@ class Guild {
|
||||
bans.reduce((collection, ban) => {
|
||||
collection.set(ban.user.id, {
|
||||
reason: ban.reason,
|
||||
user: this.client.dataManager.newUser(ban.user),
|
||||
user: this.client.users.create(ban.user),
|
||||
});
|
||||
return collection;
|
||||
}, new Collection())
|
||||
@@ -1214,69 +1199,6 @@ class Guild {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
_addMember(guildUser, emitEvent = true) {
|
||||
const existing = this.members.has(guildUser.user.id);
|
||||
if (!(guildUser.user instanceof User)) guildUser.user = this.client.dataManager.newUser(guildUser.user);
|
||||
|
||||
guildUser.joined_at = guildUser.joined_at || 0;
|
||||
const member = new GuildMember(this, guildUser);
|
||||
this.members.set(member.id, member);
|
||||
|
||||
if (this._rawVoiceStates && this._rawVoiceStates.has(member.user.id)) {
|
||||
const voiceState = this._rawVoiceStates.get(member.user.id);
|
||||
member.serverMute = voiceState.mute;
|
||||
member.serverDeaf = voiceState.deaf;
|
||||
member.selfMute = voiceState.self_mute;
|
||||
member.selfDeaf = voiceState.self_deaf;
|
||||
member.voiceSessionID = voiceState.session_id;
|
||||
member.voiceChannelID = voiceState.channel_id;
|
||||
if (this.client.channels.has(voiceState.channel_id)) {
|
||||
this.client.channels.get(voiceState.channel_id).members.set(member.user.id, member);
|
||||
} else {
|
||||
this.client.emit('warn', `Member ${member.id} added in guild ${this.id} with an uncached voice channel`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a user joins a guild.
|
||||
* @event Client#guildMemberAdd
|
||||
* @param {GuildMember} member The member that has joined a guild
|
||||
*/
|
||||
if (this.client.ws.connection.status === Constants.Status.READY && emitEvent && !existing) {
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, member);
|
||||
}
|
||||
|
||||
return member;
|
||||
}
|
||||
|
||||
_updateMember(member, data) {
|
||||
const oldMember = Util.cloneObject(member);
|
||||
|
||||
if (data.roles) member._roles = data.roles;
|
||||
if (typeof data.nick !== 'undefined') member.nickname = data.nick;
|
||||
|
||||
const notSame = member.nickname !== oldMember.nickname || !Util.arraysEqual(member._roles, oldMember._roles);
|
||||
|
||||
if (this.client.ws.connection.status === Constants.Status.READY && notSame) {
|
||||
/**
|
||||
* Emitted whenever a guild member changes - i.e. new role, removed role, nickname.
|
||||
* @event Client#guildMemberUpdate
|
||||
* @param {GuildMember} oldMember The member before the update
|
||||
* @param {GuildMember} newMember The member after the update
|
||||
*/
|
||||
this.client.emit(Constants.Events.GUILD_MEMBER_UPDATE, oldMember, member);
|
||||
}
|
||||
|
||||
return {
|
||||
old: oldMember,
|
||||
mem: member,
|
||||
};
|
||||
}
|
||||
|
||||
_removeMember(guildMember) {
|
||||
this.members.delete(guildMember.id);
|
||||
}
|
||||
|
||||
_memberSpeakUpdate(user, speaking) {
|
||||
const member = this.members.get(user);
|
||||
if (member && member.speaking !== speaking) {
|
||||
@@ -1391,4 +1313,23 @@ class Guild {
|
||||
}
|
||||
}
|
||||
|
||||
class VoiceStateCollection extends Collection {
|
||||
constructor(guild) {
|
||||
super();
|
||||
this.guild = guild;
|
||||
}
|
||||
set(id, voiceState) {
|
||||
super.set(id, voiceState);
|
||||
const member = this.guild.members.get(id);
|
||||
if (member) {
|
||||
if (member.voiceChannel && member.voiceChannel.id !== voiceState.channel_id) {
|
||||
member.voiceChannel.members.delete(member.id);
|
||||
}
|
||||
if (!voiceState.channel_id) member.speaking = null;
|
||||
const newChannel = this.guild.channels.get(voiceState.channel_id);
|
||||
if (newChannel) newChannel.members.set(member.user.id, member);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Guild;
|
||||
|
||||
@@ -51,7 +51,7 @@ const Actions = {
|
||||
*/
|
||||
class GuildAuditLogs {
|
||||
constructor(guild, data) {
|
||||
if (data.users) for (const user of data.users) guild.client.dataManager.newUser(user);
|
||||
if (data.users) for (const user of data.users) guild.client.users.create(user);
|
||||
/**
|
||||
* Cached webhooks
|
||||
* @type {Collection<Snowflake, Webhook>}
|
||||
|
||||
@@ -22,8 +22,8 @@ class GuildChannel extends Channel {
|
||||
this.guild = guild;
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
_patch(data) {
|
||||
super._patch(data);
|
||||
|
||||
/**
|
||||
* The name of the guild channel
|
||||
@@ -241,7 +241,11 @@ class GuildChannel extends Channel {
|
||||
user_limit: data.userLimit || this.userLimit,
|
||||
},
|
||||
reason,
|
||||
}).then(newData => this.client.actions.ChannelUpdate.handle(newData).updated);
|
||||
}).then(newData => {
|
||||
const clone = this._clone();
|
||||
clone._patch(newData);
|
||||
return clone;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,22 +2,18 @@ const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
||||
const Role = require('./Role');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Collection = require('../util/Collection');
|
||||
const Base = require('./Base');
|
||||
const { Presence } = require('./Presence');
|
||||
const { Error, TypeError } = require('../errors');
|
||||
|
||||
/**
|
||||
* Represents a member of a guild on Discord.
|
||||
* @implements {TextBasedChannel}
|
||||
* @extends {Base}
|
||||
*/
|
||||
class GuildMember {
|
||||
class GuildMember extends Base {
|
||||
constructor(guild, data) {
|
||||
/**
|
||||
* The client that instantiated this GuildMember
|
||||
* @name GuildMember#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: guild.client });
|
||||
super(guild.client);
|
||||
|
||||
/**
|
||||
* The guild that this member is part of
|
||||
@@ -32,7 +28,8 @@ class GuildMember {
|
||||
this.user = {};
|
||||
|
||||
this._roles = [];
|
||||
if (data) this.setup(data);
|
||||
|
||||
if (data) this._patch(data);
|
||||
|
||||
/**
|
||||
* The ID of the last message sent by the member in their guild, if one was sent
|
||||
@@ -47,65 +44,72 @@ class GuildMember {
|
||||
this.lastMessage = null;
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
/**
|
||||
* Whether this member is deafened server-wide
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.serverDeaf = data.deaf;
|
||||
|
||||
/**
|
||||
* Whether this member is muted server-wide
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.serverMute = data.mute;
|
||||
|
||||
/**
|
||||
* Whether this member is self-muted
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.selfMute = data.self_mute;
|
||||
|
||||
/**
|
||||
* Whether this member is self-deafened
|
||||
* @type {boolean}
|
||||
*/
|
||||
this.selfDeaf = data.self_deaf;
|
||||
|
||||
/**
|
||||
* The voice session ID of this member, if any
|
||||
* @type {?Snowflake}
|
||||
*/
|
||||
this.voiceSessionID = data.session_id;
|
||||
|
||||
/**
|
||||
* The voice channel ID of this member, if any
|
||||
* @type {?Snowflake}
|
||||
*/
|
||||
this.voiceChannelID = data.channel_id;
|
||||
|
||||
_patch(data) {
|
||||
/**
|
||||
* Whether this member is speaking
|
||||
* @type {boolean}
|
||||
* @name GuildMember#speaking
|
||||
*/
|
||||
this.speaking = false;
|
||||
if (typeof this.speaking === 'undefined') this.speaking = false;
|
||||
|
||||
/**
|
||||
* The nickname of this guild member, if they have one
|
||||
* @type {?string}
|
||||
* @name GuildMember#nickname
|
||||
*/
|
||||
this.nickname = data.nick || null;
|
||||
if (typeof data.nick !== 'undefined') this.nickname = data.nick;
|
||||
|
||||
/**
|
||||
* The timestamp the member joined the guild at
|
||||
* @type {number}
|
||||
* @name GuildMember#joinedTimestamp
|
||||
*/
|
||||
this.joinedTimestamp = new Date(data.joined_at).getTime();
|
||||
if (typeof data.joined_at !== 'undefined') this.joinedTimestamp = new Date(data.joined_at).getTime();
|
||||
|
||||
this.user = data.user;
|
||||
this._roles = data.roles;
|
||||
this.user = this.guild.client.users.create(data.user);
|
||||
if (data.roles) this._roles = data.roles;
|
||||
}
|
||||
|
||||
get voiceState() {
|
||||
return this._frozenVoiceState || this.guild.voiceStates.get(this.id) || {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this member is deafened server-wide
|
||||
* @type {boolean}
|
||||
*/
|
||||
get serverDeaf() { return this.voiceState.deaf; }
|
||||
|
||||
/**
|
||||
* Whether this member is muted server-wide
|
||||
* @type {boolean}
|
||||
*/
|
||||
get serverMute() { return this.voiceState.mute; }
|
||||
|
||||
/**
|
||||
* Whether this member is self-muted
|
||||
* @type {boolean}
|
||||
*/
|
||||
get selfMute() { return this.voiceState.self_mute; }
|
||||
|
||||
/**
|
||||
* Whether this member is self-deafened
|
||||
* @type {boolean}
|
||||
*/
|
||||
get selfDeaf() { return this.voiceState.self_deaf; }
|
||||
|
||||
/**
|
||||
* The voice session ID of this member (if any)
|
||||
* @type {?Snowflake}
|
||||
*/
|
||||
get voiceSessionID() { return this.voiceState.session_id; }
|
||||
|
||||
/**
|
||||
* The voice channel ID of this member, (if any)
|
||||
* @type {?Snowflake}
|
||||
*/
|
||||
get voiceChannelID() { return this.voiceState.channel_id; }
|
||||
|
||||
/**
|
||||
* The time the member joined the guild
|
||||
* @type {Date}
|
||||
@@ -350,7 +354,11 @@ class GuildMember {
|
||||
} else {
|
||||
endpoint = endpoint.members(this.id);
|
||||
}
|
||||
return endpoint.patch({ data, reason }).then(newData => this.guild._updateMember(this, newData).mem);
|
||||
return endpoint.patch({ data, reason }).then(newData => {
|
||||
const clone = this._clone();
|
||||
clone._patch(newData);
|
||||
return clone;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,26 +1,20 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const Base = require('./Base');
|
||||
|
||||
/**
|
||||
* Represents an invitation to a guild channel.
|
||||
* <warn>The only guaranteed properties are `code`, `guild` and `channel`. Other properties can be missing.</warn>
|
||||
* @extends {Base}
|
||||
*/
|
||||
class Invite {
|
||||
class Invite extends Base {
|
||||
constructor(client, data) {
|
||||
/**
|
||||
* The client that instantiated the invite
|
||||
* @name Invite#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
|
||||
this.setup(data);
|
||||
super(client);
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
const Guild = require('./Guild');
|
||||
const Channel = require('./Channel');
|
||||
|
||||
/**
|
||||
* The guild the invite is for
|
||||
* @type {Guild}
|
||||
@@ -86,7 +80,7 @@ class Invite {
|
||||
* The user who created this invite
|
||||
* @type {User}
|
||||
*/
|
||||
this.inviter = this.client.dataManager.newUser(data.inviter);
|
||||
this.inviter = this.client.users.create(data.inviter);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -6,23 +6,20 @@ const ReactionCollector = require('./ReactionCollector');
|
||||
const ClientApplication = require('./ClientApplication');
|
||||
const Util = require('../util/Util');
|
||||
const Collection = require('../util/Collection');
|
||||
const ReactionStore = require('../stores/ReactionStore');
|
||||
const Constants = require('../util/Constants');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Base = require('./Base');
|
||||
const { Error, TypeError } = require('../errors');
|
||||
let GuildMember;
|
||||
|
||||
/**
|
||||
* Represents a message on Discord.
|
||||
* @extends {Base}
|
||||
*/
|
||||
class Message {
|
||||
class Message extends Base {
|
||||
constructor(channel, data, client) {
|
||||
/**
|
||||
* The client that instantiated the Message
|
||||
* @name Message#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
super(client);
|
||||
|
||||
/**
|
||||
* The channel that the message was sent in
|
||||
@@ -30,10 +27,10 @@ class Message {
|
||||
*/
|
||||
this.channel = channel;
|
||||
|
||||
if (data) this.setup(data);
|
||||
if (data) this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) { // eslint-disable-line complexity
|
||||
_patch(data) { // eslint-disable-line complexity
|
||||
/**
|
||||
* The ID of the message
|
||||
* @type {Snowflake}
|
||||
@@ -56,7 +53,7 @@ class Message {
|
||||
* The author of the message
|
||||
* @type {User}
|
||||
*/
|
||||
this.author = this.client.dataManager.newUser(data.author);
|
||||
this.author = this.client.users.create(data.author);
|
||||
|
||||
/**
|
||||
* Represents the author of the message as a guild member
|
||||
@@ -116,9 +113,9 @@ class Message {
|
||||
|
||||
/**
|
||||
* A collection of reactions to this message, mapped by the reaction ID
|
||||
* @type {Collection<Snowflake, MessageReaction>}
|
||||
* @type {ReactionStore<Snowflake, MessageReaction>}
|
||||
*/
|
||||
this.reactions = new Collection();
|
||||
this.reactions = new ReactionStore(this);
|
||||
if (data.reactions && data.reactions.length > 0) {
|
||||
for (const reaction of data.reactions) {
|
||||
const id = reaction.emoji.id ? `${reaction.emoji.name}:${reaction.emoji.id}` : reaction.emoji.name;
|
||||
@@ -173,7 +170,7 @@ class Message {
|
||||
* @private
|
||||
*/
|
||||
patch(data) {
|
||||
const clone = Util.cloneObject(this);
|
||||
const clone = this._clone();
|
||||
this._edits.unshift(clone);
|
||||
|
||||
this.editedTimestamp = new Date(data.edited_timestamp).getTime();
|
||||
@@ -395,7 +392,11 @@ class Message {
|
||||
|
||||
return this.client.api.channels[this.channel.id].messages[this.id]
|
||||
.patch({ data: { content, embed } })
|
||||
.then(data => this.client.actions.MessageUpdate.handle(data).updated);
|
||||
.then(data => {
|
||||
const clone = this._clone();
|
||||
clone._patch(data);
|
||||
return clone;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -427,7 +428,12 @@ class Message {
|
||||
|
||||
return this.client.api.channels(this.channel.id).messages(this.id).reactions(emoji, '@me')
|
||||
.put()
|
||||
.then(() => this._addReaction(Util.parseEmoji(emoji), this.client.user));
|
||||
.then(() => this.client.actions.MessageReactionAdd.handle({
|
||||
user: this.client.user,
|
||||
channel: this.channel,
|
||||
message: this,
|
||||
emoji: Util.parseEmoji(emoji),
|
||||
}).reaction);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -553,42 +559,6 @@ class Message {
|
||||
toString() {
|
||||
return this.content;
|
||||
}
|
||||
|
||||
_addReaction(emoji, user) {
|
||||
const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : encodeURIComponent(emoji.name);
|
||||
let reaction;
|
||||
if (this.reactions.has(emojiID)) {
|
||||
reaction = this.reactions.get(emojiID);
|
||||
if (!reaction.me) reaction.me = user.id === this.client.user.id;
|
||||
} else {
|
||||
reaction = new MessageReaction(this, emoji, 0, user.id === this.client.user.id);
|
||||
this.reactions.set(emojiID, reaction);
|
||||
}
|
||||
if (!reaction.users.has(user.id)) {
|
||||
reaction.users.set(user.id, user);
|
||||
reaction.count++;
|
||||
}
|
||||
return reaction;
|
||||
}
|
||||
|
||||
_removeReaction(emoji, user) {
|
||||
const emojiID = emoji.id ? `${emoji.name}:${emoji.id}` : encodeURIComponent(emoji.name);
|
||||
if (this.reactions.has(emojiID)) {
|
||||
const reaction = this.reactions.get(emojiID);
|
||||
if (reaction.users.has(user.id)) {
|
||||
reaction.users.delete(user.id);
|
||||
reaction.count--;
|
||||
if (user.id === this.client.user.id) reaction.me = false;
|
||||
if (reaction.count <= 0) this.reactions.delete(emojiID);
|
||||
return reaction;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
_clearReactions() {
|
||||
this.reactions.clear();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Message;
|
||||
|
||||
@@ -22,8 +22,7 @@ class MessageMentions {
|
||||
} else {
|
||||
this.users = new Collection();
|
||||
for (const mention of users) {
|
||||
let user = message.client.users.get(mention.id);
|
||||
if (!user) user = message.client.dataManager.newUser(mention);
|
||||
let user = message.client.users.create(mention);
|
||||
this.users.set(user.id, user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,13 +90,32 @@ class MessageReaction {
|
||||
.then(users => {
|
||||
this.users = new Collection();
|
||||
for (const rawUser of users) {
|
||||
const user = message.client.dataManager.newUser(rawUser);
|
||||
const user = message.client.users.create(rawUser);
|
||||
this.users.set(user.id, user);
|
||||
}
|
||||
this.count = this.users.size;
|
||||
return this.users;
|
||||
});
|
||||
}
|
||||
|
||||
_add(user) {
|
||||
if (!this.users.has(user.id)) {
|
||||
this.users.set(user.id, user);
|
||||
this.count++;
|
||||
}
|
||||
if (!this.me) this.me = user.id === this.message.client.user.id;
|
||||
}
|
||||
|
||||
_remove(user) {
|
||||
if (this.users.has(user.id)) {
|
||||
this.users.delete(user.id);
|
||||
this.count--;
|
||||
if (user.id === this.message.client.user.id) this.me = false;
|
||||
if (this.count <= 0) {
|
||||
this.message.reactions.remove(this.emoji.id || this.emoji.name);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageReaction;
|
||||
|
||||
@@ -13,10 +13,10 @@ class PermissionOverwrites {
|
||||
*/
|
||||
Object.defineProperty(this, 'channel', { value: guildChannel });
|
||||
|
||||
if (data) this.setup(data);
|
||||
if (data) this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* The ID of this overwrite, either a user ID or a role ID
|
||||
* @type {Snowflake}
|
||||
|
||||
@@ -1,19 +1,15 @@
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Util = require('../util/Util');
|
||||
const Base = require('./Base');
|
||||
|
||||
/**
|
||||
* Represents a role on Discord.
|
||||
* @extends {Base}
|
||||
*/
|
||||
class Role {
|
||||
class Role extends Base {
|
||||
constructor(guild, data) {
|
||||
/**
|
||||
* The client that instantiated the role
|
||||
* @name Role#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: guild.client });
|
||||
super(guild.client);
|
||||
|
||||
/**
|
||||
* The guild that the role belongs to
|
||||
@@ -21,10 +17,10 @@ class Role {
|
||||
*/
|
||||
this.guild = guild;
|
||||
|
||||
if (data) this.setup(data);
|
||||
if (data) this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* The ID of the role (unique to the guild it is part of)
|
||||
* @type {Snowflake}
|
||||
@@ -215,7 +211,11 @@ class Role {
|
||||
},
|
||||
reason,
|
||||
})
|
||||
.then(role => this.client.actions.GuildRoleUpdate.handle({ role, guild_id: this.guild.id }).updated);
|
||||
.then(role => {
|
||||
const clone = this._clone();
|
||||
clone._patch(role);
|
||||
return clone;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,9 +320,10 @@ class Role {
|
||||
*/
|
||||
delete(reason) {
|
||||
return this.client.api.guilds[this.guild.id].roles[this.id].delete({ reason })
|
||||
.then(() =>
|
||||
this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: this.id }).role
|
||||
);
|
||||
.then(() => {
|
||||
this.client.actions.GuildRoleDelete.handle({ guild_id: this.guild.id, role_id: this.id });
|
||||
return this;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,6 +2,7 @@ const GuildChannel = require('./GuildChannel');
|
||||
const Webhook = require('./Webhook');
|
||||
const TextBasedChannel = require('./interfaces/TextBasedChannel');
|
||||
const Collection = require('../util/Collection');
|
||||
const MessageStore = require('../stores/MessageStore');
|
||||
|
||||
/**
|
||||
* Represents a guild text channel on Discord.
|
||||
@@ -12,12 +13,12 @@ class TextChannel extends GuildChannel {
|
||||
constructor(guild, data) {
|
||||
super(guild, data);
|
||||
this.type = 'text';
|
||||
this.messages = new Collection();
|
||||
this.messages = new MessageStore(this);
|
||||
this._typing = new Map();
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
_patch(data) {
|
||||
super._patch(data);
|
||||
|
||||
/**
|
||||
* The topic of the text channel
|
||||
|
||||
@@ -3,26 +3,21 @@ const Constants = require('../util/Constants');
|
||||
const { Presence } = require('./Presence');
|
||||
const UserProfile = require('./UserProfile');
|
||||
const Snowflake = require('../util/Snowflake');
|
||||
const Base = require('./Base');
|
||||
const { Error } = require('../errors');
|
||||
|
||||
/**
|
||||
* Represents a user on Discord.
|
||||
* @implements {TextBasedChannel}
|
||||
* @extends {Base}
|
||||
*/
|
||||
class User {
|
||||
class User extends Base {
|
||||
constructor(client, data) {
|
||||
/**
|
||||
* The client that created the instance of the user
|
||||
* @name User#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
|
||||
if (data) this.setup(data);
|
||||
super(client);
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* The ID of the user
|
||||
* @type {Snowflake}
|
||||
@@ -32,26 +27,30 @@ class User {
|
||||
/**
|
||||
* The username of the user
|
||||
* @type {string}
|
||||
* @name User#username
|
||||
*/
|
||||
this.username = data.username;
|
||||
if (data.username) this.username = data.username;
|
||||
|
||||
/**
|
||||
* A discriminator based on username for the user
|
||||
* @type {string}
|
||||
* @name User#discriminator
|
||||
*/
|
||||
this.discriminator = data.discriminator;
|
||||
if (data.discriminator) this.discriminator = data.discriminator;
|
||||
|
||||
/**
|
||||
* The ID of the user's avatar
|
||||
* @type {string}
|
||||
* @name User#avatar
|
||||
*/
|
||||
this.avatar = data.avatar;
|
||||
if (data.avatar) this.avatar = data.avatar;
|
||||
|
||||
/**
|
||||
* Whether or not the user is a bot
|
||||
* @type {boolean}
|
||||
* @name User#bot
|
||||
*/
|
||||
this.bot = Boolean(data.bot);
|
||||
if (typeof this.bot === 'undefined' && typeof data.bot !== 'undefined') this.bot = Boolean(data.bot);
|
||||
|
||||
/**
|
||||
* The ID of the last message sent by the user, if one was sent
|
||||
@@ -64,12 +63,7 @@ class User {
|
||||
* @type {?Message}
|
||||
*/
|
||||
this.lastMessage = null;
|
||||
}
|
||||
|
||||
patch(data) {
|
||||
for (const prop of ['id', 'username', 'discriminator', 'avatar', 'bot']) {
|
||||
if (typeof data[prop] !== 'undefined') this[prop] = data[prop];
|
||||
}
|
||||
if (data.token) this.client.token = data.token;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,10 +9,10 @@ class UserConnection {
|
||||
*/
|
||||
this.user = user;
|
||||
|
||||
this.setup(data);
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* The type of the connection
|
||||
* @type {string}
|
||||
|
||||
@@ -1,26 +1,22 @@
|
||||
const Collection = require('../util/Collection');
|
||||
const { UserFlags } = require('../util/Constants');
|
||||
const UserConnection = require('./UserConnection');
|
||||
const Base = require('./Base');
|
||||
|
||||
/**
|
||||
* Represents a user's profile on Discord.
|
||||
* @extends {Base}
|
||||
*/
|
||||
class UserProfile {
|
||||
class UserProfile extends Base {
|
||||
constructor(user, data) {
|
||||
super(user.client);
|
||||
|
||||
/**
|
||||
* The owner of the profile
|
||||
* @type {User}
|
||||
*/
|
||||
this.user = user;
|
||||
|
||||
/**
|
||||
* The client that created the instance of the UserProfile
|
||||
* @name UserProfile#client
|
||||
* @type {Client}
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: user.client });
|
||||
|
||||
/**
|
||||
* The guilds that the client user and the user share
|
||||
* @type {Collection<Snowflake, Guild>}
|
||||
@@ -33,10 +29,10 @@ class UserProfile {
|
||||
*/
|
||||
this.connections = new Collection();
|
||||
|
||||
this.setup(data);
|
||||
this._patch(data);
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* If the user has Discord Premium
|
||||
* @type {boolean}
|
||||
|
||||
@@ -17,9 +17,7 @@ class VoiceChannel extends GuildChannel {
|
||||
Object.defineProperty(this, 'members', { value: new Collection() });
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
super.setup(data);
|
||||
|
||||
_patch(data) {
|
||||
/**
|
||||
* The bitrate of this voice channel
|
||||
* @type {number}
|
||||
|
||||
@@ -17,7 +17,7 @@ class Webhook {
|
||||
* @readonly
|
||||
*/
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
if (dataOrID) this.setup(dataOrID);
|
||||
if (dataOrID) this._patch(dataOrID);
|
||||
} else {
|
||||
this.id = dataOrID;
|
||||
this.token = token;
|
||||
@@ -25,7 +25,7 @@ class Webhook {
|
||||
}
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
_patch(data) {
|
||||
/**
|
||||
* The name of the webhook
|
||||
* @type {string}
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
const path = require('path');
|
||||
const MessageCollector = require('../MessageCollector');
|
||||
const Shared = require('../shared');
|
||||
const Collection = require('../../util/Collection');
|
||||
const MessageStore = require('../../stores/MessageStore');
|
||||
const Snowflake = require('../../util/Snowflake');
|
||||
const Collection = require('../../util/Collection');
|
||||
const Attachment = require('../../structures/Attachment');
|
||||
const MessageEmbed = require('../../structures/MessageEmbed');
|
||||
const { Error, RangeError, TypeError } = require('../../errors');
|
||||
const { RangeError, TypeError } = require('../../errors');
|
||||
|
||||
/**
|
||||
* Interface for classes that have text-channel-like features.
|
||||
@@ -15,9 +16,9 @@ class TextBasedChannel {
|
||||
constructor() {
|
||||
/**
|
||||
* A collection containing the messages sent to this channel
|
||||
* @type {Collection<Snowflake, Message>}
|
||||
* @type {MessageStore<Snowflake, Message>}
|
||||
*/
|
||||
this.messages = new Collection();
|
||||
this.messages = new MessageStore(this);
|
||||
|
||||
/**
|
||||
* The ID of the last message in the channel, if one was sent
|
||||
@@ -135,87 +136,6 @@ class TextBasedChannel {
|
||||
return Shared.sendMessage(this, options);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a single message from this channel, regardless of it being cached or not. Since the single message fetching
|
||||
* endpoint is reserved for bot accounts, this abstracts the `fetchMessages` method to obtain the single message when
|
||||
* using a user account.
|
||||
* @param {Snowflake} messageID ID of the message to get
|
||||
* @returns {Promise<Message>}
|
||||
* @example
|
||||
* // Get message
|
||||
* channel.fetchMessage('99539446449315840')
|
||||
* .then(message => console.log(message.content))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
fetchMessage(messageID) {
|
||||
const Message = require('../Message');
|
||||
if (!this.client.user.bot) {
|
||||
return this.fetchMessages({ limit: 1, around: messageID })
|
||||
.then(messages => {
|
||||
const msg = messages.get(messageID);
|
||||
if (!msg) throw new Error('MESSAGE_MISSING');
|
||||
return msg;
|
||||
});
|
||||
}
|
||||
return this.client.api.channels[this.id].messages[messageID].get()
|
||||
.then(data => {
|
||||
const msg = data instanceof Message ? data : new Message(this, data, this.client);
|
||||
this._cacheMessage(msg);
|
||||
return msg;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* The parameters to pass in when requesting previous messages from a channel. `around`, `before` and
|
||||
* `after` are mutually exclusive. All the parameters are optional.
|
||||
* @typedef {Object} ChannelLogsQueryOptions
|
||||
* @property {number} [limit=50] Number of messages to acquire
|
||||
* @property {Snowflake} [before] ID of a message to get the messages that were posted before it
|
||||
* @property {Snowflake} [after] ID of a message to get the messages that were posted after it
|
||||
* @property {Snowflake} [around] ID of a message to get the messages that were posted around it
|
||||
*/
|
||||
|
||||
/**
|
||||
* Gets the past messages sent in this channel. Resolves with a collection mapping message ID's to Message objects.
|
||||
* @param {ChannelLogsQueryOptions} [options={}] Query parameters to pass in
|
||||
* @returns {Promise<Collection<Snowflake, Message>>}
|
||||
* @example
|
||||
* // Get messages
|
||||
* channel.fetchMessages({limit: 10})
|
||||
* .then(messages => console.log(`Received ${messages.size} messages`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
fetchMessages(options = {}) {
|
||||
const Message = require('../Message');
|
||||
return this.client.api.channels[this.id].messages.get({ query: 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);
|
||||
}
|
||||
return messages;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the pinned messages of this channel and returns a collection of them.
|
||||
* @returns {Promise<Collection<Snowflake, Message>>}
|
||||
*/
|
||||
fetchPinnedMessages() {
|
||||
const Message = require('../Message');
|
||||
return this.client.api.channels[this.id].pins.get().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);
|
||||
}
|
||||
return messages;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a search within the channel.
|
||||
* <warn>This is only available when using a user account.</warn>
|
||||
@@ -394,29 +314,17 @@ class TextBasedChannel {
|
||||
});
|
||||
}
|
||||
|
||||
_cacheMessage(message) {
|
||||
const maxSize = this.client.options.messageCacheMaxSize;
|
||||
if (maxSize === 0) return null;
|
||||
if (this.messages.size >= maxSize && maxSize > 0) this.messages.delete(this.messages.firstKey());
|
||||
this.messages.set(message.id, message);
|
||||
return message;
|
||||
}
|
||||
|
||||
static applyToClass(structure, full = false, ignore = []) {
|
||||
const props = ['send'];
|
||||
if (full) {
|
||||
props.push(
|
||||
'_cacheMessage',
|
||||
'acknowledge',
|
||||
'fetchMessages',
|
||||
'fetchMessage',
|
||||
'search',
|
||||
'bulkDelete',
|
||||
'startTyping',
|
||||
'stopTyping',
|
||||
'typing',
|
||||
'typingCount',
|
||||
'fetchPinnedMessages',
|
||||
'createMessageCollector',
|
||||
'awaitMessages'
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user