mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +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:
3
jsdoc.json
Normal file
3
jsdoc.json
Normal file
@@ -0,0 +1,3 @@
|
||||
{
|
||||
"plugins": ["node_modules/jsdoc-strip-async-await"]
|
||||
}
|
||||
@@ -6,8 +6,8 @@
|
||||
"types": "./typings/index.d.ts",
|
||||
"scripts": {
|
||||
"test": "npm run lint && npm run docs:test",
|
||||
"docs": "docgen --source src --custom docs/index.yml --output docs/docs.json",
|
||||
"docs:test": "docgen --source src --custom docs/index.yml",
|
||||
"docs": "docgen --source src --custom docs/index.yml --output docs/docs.json --jsdoc jsdoc.json",
|
||||
"docs:test": "docgen --source src --custom docs/index.yml --jsdoc jsdoc.json",
|
||||
"lint": "eslint src",
|
||||
"lint:fix": "eslint --fix src",
|
||||
"webpack": "parallel-webpack"
|
||||
@@ -51,6 +51,7 @@
|
||||
"@types/node": "^8.0.0",
|
||||
"discord.js-docgen": "hydrabolt/discord.js-docgen",
|
||||
"eslint": "^4.0.0",
|
||||
"jsdoc-strip-async-await": "^0.1.0",
|
||||
"parallel-webpack": "^2.0.0",
|
||||
"uglifyjs-webpack-plugin": "^1.0.0-beta.1",
|
||||
"webpack": "^3.0.0"
|
||||
|
||||
@@ -3,7 +3,6 @@ const Constants = require('../util/Constants');
|
||||
const Permissions = require('../util/Permissions');
|
||||
const Util = require('../util/Util');
|
||||
const RESTManager = require('./rest/RESTManager');
|
||||
const ClientDataManager = require('./ClientDataManager');
|
||||
const ClientManager = require('./ClientManager');
|
||||
const ClientDataResolver = require('./ClientDataResolver');
|
||||
const ClientVoiceManager = require('./voice/ClientVoiceManager');
|
||||
@@ -13,11 +12,13 @@ const Collection = require('../util/Collection');
|
||||
const { Presence } = require('../structures/Presence');
|
||||
const VoiceRegion = require('../structures/VoiceRegion');
|
||||
const Webhook = require('../structures/Webhook');
|
||||
const User = require('../structures/User');
|
||||
const Invite = require('../structures/Invite');
|
||||
const ClientApplication = require('../structures/ClientApplication');
|
||||
const ShardClientUtil = require('../sharding/ShardClientUtil');
|
||||
const VoiceBroadcast = require('./voice/VoiceBroadcast');
|
||||
const UserStore = require('../stores/UserStore');
|
||||
const ChannelStore = require('../stores/ChannelStore');
|
||||
const GuildStore = require('../stores/GuildStore');
|
||||
const { Error, TypeError, RangeError } = require('../errors');
|
||||
|
||||
/**
|
||||
@@ -49,13 +50,6 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
this.rest = new RESTManager(this);
|
||||
|
||||
/**
|
||||
* The data manager of the client
|
||||
* @type {ClientDataManager}
|
||||
* @private
|
||||
*/
|
||||
this.dataManager = new ClientDataManager(this);
|
||||
|
||||
/**
|
||||
* The manager of the client
|
||||
* @type {ClientManager}
|
||||
@@ -100,23 +94,23 @@ class Client extends EventEmitter {
|
||||
|
||||
/**
|
||||
* All of the {@link User} objects that have been cached at any point, mapped by their IDs
|
||||
* @type {Collection<Snowflake, User>}
|
||||
* @type {UserStore<Snowflake, User>}
|
||||
*/
|
||||
this.users = new Collection();
|
||||
this.users = new UserStore(this);
|
||||
|
||||
/**
|
||||
* All of the guilds the client is currently handling, mapped by their IDs -
|
||||
* as long as sharding isn't being used, this will be *every* guild the bot is a member of
|
||||
* @type {Collection<Snowflake, Guild>}
|
||||
* @type {GuildStore<Snowflake, Guild>}
|
||||
*/
|
||||
this.guilds = new Collection();
|
||||
this.guilds = new GuildStore(this);
|
||||
|
||||
/**
|
||||
* All of the {@link Channel}s that the client is currently handling, mapped by their IDs -
|
||||
* as long as sharding isn't being used, this will be *every* channel in *every* guild, and all DM channels
|
||||
* @type {Collection<Snowflake, Channel>}
|
||||
* @type {ChannelStore<Snowflake, Channel>}
|
||||
*/
|
||||
this.channels = new Collection();
|
||||
this.channels = new ChannelStore(this);
|
||||
|
||||
/**
|
||||
* Presences that have been received for the client user's friends, mapped by user IDs
|
||||
@@ -322,20 +316,6 @@ class Client extends EventEmitter {
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains a user from Discord, or the user cache if it's already available.
|
||||
* <warn>This is only available when using a bot account.</warn>
|
||||
* @param {Snowflake} id ID of the user
|
||||
* @param {boolean} [cache=true] Whether to cache the new user object if it isn't already
|
||||
* @returns {Promise<User>}
|
||||
*/
|
||||
fetchUser(id, cache = true) {
|
||||
if (this.users.has(id)) return Promise.resolve(this.users.get(id));
|
||||
return this.api.users(id).get().then(data =>
|
||||
cache ? this.dataManager.newUser(data) : new User(this, data)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains an invite from Discord.
|
||||
* @param {InviteResolvable} invite Invite code or URL
|
||||
|
||||
@@ -1,112 +0,0 @@
|
||||
const Constants = require('../util/Constants');
|
||||
const Util = require('../util/Util');
|
||||
const Guild = require('../structures/Guild');
|
||||
const User = require('../structures/User');
|
||||
const Channel = require('../structures/Channel');
|
||||
const GuildChannel = require('../structures/GuildChannel');
|
||||
const Emoji = require('../structures/Emoji');
|
||||
|
||||
class ClientDataManager {
|
||||
constructor(client) {
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
get pastReady() {
|
||||
return this.client.ws.connection.status === Constants.Status.READY;
|
||||
}
|
||||
|
||||
newGuild(data) {
|
||||
const already = this.client.guilds.has(data.id);
|
||||
const guild = new Guild(this.client, data);
|
||||
this.client.guilds.set(guild.id, guild);
|
||||
if (!this.client.user.bot && this.client.options.sync) this.client.syncGuilds([guild]);
|
||||
if (this.pastReady && !already) {
|
||||
/**
|
||||
* Emitted whenever the client joins a guild.
|
||||
* @event Client#guildCreate
|
||||
* @param {Guild} guild The created guild
|
||||
*/
|
||||
if (this.client.options.fetchAllMembers) {
|
||||
guild.fetchMembers().then(() => { this.client.emit(Constants.Events.GUILD_CREATE, guild); });
|
||||
} else {
|
||||
this.client.emit(Constants.Events.GUILD_CREATE, guild);
|
||||
}
|
||||
}
|
||||
|
||||
return guild;
|
||||
}
|
||||
|
||||
newUser(data) {
|
||||
if (this.client.users.has(data.id)) return this.client.users.get(data.id);
|
||||
const user = new User(this.client, data);
|
||||
this.client.users.set(user.id, user);
|
||||
return user;
|
||||
}
|
||||
|
||||
newChannel(data, guild) {
|
||||
const already = this.client.channels.has(data.id);
|
||||
const channel = Channel.create(this.client, data, guild);
|
||||
|
||||
if (channel) {
|
||||
if (this.pastReady && !already) this.client.emit(Constants.Events.CHANNEL_CREATE, channel);
|
||||
this.client.channels.set(channel.id, channel);
|
||||
return channel;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
newEmoji(data, guild) {
|
||||
const already = guild.emojis.has(data.id);
|
||||
if (data && !already) {
|
||||
let emoji = new Emoji(guild, data);
|
||||
this.client.emit(Constants.Events.GUILD_EMOJI_CREATE, emoji);
|
||||
guild.emojis.set(emoji.id, emoji);
|
||||
return emoji;
|
||||
} else if (already) {
|
||||
return guild.emojis.get(data.id);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
killEmoji(emoji) {
|
||||
if (!(emoji instanceof Emoji && emoji.guild)) return;
|
||||
this.client.emit(Constants.Events.GUILD_EMOJI_DELETE, emoji);
|
||||
emoji.guild.emojis.delete(emoji.id);
|
||||
}
|
||||
|
||||
killGuild(guild) {
|
||||
const already = this.client.guilds.has(guild.id);
|
||||
this.client.guilds.delete(guild.id);
|
||||
if (already && this.pastReady) this.client.emit(Constants.Events.GUILD_DELETE, guild);
|
||||
}
|
||||
|
||||
killUser(user) {
|
||||
this.client.users.delete(user.id);
|
||||
}
|
||||
|
||||
killChannel(channel) {
|
||||
this.client.channels.delete(channel.id);
|
||||
if (channel instanceof GuildChannel) channel.guild.channels.delete(channel.id);
|
||||
}
|
||||
|
||||
updateGuild(currentGuild, newData) {
|
||||
const oldGuild = Util.cloneObject(currentGuild);
|
||||
currentGuild.setup(newData);
|
||||
if (this.pastReady) this.client.emit(Constants.Events.GUILD_UPDATE, oldGuild, currentGuild);
|
||||
}
|
||||
|
||||
updateChannel(currentChannel, newData) {
|
||||
currentChannel.setup(newData);
|
||||
}
|
||||
|
||||
updateEmoji(currentEmoji, newData) {
|
||||
const oldEmoji = Util.cloneObject(currentEmoji);
|
||||
currentEmoji.setup(newData);
|
||||
this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, oldEmoji, currentEmoji);
|
||||
return currentEmoji;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ClientDataManager;
|
||||
@@ -1,9 +1,14 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class ChannelCreateAction extends Action {
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
const channel = client.dataManager.newChannel(data);
|
||||
const existing = client.channels.has(data.id);
|
||||
const channel = client.channels.create(data);
|
||||
if (!existing && channel) {
|
||||
client.emit(Constants.Events.CHANNEL_CREATE, channel);
|
||||
}
|
||||
return { channel };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class ChannelDeleteAction extends Action {
|
||||
constructor(client) {
|
||||
@@ -8,22 +9,21 @@ class ChannelDeleteAction extends Action {
|
||||
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
|
||||
let channel = client.channels.get(data.id);
|
||||
|
||||
if (channel) {
|
||||
client.dataManager.killChannel(channel);
|
||||
this.deleted.set(channel.id, channel);
|
||||
this.scheduleForDeletion(channel.id);
|
||||
} else {
|
||||
channel = this.deleted.get(data.id) || null;
|
||||
client.channels.remove(channel.id);
|
||||
client.emit(Constants.Events.CHANNEL_DELETE, channel);
|
||||
}
|
||||
|
||||
return { channel };
|
||||
}
|
||||
|
||||
scheduleForDeletion(id) {
|
||||
this.client.setTimeout(() => this.deleted.delete(id), this.client.options.restWsBridgeTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a channel is deleted.
|
||||
* @event Client#channelDelete
|
||||
* @param {Channel} channel The channel that was deleted
|
||||
*/
|
||||
|
||||
module.exports = ChannelDeleteAction;
|
||||
|
||||
@@ -1,6 +1,4 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Util = require('../../util/Util');
|
||||
|
||||
class ChannelUpdateAction extends Action {
|
||||
handle(data) {
|
||||
@@ -8,27 +6,15 @@ class ChannelUpdateAction extends Action {
|
||||
|
||||
const channel = client.channels.get(data.id);
|
||||
if (channel) {
|
||||
const oldChannel = Util.cloneObject(channel);
|
||||
channel.setup(data);
|
||||
client.emit(Constants.Events.CHANNEL_UPDATE, oldChannel, channel);
|
||||
const old = channel._update(data);
|
||||
return {
|
||||
old: oldChannel,
|
||||
old,
|
||||
updated: channel,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
old: null,
|
||||
updated: null,
|
||||
};
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a channel is updated - e.g. name change, topic change.
|
||||
* @event Client#channelUpdate
|
||||
* @param {Channel} oldChannel The channel before the update
|
||||
* @param {Channel} newChannel The channel after the update
|
||||
*/
|
||||
|
||||
module.exports = ChannelUpdateAction;
|
||||
|
||||
@@ -5,7 +5,7 @@ class GuildBanRemove extends Action {
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
const guild = client.guilds.get(data.guild_id);
|
||||
const user = client.dataManager.newUser(data.user);
|
||||
const user = client.users.create(data.user);
|
||||
if (guild && user) client.emit(Constants.Events.GUILD_BAN_REMOVE, guild, user);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,8 @@ class GuildDeleteAction extends Action {
|
||||
}
|
||||
|
||||
// Delete guild
|
||||
client.guilds.delete(guild.id);
|
||||
client.guilds.remove(guild.id);
|
||||
client.emit(Constants.Events.GUILD_DELETE, guild);
|
||||
this.deleted.set(guild.id, guild);
|
||||
this.scheduleForDeletion(guild.id);
|
||||
} else {
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class GuildEmojiCreateAction extends Action {
|
||||
handle(guild, createdEmoji) {
|
||||
const client = this.client;
|
||||
const emoji = client.dataManager.newEmoji(createdEmoji, guild);
|
||||
const emoji = guild.emojis.create(createdEmoji);
|
||||
this.client.emit(Constants.Events.GUILD_EMOJI_CREATE, emoji);
|
||||
return { emoji };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class GuildEmojiDeleteAction extends Action {
|
||||
handle(emoji) {
|
||||
const client = this.client;
|
||||
client.dataManager.killEmoji(emoji);
|
||||
emoji.guild.emojis.remove(emoji.id);
|
||||
this.client.emit(Constants.Events.GUILD_EMOJI_DELETE, emoji);
|
||||
return { emoji };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class GuildEmojiUpdateAction extends Action {
|
||||
handle(oldEmoji, newEmoji) {
|
||||
const emoji = this.client.dataManager.updateEmoji(oldEmoji, newEmoji);
|
||||
return { emoji };
|
||||
handle(current, data) {
|
||||
const old = current._update(data);
|
||||
this.client.emit(Constants.Events.GUILD_EMOJI_UPDATE, old, current);
|
||||
return { emoji: current };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ const Action = require('./Action');
|
||||
|
||||
class GuildMemberGetAction extends Action {
|
||||
handle(guild, data) {
|
||||
const member = guild._addMember(data, false);
|
||||
const member = guild.members.create(data);
|
||||
return { member };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,11 +2,6 @@ const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class GuildMemberRemoveAction extends Action {
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.deleted = new Map();
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
const guild = client.guilds.get(data.guild_id);
|
||||
@@ -15,20 +10,12 @@ class GuildMemberRemoveAction extends Action {
|
||||
member = guild.members.get(data.user.id);
|
||||
if (member) {
|
||||
guild.memberCount--;
|
||||
guild._removeMember(member);
|
||||
this.deleted.set(guild.id + data.user.id, member);
|
||||
guild.members.remove(member.id);
|
||||
if (client.status === Constants.Status.READY) client.emit(Constants.Events.GUILD_MEMBER_REMOVE, member);
|
||||
this.scheduleForDeletion(guild.id, data.user.id);
|
||||
} else {
|
||||
member = this.deleted.get(guild.id + data.user.id) || null;
|
||||
}
|
||||
}
|
||||
return { guild, member };
|
||||
}
|
||||
|
||||
scheduleForDeletion(guildID, userID) {
|
||||
this.client.setTimeout(() => this.deleted.delete(guildID + userID), this.client.options.restWsBridgeTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Role = require('../../structures/Role');
|
||||
|
||||
class GuildRoleCreate extends Action {
|
||||
handle(data) {
|
||||
@@ -9,8 +8,7 @@ class GuildRoleCreate extends Action {
|
||||
let role;
|
||||
if (guild) {
|
||||
const already = guild.roles.has(data.role.id);
|
||||
role = new Role(guild, data.role);
|
||||
guild.roles.set(role.id, role);
|
||||
role = guild.roles.create(data.role);
|
||||
if (!already) client.emit(Constants.Events.GUILD_ROLE_CREATE, role);
|
||||
}
|
||||
return { role };
|
||||
|
||||
@@ -2,11 +2,6 @@ const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class GuildRoleDeleteAction extends Action {
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.deleted = new Map();
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
const guild = client.guilds.get(data.guild_id);
|
||||
@@ -15,21 +10,13 @@ class GuildRoleDeleteAction extends Action {
|
||||
if (guild) {
|
||||
role = guild.roles.get(data.role_id);
|
||||
if (role) {
|
||||
guild.roles.delete(data.role_id);
|
||||
this.deleted.set(guild.id + data.role_id, role);
|
||||
this.scheduleForDeletion(guild.id, data.role_id);
|
||||
guild.roles.remove(data.role_id);
|
||||
client.emit(Constants.Events.GUILD_ROLE_DELETE, role);
|
||||
} else {
|
||||
role = this.deleted.get(guild.id + data.role_id) || null;
|
||||
}
|
||||
}
|
||||
|
||||
return { role };
|
||||
}
|
||||
|
||||
scheduleForDeletion(guildID, roleID) {
|
||||
this.client.setTimeout(() => this.deleted.delete(guildID + roleID), this.client.options.restWsBridgeTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Util = require('../../util/Util');
|
||||
|
||||
class GuildRoleUpdateAction extends Action {
|
||||
handle(data) {
|
||||
@@ -8,18 +7,16 @@ class GuildRoleUpdateAction extends Action {
|
||||
const guild = client.guilds.get(data.guild_id);
|
||||
|
||||
if (guild) {
|
||||
const roleData = data.role;
|
||||
let oldRole = null;
|
||||
let old = null;
|
||||
|
||||
const role = guild.roles.get(roleData.id);
|
||||
const role = guild.roles.get(data.role.id);
|
||||
if (role) {
|
||||
oldRole = Util.cloneObject(role);
|
||||
role.setup(data.role);
|
||||
client.emit(Constants.Events.GUILD_ROLE_UPDATE, oldRole, role);
|
||||
old = role._update(data.role);
|
||||
client.emit(Constants.Events.GUILD_ROLE_UPDATE, old, role);
|
||||
}
|
||||
|
||||
return {
|
||||
old: oldRole,
|
||||
old,
|
||||
updated: role,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -14,9 +14,9 @@ class GuildSync extends Action {
|
||||
for (const syncMember of data.members) {
|
||||
const member = guild.members.get(syncMember.user.id);
|
||||
if (member) {
|
||||
guild._updateMember(member, syncMember);
|
||||
member._patch(syncMember);
|
||||
} else {
|
||||
guild._addMember(syncMember, false);
|
||||
guild.members.create(syncMember, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Util = require('../../util/Util');
|
||||
|
||||
class GuildUpdateAction extends Action {
|
||||
handle(data) {
|
||||
@@ -8,11 +7,10 @@ class GuildUpdateAction extends Action {
|
||||
|
||||
const guild = client.guilds.get(data.id);
|
||||
if (guild) {
|
||||
const oldGuild = Util.cloneObject(guild);
|
||||
guild.setup(data);
|
||||
client.emit(Constants.Events.GUILD_UPDATE, oldGuild, guild);
|
||||
const old = guild._update(data);
|
||||
client.emit(Constants.Events.GUILD_UPDATE, old, guild);
|
||||
return {
|
||||
old: oldGuild,
|
||||
old,
|
||||
updated: guild,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,55 +1,39 @@
|
||||
const Action = require('./Action');
|
||||
const Message = require('../../structures/Message');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class MessageCreateAction extends Action {
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
|
||||
const channel = client.channels.get((data instanceof Array ? data[0] : data).channel_id);
|
||||
const user = client.users.get((data instanceof Array ? data[0] : data).author.id);
|
||||
const channel = client.channels.get(data.channel_id);
|
||||
const user = client.users.get(data.author.id);
|
||||
if (channel) {
|
||||
const existing = channel.messages.get(data.id);
|
||||
if (existing) return { message: existing };
|
||||
const member = channel.guild ? channel.guild.member(user) : null;
|
||||
if (data instanceof Array) {
|
||||
const messages = new Array(data.length);
|
||||
for (let i = 0; i < data.length; i++) {
|
||||
messages[i] = channel._cacheMessage(new Message(channel, data[i], client));
|
||||
}
|
||||
const lastMessage = messages[messages.length - 1];
|
||||
channel.lastMessageID = lastMessage.id;
|
||||
channel.lastMessage = lastMessage;
|
||||
if (user) {
|
||||
user.lastMessageID = lastMessage.id;
|
||||
user.lastMessage = lastMessage;
|
||||
}
|
||||
if (member) {
|
||||
member.lastMessageID = lastMessage.id;
|
||||
member.lastMessage = lastMessage;
|
||||
}
|
||||
return {
|
||||
messages,
|
||||
};
|
||||
} else {
|
||||
const message = channel._cacheMessage(new Message(channel, data, client));
|
||||
channel.lastMessageID = data.id;
|
||||
channel.lastMessage = message;
|
||||
if (user) {
|
||||
user.lastMessageID = data.id;
|
||||
user.lastMessage = message;
|
||||
}
|
||||
if (member) {
|
||||
member.lastMessageID = data.id;
|
||||
member.lastMessage = message;
|
||||
}
|
||||
return {
|
||||
message,
|
||||
};
|
||||
const message = channel.messages.create(data);
|
||||
channel.lastMessageID = data.id;
|
||||
channel.lastMessage = message;
|
||||
if (user) {
|
||||
user.lastMessageID = data.id;
|
||||
user.lastMessage = message;
|
||||
}
|
||||
if (member) {
|
||||
member.lastMessageID = data.id;
|
||||
member.lastMessage = message;
|
||||
}
|
||||
|
||||
client.emit(Constants.Events.MESSAGE_CREATE, message);
|
||||
return { message };
|
||||
}
|
||||
|
||||
return {
|
||||
message: null,
|
||||
};
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a message is created.
|
||||
* @event Client#message
|
||||
* @param {Message} message The created message
|
||||
*/
|
||||
|
||||
module.exports = MessageCreateAction;
|
||||
|
||||
@@ -1,11 +1,7 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class MessageDeleteAction extends Action {
|
||||
constructor(client) {
|
||||
super(client);
|
||||
this.deleted = new Map();
|
||||
}
|
||||
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
const channel = client.channels.get(data.channel_id);
|
||||
@@ -15,20 +11,18 @@ class MessageDeleteAction extends Action {
|
||||
message = channel.messages.get(data.id);
|
||||
if (message) {
|
||||
channel.messages.delete(message.id);
|
||||
this.deleted.set(channel.id + message.id, message);
|
||||
this.scheduleForDeletion(channel.id, message.id);
|
||||
} else {
|
||||
message = this.deleted.get(channel.id + data.id) || null;
|
||||
client.emit(Constants.Events.MESSAGE_DELETE, message);
|
||||
}
|
||||
}
|
||||
|
||||
return { message };
|
||||
}
|
||||
|
||||
scheduleForDeletion(channelID, messageID) {
|
||||
this.client.setTimeout(() => this.deleted.delete(channelID + messageID),
|
||||
this.client.options.restWsBridgeTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a message is deleted.
|
||||
* @event Client#messageDelete
|
||||
* @param {Message} message The deleted message
|
||||
*/
|
||||
|
||||
module.exports = MessageDeleteAction;
|
||||
|
||||
@@ -7,16 +7,25 @@ class MessageDeleteBulkAction extends Action {
|
||||
const client = this.client;
|
||||
const channel = client.channels.get(data.channel_id);
|
||||
|
||||
const ids = data.ids;
|
||||
const messages = new Collection();
|
||||
for (const id of ids) {
|
||||
const message = channel.messages.get(id);
|
||||
if (message) messages.set(message.id, message);
|
||||
}
|
||||
if (channel) {
|
||||
const ids = data.ids;
|
||||
const messages = new Collection();
|
||||
for (const id of ids) {
|
||||
const message = channel.messages.get(id);
|
||||
if (message) messages.set(message.id, message);
|
||||
}
|
||||
|
||||
if (messages.size > 0) client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages);
|
||||
return { messages };
|
||||
if (messages.size > 0) client.emit(Constants.Events.MESSAGE_BULK_DELETE, messages);
|
||||
return { messages };
|
||||
}
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever messages are deleted in bulk.
|
||||
* @event Client#messageDeleteBulk
|
||||
* @param {Collection<Snowflake, Message>} messages The deleted messages, mapped by their ID
|
||||
*/
|
||||
|
||||
module.exports = MessageDeleteBulkAction;
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
/*
|
||||
{ user_id: 'id',
|
||||
@@ -10,19 +9,22 @@ const Constants = require('../../util/Constants');
|
||||
|
||||
class MessageReactionAdd extends Action {
|
||||
handle(data) {
|
||||
const user = this.client.users.get(data.user_id);
|
||||
const user = data.user || this.client.users.get(data.user_id);
|
||||
if (!user) return false;
|
||||
// Verify channel
|
||||
const channel = this.client.channels.get(data.channel_id);
|
||||
const channel = data.channel || this.client.channels.get(data.channel_id);
|
||||
if (!channel || channel.type === 'voice') return false;
|
||||
// Verify message
|
||||
const message = channel.messages.get(data.message_id);
|
||||
const message = data.message || channel.messages.get(data.message_id);
|
||||
if (!message) return false;
|
||||
if (!data.emoji) return false;
|
||||
// Verify reaction
|
||||
const reaction = message._addReaction(data.emoji, user);
|
||||
if (reaction) this.client.emit(Constants.Events.MESSAGE_REACTION_ADD, reaction, user);
|
||||
|
||||
const reaction = message.reactions.create({
|
||||
emoji: data.emoji,
|
||||
count: 0,
|
||||
me: user.id === this.client.user.id,
|
||||
});
|
||||
reaction._add(user);
|
||||
return { message, reaction, user };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,8 +20,11 @@ class MessageReactionRemove extends Action {
|
||||
if (!message) return false;
|
||||
if (!data.emoji) return false;
|
||||
// Verify reaction
|
||||
const reaction = message._removeReaction(data.emoji, user);
|
||||
if (reaction) this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user);
|
||||
const emojiID = data.emoji.id || decodeURIComponent(data.emoji.name);
|
||||
const reaction = message.reactions.get(emojiID);
|
||||
if (!reaction) return false;
|
||||
reaction._remove(user);
|
||||
this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE, reaction, user);
|
||||
|
||||
return { message, reaction, user };
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ class MessageReactionRemoveAll extends Action {
|
||||
const message = channel.messages.get(data.message_id);
|
||||
if (!message) return false;
|
||||
|
||||
message._clearReactions();
|
||||
message.reactions.clear();
|
||||
this.client.emit(Constants.Events.MESSAGE_REACTION_REMOVE_ALL, message);
|
||||
|
||||
return { message };
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
|
||||
class MessageUpdateAction extends Action {
|
||||
handle(data) {
|
||||
@@ -10,31 +9,15 @@ class MessageUpdateAction extends Action {
|
||||
const message = channel.messages.get(data.id);
|
||||
if (message) {
|
||||
message.patch(data);
|
||||
client.emit(Constants.Events.MESSAGE_UPDATE, message._edits[0], message);
|
||||
return {
|
||||
old: message._edits[0],
|
||||
updated: message,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
old: message,
|
||||
updated: message,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
old: null,
|
||||
updated: null,
|
||||
};
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a message is updated - e.g. embed or content change.
|
||||
* @event Client#messageUpdate
|
||||
* @param {Message} oldMessage The message before the update
|
||||
* @param {Message} newMessage The message after the update
|
||||
*/
|
||||
|
||||
module.exports = MessageUpdateAction;
|
||||
|
||||
@@ -3,7 +3,7 @@ const Action = require('./Action');
|
||||
class UserGetAction extends Action {
|
||||
handle(data) {
|
||||
const client = this.client;
|
||||
const user = client.dataManager.newUser(data);
|
||||
const user = client.users.create(data);
|
||||
return { user };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
const Action = require('./Action');
|
||||
const Constants = require('../../util/Constants');
|
||||
const Util = require('../../util/Util');
|
||||
|
||||
class UserUpdateAction extends Action {
|
||||
handle(data) {
|
||||
@@ -14,8 +13,7 @@ class UserUpdateAction extends Action {
|
||||
};
|
||||
}
|
||||
|
||||
const oldUser = Util.cloneObject(client.user);
|
||||
client.user.patch(data);
|
||||
const oldUser = client.user._update(data);
|
||||
client.emit(Constants.Events.USER_UPDATE, oldUser, client.user);
|
||||
return {
|
||||
old: oldUser,
|
||||
|
||||
@@ -2,9 +2,7 @@ const AbstractHandler = require('./AbstractHandler');
|
||||
|
||||
class ChannelCreateHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
client.actions.ChannelCreate.handle(data);
|
||||
this.packetManager.client.actions.ChannelCreate.handle(packet.d);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,20 +1,9 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class ChannelDeleteHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
const response = client.actions.ChannelDelete.handle(data);
|
||||
if (response.channel) client.emit(Constants.Events.CHANNEL_DELETE, response.channel);
|
||||
this.packetManager.client.actions.ChannelDelete.handle(packet.d);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a channel is deleted.
|
||||
* @event Client#channelDelete
|
||||
* @param {Channel} channel The channel that was deleted
|
||||
*/
|
||||
|
||||
module.exports = ChannelDeleteHandler;
|
||||
|
||||
@@ -1,11 +1,20 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class ChannelUpdateHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
client.actions.ChannelUpdate.handle(data);
|
||||
const { old, updated } = this.packetManager.client.actions.ChannelUpdate.handle(packet.d);
|
||||
if (old && updated) {
|
||||
this.packetManager.client.emit(Constants.Events.CHANNEL_UPDATE, old, updated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ChannelUpdateHandler;
|
||||
|
||||
/**
|
||||
* Emitted whenever a channel is updated - e.g. name change, topic change.
|
||||
* @event Client#channelUpdate
|
||||
* @param {Channel} oldChannel The channel before the update
|
||||
* @param {Channel} newChannel The channel after the update
|
||||
*/
|
||||
|
||||
@@ -1,20 +1,31 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class GuildCreateHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
async handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
|
||||
const guild = client.guilds.get(data.id);
|
||||
let guild = client.guilds.get(data.id);
|
||||
if (guild) {
|
||||
if (!guild.available && !data.unavailable) {
|
||||
// A newly available guild
|
||||
guild.setup(data);
|
||||
guild._patch(data);
|
||||
this.packetManager.ws.checkIfReady();
|
||||
}
|
||||
} else {
|
||||
// A new guild
|
||||
client.dataManager.newGuild(data);
|
||||
guild = client.guilds.create(data);
|
||||
const emitEvent = client.ws.connection.status === Constants.Status.READY;
|
||||
if (emitEvent) {
|
||||
/**
|
||||
* Emitted whenever the client joins a guild.
|
||||
* @event Client#guildCreate
|
||||
* @param {Guild} guild The created guild
|
||||
*/
|
||||
if (client.options.fetchAllMembers) await guild.fetchMembers();
|
||||
client.emit(Constants.Events.GUILD_CREATE, guild);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,9 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class GuildDeleteHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
const response = client.actions.GuildDelete.handle(data);
|
||||
if (response.guild) client.emit(Constants.Events.GUILD_DELETE, response.guild);
|
||||
client.actions.GuildDelete.handle(packet.d);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// ##untested handler##
|
||||
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class GuildMemberAddHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
@@ -9,9 +10,18 @@ class GuildMemberAddHandler extends AbstractHandler {
|
||||
const guild = client.guilds.get(data.guild_id);
|
||||
if (guild) {
|
||||
guild.memberCount++;
|
||||
guild._addMember(data);
|
||||
const member = guild.members.create(data);
|
||||
if (client.ws.connection.status === Constants.Status.READY) {
|
||||
client.emit(Constants.Events.GUILD_MEMBER_ADD, member);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildMemberAddHandler;
|
||||
|
||||
/**
|
||||
* Emitted whenever a user joins a guild.
|
||||
* @event Client#guildMemberAdd
|
||||
* @param {GuildMember} member The member that has joined a guild
|
||||
*/
|
||||
|
||||
@@ -1,16 +1,27 @@
|
||||
// ##untested handler##
|
||||
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class GuildMemberUpdateHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
|
||||
const guild = client.guilds.get(data.guild_id);
|
||||
if (guild) {
|
||||
const member = guild.members.get(data.user.id);
|
||||
if (member) guild._updateMember(member, data);
|
||||
if (member) {
|
||||
const old = member._update(data);
|
||||
if (client.ws.connection.status === Constants.Status.READY) {
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
client.emit(Constants.Events.GUILD_MEMBER_UPDATE, old, member);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ class GuildMembersChunkHandler extends AbstractHandler {
|
||||
if (!guild) return;
|
||||
const members = new Collection();
|
||||
|
||||
for (const member of data.members) members.set(member.user.id, guild._addMember(member, false));
|
||||
for (const member of data.members) members.set(member.user.id, guild.members.create(member));
|
||||
|
||||
client.emit(Constants.Events.GUILD_MEMBERS_CHUNK, members, guild);
|
||||
|
||||
|
||||
@@ -1,19 +1,9 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class MessageCreateHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
const response = client.actions.MessageCreate.handle(data);
|
||||
if (response.message) client.emit(Constants.Events.MESSAGE_CREATE, response.message);
|
||||
this.packetManager.client.actions.MessageCreate.handle(packet.d);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a message is created.
|
||||
* @event Client#message
|
||||
* @param {Message} message The created message
|
||||
*/
|
||||
|
||||
module.exports = MessageCreateHandler;
|
||||
|
||||
@@ -10,10 +10,4 @@ class MessageDeleteHandler extends AbstractHandler {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever a message is deleted.
|
||||
* @event Client#messageDelete
|
||||
* @param {Message} message The deleted message
|
||||
*/
|
||||
|
||||
module.exports = MessageDeleteHandler;
|
||||
|
||||
@@ -2,16 +2,8 @@ const AbstractHandler = require('./AbstractHandler');
|
||||
|
||||
class MessageDeleteBulkHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
client.actions.MessageDeleteBulk.handle(data);
|
||||
this.packetManager.client.actions.MessageDeleteBulk.handle(packet.d);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Emitted whenever messages are deleted in bulk.
|
||||
* @event Client#messageDeleteBulk
|
||||
* @param {Collection<Snowflake, Message>} messages The deleted messages, mapped by their ID
|
||||
*/
|
||||
|
||||
module.exports = MessageDeleteBulkHandler;
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class MessageReactionAddHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
client.actions.MessageReactionAdd.handle(data);
|
||||
const { user, reaction } = client.actions.MessageReactionAdd.handle(data);
|
||||
if (reaction) client.emit(Constants.Events.MESSAGE_REACTION_ADD, reaction, user);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
const Constants = require('../../../../util/Constants');
|
||||
|
||||
class MessageUpdateHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
const client = this.packetManager.client;
|
||||
const data = packet.d;
|
||||
client.actions.MessageUpdate.handle(data);
|
||||
const { old, updated } = this.packetManager.client.actions.MessageUpdate.handle(packet.d);
|
||||
if (old && updated) {
|
||||
this.packetManager.client.emit(Constants.Events.MESSAGE_UPDATE, old, updated);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,14 +12,13 @@ class PresenceUpdateHandler extends AbstractHandler {
|
||||
// Step 1
|
||||
if (!user) {
|
||||
if (data.user.username) {
|
||||
user = client.dataManager.newUser(data.user);
|
||||
user = client.users.create(data.user);
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
const oldUser = Util.cloneObject(user);
|
||||
user.patch(data.user);
|
||||
const oldUser = user._update(data.user);
|
||||
if (!user.equals(oldUser)) {
|
||||
client.emit(Constants.Events.USER_UPDATE, oldUser, user);
|
||||
}
|
||||
@@ -27,12 +26,12 @@ class PresenceUpdateHandler extends AbstractHandler {
|
||||
if (guild) {
|
||||
let member = guild.members.get(user.id);
|
||||
if (!member && data.status !== 'offline') {
|
||||
member = guild._addMember({
|
||||
member = guild.members.create({
|
||||
user,
|
||||
roles: data.roles,
|
||||
deaf: false,
|
||||
mute: false,
|
||||
}, false);
|
||||
});
|
||||
client.emit(Constants.Events.GUILD_MEMBER_AVAILABLE, member);
|
||||
}
|
||||
if (member) {
|
||||
@@ -40,7 +39,7 @@ class PresenceUpdateHandler extends AbstractHandler {
|
||||
guild._setPresence(user.id, data);
|
||||
return;
|
||||
}
|
||||
const oldMember = Util.cloneObject(member);
|
||||
const oldMember = member._clone();
|
||||
if (member.presence) {
|
||||
oldMember.frozenPresence = Util.cloneObject(member.presence);
|
||||
}
|
||||
|
||||
@@ -17,11 +17,11 @@ class ReadyHandler extends AbstractHandler {
|
||||
client.readyAt = new Date();
|
||||
client.users.set(clientUser.id, clientUser);
|
||||
|
||||
for (const guild of data.guilds) client.dataManager.newGuild(guild);
|
||||
for (const privateDM of data.private_channels) client.dataManager.newChannel(privateDM);
|
||||
for (const guild of data.guilds) client.guilds.create(guild);
|
||||
for (const privateDM of data.private_channels) client.channels.create(privateDM);
|
||||
|
||||
for (const relation of data.relationships) {
|
||||
const user = client.dataManager.newUser(relation.user);
|
||||
const user = client.users.create(relation.user);
|
||||
if (relation.type === 1) {
|
||||
client.user.friends.set(user.id, user);
|
||||
} else if (relation.type === 2) {
|
||||
@@ -31,7 +31,7 @@ class ReadyHandler extends AbstractHandler {
|
||||
|
||||
data.presences = data.presences || [];
|
||||
for (const presence of data.presences) {
|
||||
client.dataManager.newUser(presence.user);
|
||||
client.users.create(presence.user);
|
||||
client._setPresence(presence.user.id, presence);
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ class ReadyHandler extends AbstractHandler {
|
||||
}
|
||||
|
||||
if (!client.users.has('1')) {
|
||||
client.dataManager.newUser({
|
||||
client.users.create({
|
||||
id: '1',
|
||||
username: 'Clyde',
|
||||
discriminator: '0000',
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const AbstractHandler = require('./AbstractHandler');
|
||||
|
||||
const Constants = require('../../../../util/Constants');
|
||||
const Util = require('../../../../util/Util');
|
||||
|
||||
class VoiceStateUpdateHandler extends AbstractHandler {
|
||||
handle(packet) {
|
||||
@@ -12,28 +11,16 @@ class VoiceStateUpdateHandler extends AbstractHandler {
|
||||
if (guild) {
|
||||
const member = guild.members.get(data.user_id);
|
||||
if (member) {
|
||||
const oldVoiceChannelMember = Util.cloneObject(member);
|
||||
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {
|
||||
member.voiceChannel.members.delete(oldVoiceChannelMember.id);
|
||||
}
|
||||
|
||||
// If the member left the voice channel, unset their speaking property
|
||||
if (!data.channel_id) member.speaking = null;
|
||||
const oldMember = member._clone();
|
||||
oldMember._frozenVoiceState = oldMember.voiceState;
|
||||
|
||||
if (member.user.id === client.user.id && data.channel_id) {
|
||||
client.emit('self.voiceStateUpdate', data);
|
||||
}
|
||||
|
||||
const newChannel = client.channels.get(data.channel_id);
|
||||
if (newChannel) newChannel.members.set(member.user.id, member);
|
||||
guild.voiceStates.set(member.user.id, data);
|
||||
|
||||
member.serverMute = data.mute;
|
||||
member.serverDeaf = data.deaf;
|
||||
member.selfMute = data.self_mute;
|
||||
member.selfDeaf = data.self_deaf;
|
||||
member.voiceSessionID = data.session_id;
|
||||
member.voiceChannelID = data.channel_id;
|
||||
client.emit(Constants.Events.VOICE_STATE_UPDATE, oldVoiceChannelMember, member);
|
||||
client.emit(Constants.Events.VOICE_STATE_UPDATE, oldMember, member);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
46
src/stores/ChannelStore.js
Normal file
46
src/stores/ChannelStore.js
Normal file
@@ -0,0 +1,46 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const DMChannel = require('../structures/DMChannel');
|
||||
const GroupDMChannel = require('../structures/GroupDMChannel');
|
||||
const Constants = require('../util/Constants');
|
||||
|
||||
/**
|
||||
* Stores channels.
|
||||
* @private
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class ChannelStore extends DataStore {
|
||||
create(data, guild, cache = true) {
|
||||
const existing = this.get(data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
let channel;
|
||||
switch (data.type) {
|
||||
case Constants.ChannelTypes.DM:
|
||||
channel = new DMChannel(this.client, data);
|
||||
break;
|
||||
case Constants.ChannelTypes.GROUP:
|
||||
channel = new GroupDMChannel(this.client, data);
|
||||
break;
|
||||
default: // eslint-disable-line no-case-declarations
|
||||
guild = guild || this.client.guilds.get(data.guild_id);
|
||||
if (!guild) {
|
||||
this.client.emit(Constants.Events.DEBUG, `Failed to find guild for channel ${data.id} ${data.type}`);
|
||||
return null;
|
||||
}
|
||||
channel = guild.channels.create(data, cache);
|
||||
break;
|
||||
}
|
||||
|
||||
if (cache) this.set(channel.id, channel);
|
||||
|
||||
return channel;
|
||||
}
|
||||
|
||||
remove(id) {
|
||||
const channel = this.get(id);
|
||||
if (channel.guild) channel.guild.channels.remove(id);
|
||||
super.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ChannelStore;
|
||||
19
src/stores/DataStore.js
Normal file
19
src/stores/DataStore.js
Normal file
@@ -0,0 +1,19 @@
|
||||
const Collection = require('../util/Collection');
|
||||
|
||||
/**
|
||||
* Manages the creation, retrieval and deletion of a specific data model.
|
||||
* @extends {Collection}
|
||||
*/
|
||||
class DataStore extends Collection {
|
||||
constructor(client, iterable) {
|
||||
super();
|
||||
if (iterable) for (const item of iterable) this.create(item);
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
}
|
||||
|
||||
// Stubs
|
||||
create() { return undefined; }
|
||||
remove(key) { return this.delete(key); }
|
||||
}
|
||||
|
||||
module.exports = DataStore;
|
||||
27
src/stores/EmojiStore.js
Normal file
27
src/stores/EmojiStore.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const Emoji = require('../structures/Emoji');
|
||||
/**
|
||||
* Stores emojis.
|
||||
* @private
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class EmojiStore extends DataStore {
|
||||
constructor(guild, iterable) {
|
||||
super(guild.client, iterable);
|
||||
this.guild = guild;
|
||||
}
|
||||
|
||||
create(data) {
|
||||
const guild = this.guild;
|
||||
|
||||
const existing = guild.emojis.get(data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const emoji = new Emoji(guild, data);
|
||||
guild.emojis.set(emoji.id, emoji);
|
||||
|
||||
return emoji;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = EmojiStore;
|
||||
28
src/stores/GuildChannelStore.js
Normal file
28
src/stores/GuildChannelStore.js
Normal file
@@ -0,0 +1,28 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const TextChannel = require('../structures/TextChannel');
|
||||
const VoiceChannel = require('../structures/VoiceChannel');
|
||||
const Constants = require('../util/Constants');
|
||||
/**
|
||||
* Stores guild channels.
|
||||
* @private
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class GuildChannelStore extends DataStore {
|
||||
constructor(guild, iterable) {
|
||||
super(guild.client, iterable);
|
||||
this.guild = guild;
|
||||
}
|
||||
|
||||
create(data, cache = true) {
|
||||
const existing = this.get(data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const ChannelModel = data.type === Constants.ChannelTypes.TEXT ? TextChannel : VoiceChannel;
|
||||
const channel = new ChannelModel(this.guild, data);
|
||||
if (cache) this.set(channel.id, channel);
|
||||
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildChannelStore;
|
||||
25
src/stores/GuildMemberStore.js
Normal file
25
src/stores/GuildMemberStore.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const GuildMember = require('../structures/GuildMember');
|
||||
/**
|
||||
* Stores guild members.
|
||||
* @private
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class GuildMemberStore extends DataStore {
|
||||
constructor(guild, iterable) {
|
||||
super(guild.client, iterable);
|
||||
this.guild = guild;
|
||||
}
|
||||
|
||||
create(data) {
|
||||
const existing = this.has(data.user.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const member = new GuildMember(this.guild, data);
|
||||
this.set(member.id, member);
|
||||
|
||||
return member;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildMemberStore;
|
||||
20
src/stores/GuildStore.js
Normal file
20
src/stores/GuildStore.js
Normal file
@@ -0,0 +1,20 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const Guild = require('../structures/Guild');
|
||||
/**
|
||||
* Stores guilds.
|
||||
* @private
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class GuildStore extends DataStore {
|
||||
create(data) {
|
||||
const existing = this.get(data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const guild = new Guild(this.client, data);
|
||||
this.set(guild.id, guild);
|
||||
|
||||
return guild;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = GuildStore;
|
||||
97
src/stores/MessageStore.js
Normal file
97
src/stores/MessageStore.js
Normal file
@@ -0,0 +1,97 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const Collection = require('../util/Collection');
|
||||
let Message;
|
||||
|
||||
/**
|
||||
* Stores messages for text-based channels.
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class MessageStore extends DataStore {
|
||||
constructor(channel, iterable) {
|
||||
super(channel.client, iterable);
|
||||
this.channel = channel;
|
||||
Message = require('../structures/Message');
|
||||
}
|
||||
|
||||
create(data, cache = true) {
|
||||
const existing = this.get(data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const message = new Message(this.client.channels.get(data.channel_id), data, this.client);
|
||||
|
||||
if (cache) this.set(message.id, message);
|
||||
return message;
|
||||
}
|
||||
|
||||
set(key, value) {
|
||||
const maxSize = this.client.options.messageCacheMaxSize;
|
||||
if (maxSize === 0) return;
|
||||
if (this.size >= maxSize && maxSize > 0) this.delete(this.firstKey());
|
||||
super.set(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 a message, or messages, from this channel.
|
||||
* @param {Snowflake|ChannelLogsQueryOptions} [message] The ID of the message to fetch, or query parameters.
|
||||
* @returns {Promise<Message>|Promise<Collection<Snowflake, Message>>}
|
||||
* @example
|
||||
* // Get message
|
||||
* channel.messages.fetch('99539446449315840')
|
||||
* .then(message => console.log(message.content))
|
||||
* .catch(console.error);
|
||||
* @example
|
||||
* // Get messages
|
||||
* channel.messages.fetch({limit: 10})
|
||||
* .then(messages => console.log(`Received ${messages.size} messages`))
|
||||
* .catch(console.error);
|
||||
*/
|
||||
fetch(message) {
|
||||
return typeof message === 'string' ? this._fetchId(message) : this._fetchMany(message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetches the pinned messages of this channel and returns a collection of them.
|
||||
* @returns {Promise<Collection<Snowflake, Message>>}
|
||||
*/
|
||||
fetchPinned() {
|
||||
return this.client.api.channels[this.message.channel.id].pins.get().then(data => {
|
||||
const messages = new Collection();
|
||||
for (const message of data) messages.set(message.id, this.create(message));
|
||||
return messages;
|
||||
});
|
||||
}
|
||||
|
||||
_fetchId(messageID) {
|
||||
if (!this.client.user.bot) {
|
||||
return this._fetchMany({ 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.channel.id].messages[messageID].get()
|
||||
.then(data => this.create(data));
|
||||
}
|
||||
|
||||
_fetchMany(options = {}) {
|
||||
return this.client.api.channels[this.channel.id].messages.get({ query: options })
|
||||
.then(data => {
|
||||
const messages = new Collection();
|
||||
for (const message of data) messages.set(message.id, this.create(message));
|
||||
return messages;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = MessageStore;
|
||||
27
src/stores/ReactionStore.js
Normal file
27
src/stores/ReactionStore.js
Normal file
@@ -0,0 +1,27 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const MessageReaction = require('../structures/MessageReaction');
|
||||
/**
|
||||
* Stores reactions.
|
||||
* @private
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class ReactionStore extends DataStore {
|
||||
constructor(message, iterable) {
|
||||
super(message.client, iterable);
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
create(data) {
|
||||
const emojiID = data.emoji.id || decodeURIComponent(data.emoji.name);
|
||||
|
||||
const existing = this.get(data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const reaction = new MessageReaction(this.message, data.emoji, data.count, data.me);
|
||||
this.set(emojiID, reaction);
|
||||
|
||||
return reaction;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = ReactionStore;
|
||||
25
src/stores/RoleStore.js
Normal file
25
src/stores/RoleStore.js
Normal file
@@ -0,0 +1,25 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const Role = require('../structures/Role');
|
||||
/**
|
||||
* Stores roles.
|
||||
* @private
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class RoleStore extends DataStore {
|
||||
constructor(guild, iterable) {
|
||||
super(guild.client, iterable);
|
||||
this.guild = guild;
|
||||
}
|
||||
|
||||
create(data) {
|
||||
const existing = this.get(data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const role = new Role(this.guild, data);
|
||||
this.set(role.id, role);
|
||||
|
||||
return role;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = RoleStore;
|
||||
35
src/stores/UserStore.js
Normal file
35
src/stores/UserStore.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const DataStore = require('./DataStore');
|
||||
const User = require('../structures/User');
|
||||
|
||||
/**
|
||||
* A data store to store User models.
|
||||
* @extends {DataStore}
|
||||
*/
|
||||
class UserStore extends DataStore {
|
||||
create(data) {
|
||||
const existing = this.get(data.id);
|
||||
if (existing) return existing;
|
||||
|
||||
const user = new User(this.client, data);
|
||||
this.set(user.id, user);
|
||||
return user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtains a user from Discord, or the user cache if it's already available.
|
||||
* <warn>This is only available when using a bot account.</warn>
|
||||
* @param {Snowflake} id ID of the user
|
||||
* @param {boolean} [cache=true] Whether to cache the new user object if it isn't already
|
||||
* @returns {Promise<User>}
|
||||
*/
|
||||
fetch(id, cache = true) {
|
||||
const existing = this.get(id);
|
||||
if (existing) return Promise.resolve(existing);
|
||||
|
||||
return this.client.api.users(id).get().then(data =>
|
||||
cache ? this.create(data) : new User(this.client, data)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = UserStore;
|
||||
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'
|
||||
);
|
||||
|
||||
@@ -8,9 +8,9 @@ console.time('magic');
|
||||
|
||||
const client = new Discord.Client({ fetchAllMembers: true, apiRequestMethod: 'sequential' });
|
||||
|
||||
const { email, password, token, usertoken, song } = require('./auth.json');
|
||||
const { email, password, token, usertoken, song } = require('./auth.js');
|
||||
|
||||
client.login(token).then(atoken => console.log('logged in with token ' + atoken)).catch(console.error);
|
||||
client.login(token).then(atoken => console.log('logged in')).catch(console.error);
|
||||
|
||||
client.on('ready', () => {
|
||||
console.log(`ready with ${client.users.size} users`);
|
||||
|
||||
2
typings
2
typings
Submodule typings updated: 0967675a2f...f07cf2bb96
Reference in New Issue
Block a user