diff --git a/client/src/structs/discord/channel.js b/client/src/structs/discord/channel.js index 05f17749..20a55bb3 100644 --- a/client/src/structs/discord/channel.js +++ b/client/src/structs/discord/channel.js @@ -5,9 +5,14 @@ import { Guild } from './guild'; import { Message } from './message'; import { User, GuildMember } from './user'; +const channels = new WeakMap(); + export class Channel { constructor(data) { + if (channels.has(data)) return channels.get(data); + channels.set(data, this); + this.discordObject = data; } diff --git a/client/src/structs/discord/guild.js b/client/src/structs/discord/guild.js index dfc1a4b0..b6c4bf9e 100644 --- a/client/src/structs/discord/guild.js +++ b/client/src/structs/discord/guild.js @@ -4,9 +4,14 @@ import { List } from 'structs'; import { Channel } from './channel'; import { GuildMember } from './user'; +const guilds = new WeakMap(); + export class Guild { constructor(data) { + if (guilds.has(data)) return guilds.get(data); + guilds.set(data, this); + this.discordObject = data; } diff --git a/client/src/structs/discord/message.js b/client/src/structs/discord/message.js index c565896c..54ec2735 100644 --- a/client/src/structs/discord/message.js +++ b/client/src/structs/discord/message.js @@ -3,9 +3,14 @@ import { DiscordApi, DiscordApiModules as Modules } from 'modules'; import { Channel } from './channel'; import { User } from './user'; +const messages = new WeakMap(); + export class Message { constructor(data) { + if (messages.has(data)) return messages.get(data); + messages.set(data, this); + this.discordObject = data; } @@ -61,7 +66,7 @@ export class Message { * @param {Boolean} parse Whether to parse the message or update it as it is */ edit(content, parse = false) { - if (this.author.id !== DiscordApi.currentUser.id) + if (this.author !== DiscordApi.currentUser) throw new Error('Cannot edit messages sent by other users.'); if (parse) Modules.MessageActions.editMessage(this.channel_id, this.id, Modules.MessageParser.parse(this.discordObject, content)); else Modules.MessageActions.editMessage(this.channel_id, this.id, {content}); @@ -71,7 +76,7 @@ export class Message { * Start the edit mode of the UI. */ startEdit() { - if (this.author.id !== DiscordApi.currentUser.id) + if (this.author !== DiscordApi.currentUser) throw new Error('Cannot edit messages sent by other users.'); Modules.MessageActions.startEditMessage(this.channel_id, this.id, this.content); } diff --git a/client/src/structs/discord/user.js b/client/src/structs/discord/user.js index 54a983de..b7d21251 100644 --- a/client/src/structs/discord/user.js +++ b/client/src/structs/discord/user.js @@ -4,9 +4,15 @@ import { List, InsufficientPermissions } from 'structs'; import { Guild } from './guild'; import { PrivateChannel } from './channel'; +const users = new WeakMap(); + export class User { - constructor(data) { + constructor(data, _wm) { + if (!_wm) _wm = users; + if (_wm.has(data)) return _wm.get(data); + _wm.set(data, this); + this.discordObject = data; } @@ -74,11 +80,13 @@ export class User { } +const guild_members = new WeakMap(); + // TODO: don't extend User export class GuildMember extends User { constructor(data, guild_id) { const user = Modules.UserStore.getUser(data.userId); - super(user); + super(user, guild_members); this.member_data = data; this.guild_id = guild_id;