mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-11 09:03:29 +01:00
Store Channel Messages in Maps
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -12,10 +12,10 @@ class MessageDeleteAction extends Action {
|
||||
const client = this.client;
|
||||
const channel = client.store.get('channels', data.channel_id);
|
||||
if (channel) {
|
||||
let message = channel.store.get('messages', data.id);
|
||||
let message = channel.messages.get(data.id);
|
||||
|
||||
if (message) {
|
||||
channel.store.remove('messages', message.id);
|
||||
channel.messages.delete(message.id);
|
||||
this.deleted[channel.id + message.id] = message;
|
||||
this.scheduleForDeletion(channel.id, message.id);
|
||||
} else if (this.deleted[channel.id + data.id]) {
|
||||
|
||||
@@ -9,7 +9,7 @@ class MessageUpdateAction extends Action {
|
||||
const channel = client.store.get('channels', data.channel_id);
|
||||
|
||||
if (channel) {
|
||||
const message = channel.store.get('messages', data.id);
|
||||
const message = channel.messages.get(data.id);
|
||||
if (message && !message.equals(data, true)) {
|
||||
const oldMessage = cloneObject(message);
|
||||
message.patch(data);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
const Channel = require('./Channel');
|
||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||
const User = require('./User');
|
||||
const TextChannelDataStore = require('./datastore/TextChannelDataStore');
|
||||
|
||||
/**
|
||||
* Represents a Direct Message Channel between two users.
|
||||
@@ -11,22 +10,23 @@ const TextChannelDataStore = require('./datastore/TextChannelDataStore');
|
||||
class DMChannel extends Channel {
|
||||
constructor(client, data) {
|
||||
super(client, data);
|
||||
this.store = new TextChannelDataStore();
|
||||
this.messages = new Map();
|
||||
}
|
||||
|
||||
_cacheMessage(message) {
|
||||
const maxSize = this.client.options.max_message_cache;
|
||||
if (maxSize === 0) {
|
||||
// saves on performance
|
||||
return;
|
||||
return null;
|
||||
}
|
||||
|
||||
const storeKeys = Object.keys(this.store);
|
||||
if (storeKeys.length >= maxSize) {
|
||||
this.store.remove(storeKeys[0]);
|
||||
if (this.messages.size >= maxSize) {
|
||||
this.messages.delete(Array.from(this.messages.keys())[0]);
|
||||
}
|
||||
|
||||
this.store.add('messages', message);
|
||||
this.messages.set(message.id, message);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
setup(data) {
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
const GuildChannel = require('./GuildChannel');
|
||||
const TextChannelDataStore = require('./datastore/TextChannelDataStore');
|
||||
const TextBasedChannel = require('./interface/TextBasedChannel');
|
||||
|
||||
/**
|
||||
@@ -11,7 +10,7 @@ class TextChannel extends GuildChannel {
|
||||
|
||||
constructor(guild, data) {
|
||||
super(guild, data);
|
||||
this.store = new TextChannelDataStore();
|
||||
this.messages = new Map();
|
||||
}
|
||||
|
||||
_cacheMessage(message) {
|
||||
@@ -21,12 +20,13 @@ class TextChannel extends GuildChannel {
|
||||
return null;
|
||||
}
|
||||
|
||||
const storeKeys = Object.keys(this.store);
|
||||
if (storeKeys.length >= maxSize) {
|
||||
this.store.remove(storeKeys[0]);
|
||||
if (this.messages.size >= maxSize) {
|
||||
this.messages.delete(Array.from(this.messages.keys())[0]);
|
||||
}
|
||||
|
||||
return this.store.add('messages', message);
|
||||
this.messages.set(message.id, message);
|
||||
|
||||
return message;
|
||||
}
|
||||
|
||||
sendMessage() {
|
||||
|
||||
@@ -3,6 +3,14 @@
|
||||
* @interface
|
||||
*/
|
||||
class TextBasedChannel {
|
||||
|
||||
constructor() {
|
||||
/**
|
||||
* A Map containing the messages sent to this channel.
|
||||
* @type {Map<String, Message>}
|
||||
*/
|
||||
this.messages = new Map();
|
||||
}
|
||||
/**
|
||||
* Send a message to this channel
|
||||
* @param {String} content the content to send
|
||||
|
||||
Reference in New Issue
Block a user