mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-17 12:03:31 +01:00
Remove GuildDataStore and move towards storing data in Maps
This commit is contained in:
@@ -92,7 +92,7 @@ class ClientDataResolver {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return guild.store.get('members', user.id);
|
return guild.members.get(user.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ class GuildMemberRemoveAction extends Action {
|
|||||||
const client = this.client;
|
const client = this.client;
|
||||||
const guild = client.store.get('guilds', data.guild_id);
|
const guild = client.store.get('guilds', data.guild_id);
|
||||||
if (guild) {
|
if (guild) {
|
||||||
let member = guild.store.get('members', data.user.id);
|
let member = guild.members.get(data.user.id);
|
||||||
if (member) {
|
if (member) {
|
||||||
guild._removeMember(member);
|
guild._removeMember(member);
|
||||||
this.deleted[guild.id + data.user.id] = member;
|
this.deleted[guild.id + data.user.id] = member;
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ class GuildRoleCreate extends Action {
|
|||||||
const guild = client.store.get('guilds', data.guild_id);
|
const guild = client.store.get('guilds', data.guild_id);
|
||||||
|
|
||||||
if (guild) {
|
if (guild) {
|
||||||
const already = guild.store.get('roles', data.role.id);
|
const already = guild.roles.get(data.role.id);
|
||||||
const role = new Role(guild, data.role);
|
const role = new Role(guild, data.role);
|
||||||
guild.store.add('roles', role);
|
guild.roles.set(role.id, role);
|
||||||
|
|
||||||
if (!already) {
|
if (!already) {
|
||||||
client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role);
|
client.emit(Constants.Events.GUILD_ROLE_CREATE, guild, role);
|
||||||
|
|||||||
@@ -14,9 +14,9 @@ class GuildRoleDeleteAction extends Action {
|
|||||||
const guild = client.store.get('guilds', data.guild_id);
|
const guild = client.store.get('guilds', data.guild_id);
|
||||||
|
|
||||||
if (guild) {
|
if (guild) {
|
||||||
let exists = guild.store.get('roles', data.role_id);
|
let exists = guild.roles.get(data.role_id);
|
||||||
if (exists) {
|
if (exists) {
|
||||||
guild.store.remove('roles', data.role_id);
|
guild.roles.remove(data.role_id);
|
||||||
this.deleted[guild.id + data.role_id] = exists;
|
this.deleted[guild.id + data.role_id] = exists;
|
||||||
this.scheduleForDeletion(guild.id, data.role_id);
|
this.scheduleForDeletion(guild.id, data.role_id);
|
||||||
client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists);
|
client.emit(Constants.Events.GUILD_ROLE_DELETE, guild, exists);
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ class GuildRoleUpdateAction extends Action {
|
|||||||
|
|
||||||
if (guild) {
|
if (guild) {
|
||||||
let oldRole;
|
let oldRole;
|
||||||
const existingRole = guild.store.get('roles', roleData.id);
|
const existingRole = guild.roles.get(roleData.id);
|
||||||
// exists and not the same
|
// exists and not the same
|
||||||
if (existingRole && !existingRole.equals(roleData)) {
|
if (existingRole && !existingRole.equals(roleData)) {
|
||||||
oldRole = cloneObject(existingRole);
|
oldRole = cloneObject(existingRole);
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class GuildMemberUpdateHandler extends AbstractHandler {
|
|||||||
const guild = client.store.get('guilds', data.guild_id);
|
const guild = client.store.get('guilds', data.guild_id);
|
||||||
|
|
||||||
if (guild) {
|
if (guild) {
|
||||||
const member = guild.store.get('members', data.user.id);
|
const member = guild.members.get(data.user.id);
|
||||||
if (member) {
|
if (member) {
|
||||||
guild._updateMember(member, data);
|
guild._updateMember(member, data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ class PresenceUpdateHandler extends AbstractHandler {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (guild) {
|
if (guild) {
|
||||||
const memberInGuild = guild.store.get('members', user.id);
|
const memberInGuild = guild.members.get(user.id);
|
||||||
if (!memberInGuild) {
|
if (!memberInGuild) {
|
||||||
const member = guild._addMember({
|
const member = guild._addMember({
|
||||||
user,
|
user,
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ class VoiceStateUpdateHandler extends AbstractHandler {
|
|||||||
const guild = client.store.get('guilds', data.guild_id);
|
const guild = client.store.get('guilds', data.guild_id);
|
||||||
|
|
||||||
if (guild) {
|
if (guild) {
|
||||||
const member = guild.store.get('members', data.user_id);
|
const member = guild.members.get(data.user_id);
|
||||||
if (member) {
|
if (member) {
|
||||||
const oldVoiceChannelMember = cloneObject(member);
|
const oldVoiceChannelMember = cloneObject(member);
|
||||||
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {
|
if (member.voiceChannel && member.voiceChannel.id !== data.channel_id) {
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
const User = require('./User');
|
const User = require('./User');
|
||||||
const GuildMember = require('./GuildMember');
|
const GuildMember = require('./GuildMember');
|
||||||
const GuildDataStore = require('./datastore/GuildDataStore');
|
|
||||||
const Constants = require('../util/Constants');
|
const Constants = require('../util/Constants');
|
||||||
const Role = require('./Role');
|
const Role = require('./Role');
|
||||||
|
|
||||||
@@ -31,10 +30,22 @@ class Guild {
|
|||||||
this.client = client;
|
this.client = client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data store of the Guild.
|
* A Map of members that are in this Guild. The key is the member's ID, the value is the member.
|
||||||
* @type {GuildDataStore}
|
* @type {Map<String, GuildMember>}
|
||||||
*/
|
*/
|
||||||
this.store = new GuildDataStore();
|
this.members = new Map();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Map of channels that are in this Guild. The key is the channel's ID, the value is the channel.
|
||||||
|
* @type {Map<String, GuildChannel>}
|
||||||
|
*/
|
||||||
|
this.channels = new Map();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Map of roles that are in this Guild. The key is the role's ID, the value is the role.
|
||||||
|
* @type {Map<String, Role>}
|
||||||
|
*/
|
||||||
|
this.roles = new Map();
|
||||||
|
|
||||||
if (!data) {
|
if (!data) {
|
||||||
return;
|
return;
|
||||||
@@ -63,7 +74,8 @@ class Guild {
|
|||||||
}
|
}
|
||||||
|
|
||||||
guildUser.joined_at = guildUser.joined_at || 0;
|
guildUser.joined_at = guildUser.joined_at || 0;
|
||||||
const member = this.store.add('members', new GuildMember(this, guildUser));
|
const member = new GuildMember(this, guildUser);
|
||||||
|
this.members.set(member.id, member);
|
||||||
if (this.client.ws.status === Constants.Status.READY && !noEvent) {
|
if (this.client.ws.status === Constants.Status.READY && !noEvent) {
|
||||||
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member);
|
this.client.emit(Constants.Events.GUILD_MEMBER_ADD, this, member);
|
||||||
}
|
}
|
||||||
@@ -81,7 +93,7 @@ class Guild {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_removeMember(guildMember) {
|
_removeMember(guildMember) {
|
||||||
this.store.remove('members', guildMember);
|
this.members.delete(guildMember.id);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -236,7 +248,7 @@ class Guild {
|
|||||||
this.features = data.features || [];
|
this.features = data.features || [];
|
||||||
|
|
||||||
if (data.members) {
|
if (data.members) {
|
||||||
this.store.clear('members');
|
this.members.clear();
|
||||||
for (const guildUser of data.members) {
|
for (const guildUser of data.members) {
|
||||||
this._addMember(guildUser);
|
this._addMember(guildUser);
|
||||||
}
|
}
|
||||||
@@ -246,10 +258,10 @@ class Guild {
|
|||||||
* The owner of the guild
|
* The owner of the guild
|
||||||
* @type {User}
|
* @type {User}
|
||||||
*/
|
*/
|
||||||
this.owner = this.store.get('members', data.owner_id);
|
this.owner = this.members.get(data.owner_id);
|
||||||
|
|
||||||
if (data.channels) {
|
if (data.channels) {
|
||||||
this.store.clear('channels');
|
this.channels.clear();
|
||||||
for (const channel of data.channels) {
|
for (const channel of data.channels) {
|
||||||
this.client.store.newChannel(channel, this);
|
this.client.store.newChannel(channel, this);
|
||||||
}
|
}
|
||||||
@@ -259,12 +271,13 @@ class Guild {
|
|||||||
* The embed channel of the Guild.
|
* The embed channel of the Guild.
|
||||||
* @type {GuildChannel}
|
* @type {GuildChannel}
|
||||||
*/
|
*/
|
||||||
this.embedChannel = this.store.get('channels', data.embed_channel_id);
|
this.embedChannel = this.channels.get(data.embed_channel_id);
|
||||||
|
|
||||||
if (data.roles) {
|
if (data.roles) {
|
||||||
this.store.clear('roles');
|
this.roles.clear();
|
||||||
for (const role of data.roles) {
|
for (const role of data.roles) {
|
||||||
this.store.add('roles', new Role(this, role));
|
const newRole = new Role(this, role);
|
||||||
|
this.roles.set(newRole.id, newRole);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,7 +293,7 @@ class Guild {
|
|||||||
|
|
||||||
if (data.voice_states) {
|
if (data.voice_states) {
|
||||||
for (const voiceState of data.voice_states) {
|
for (const voiceState of data.voice_states) {
|
||||||
const member = this.store.get('members', voiceState.user_id);
|
const member = this.members.get(voiceState.user_id);
|
||||||
if (member) {
|
if (member) {
|
||||||
member.serverMute = voiceState.mute;
|
member.serverMute = voiceState.mute;
|
||||||
member.serverDeaf = voiceState.deaf;
|
member.serverDeaf = voiceState.deaf;
|
||||||
@@ -475,51 +488,6 @@ class Guild {
|
|||||||
setSplash(splash) {
|
setSplash(splash) {
|
||||||
return this.edit({ splash });
|
return this.edit({ splash });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* The channels in the guild.
|
|
||||||
* @type {Array<GuildChannel>}
|
|
||||||
* @readonly
|
|
||||||
*/
|
|
||||||
get channels() { return this.store.getAsArray('channels'); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A dictionary mapping the IDs of channels in this guild to the channel itself. If you want to find a channel
|
|
||||||
* in the guild by ID, use `guild.$channels[id]` rather than filtering `guild.channels` as it is much more efficient.
|
|
||||||
* @readonly
|
|
||||||
* @type {Object<String, Channel>}
|
|
||||||
*/
|
|
||||||
get $channels() { return this.store.data.channels; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The roles in the guild.
|
|
||||||
* @type {Array<Role>}
|
|
||||||
* @readonly
|
|
||||||
*/
|
|
||||||
get roles() { return this.store.getAsArray('roles'); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A dictionary mapping the IDs of roles in this guild to the role itself. If you want to find a role
|
|
||||||
* in the guild by ID, use `guild.$roles[id]` rather than filtering `guild.roles` as it is much more efficient.
|
|
||||||
* @readonly
|
|
||||||
* @type {Object<String, Role>}
|
|
||||||
*/
|
|
||||||
get $roles() { return this.store.data.roles; }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The members of the guild.
|
|
||||||
* @type {Array<GuildMember>}
|
|
||||||
* @readonly
|
|
||||||
*/
|
|
||||||
get members() { return this.store.getAsArray('members'); }
|
|
||||||
|
|
||||||
/**
|
|
||||||
* A dictionary mapping the IDs of members in this guild to the member object itself. If you want to find a member
|
|
||||||
* in the guild by ID, use `guild.$members[id]` rather than filtering `guild.members` as it is much more efficient.
|
|
||||||
* @readonly
|
|
||||||
* @type {Object<String, GuildMember>}
|
|
||||||
*/
|
|
||||||
get $members() { return this.store.data.members; }
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Guild;
|
module.exports = Guild;
|
||||||
|
|||||||
@@ -74,14 +74,14 @@ class GuildMember {
|
|||||||
*/
|
*/
|
||||||
get roles() {
|
get roles() {
|
||||||
const list = [];
|
const list = [];
|
||||||
const everyoneRole = this.guild.store.get('roles', this.guild.id);
|
const everyoneRole = this.guild.roles.get(this.guild.id);
|
||||||
|
|
||||||
if (everyoneRole) {
|
if (everyoneRole) {
|
||||||
list.push(everyoneRole);
|
list.push(everyoneRole);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const roleID of this._roles) {
|
for (const roleID of this._roles) {
|
||||||
const role = this.guild.store.get('roles', roleID);
|
const role = this.guild.roles.get(roleID);
|
||||||
if (role) {
|
if (role) {
|
||||||
list.push(role);
|
list.push(role);
|
||||||
}
|
}
|
||||||
@@ -114,7 +114,7 @@ class GuildMember {
|
|||||||
* @readonly
|
* @readonly
|
||||||
*/
|
*/
|
||||||
get voiceChannel() {
|
get voiceChannel() {
|
||||||
return this.guild.store.get('channels', this.voiceChannelID);
|
return this.guild.channels.get(this.voiceChannelID);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -53,10 +53,10 @@ class ClientDataStore extends AbstractDataStore {
|
|||||||
if (guild) {
|
if (guild) {
|
||||||
if (data.type === Constants.ChannelTypes.text) {
|
if (data.type === Constants.ChannelTypes.text) {
|
||||||
channel = new TextChannel(guild, data);
|
channel = new TextChannel(guild, data);
|
||||||
guild.store.add('channels', channel);
|
guild.channels.set(channel.id, channel);
|
||||||
} else if (data.type === Constants.ChannelTypes.voice) {
|
} else if (data.type === Constants.ChannelTypes.voice) {
|
||||||
channel = new VoiceChannel(guild, data);
|
channel = new VoiceChannel(guild, data);
|
||||||
guild.store.add('channels', channel);
|
guild.channels.set(channel.id, channel);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -86,7 +86,7 @@ class ClientDataStore extends AbstractDataStore {
|
|||||||
killChannel(channel) {
|
killChannel(channel) {
|
||||||
this.remove('channels', channel);
|
this.remove('channels', channel);
|
||||||
if (channel instanceof GuildChannel) {
|
if (channel instanceof GuildChannel) {
|
||||||
channel.guild.store.remove('channels', channel);
|
channel.guild.channels.delete(channel.id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,12 +0,0 @@
|
|||||||
const AbstractDataStore = require('./AbstractDataStore');
|
|
||||||
|
|
||||||
class GuildDataStore extends AbstractDataStore {
|
|
||||||
constructor() {
|
|
||||||
super();
|
|
||||||
|
|
||||||
this.register('members');
|
|
||||||
this.register('channels');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = GuildDataStore;
|
|
||||||
@@ -121,8 +121,8 @@ client.on('message', message => {
|
|||||||
|
|
||||||
if (message.content === 'stats') {
|
if (message.content === 'stats') {
|
||||||
let m = '';
|
let m = '';
|
||||||
m += `I am aware of ${message.guild.channels.length} channels\n`;
|
m += `I am aware of ${message.guild.channels.size} channels\n`;
|
||||||
m += `I am aware of ${message.guild.members.length} members`;
|
m += `I am aware of ${message.guild.members.size} members`;
|
||||||
message.channel.sendMessage(m);
|
message.channel.sendMessage(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user