mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-10 08:33:30 +01:00
* Start Store cleanup * wip store cleanup * fix iterables initiating datastores * handle the possibility of a datastore with no holds and no its own 'create' method * more cleanup (instances that need more than just client/data) * missed RoleStore extras * not sure how eslint didn't catch that tab... * avoid re-getting the channel we already have... * cleanup resolvers and refactor them into DataStores * ^ * and remove * fix some bugs * fix lint * fix documentation maybe? * formatting * fix presences * really fix presences this time * bad fix was bad... let;s see how bad this one is.. * forgot to save a file * make presence resolving take userresolveables too * fix tabs in jsdocs * fix bad fix from last night that caused issues, with better fix... * oops
101 lines
2.6 KiB
JavaScript
101 lines
2.6 KiB
JavaScript
const DataStore = require('./DataStore');
|
|
const Channel = require('../structures/Channel');
|
|
const Constants = require('../util/Constants');
|
|
|
|
const kLru = Symbol('LRU');
|
|
const lruable = ['group', 'dm'];
|
|
|
|
/**
|
|
* Stores channels.
|
|
* @private
|
|
* @extends {DataStore}
|
|
*/
|
|
class ChannelStore extends DataStore {
|
|
constructor(client, iterableOrOptions = {}, options) {
|
|
if (!options && typeof iterableOrOptions[Symbol.iterator] !== 'function') {
|
|
options = iterableOrOptions;
|
|
iterableOrOptions = undefined;
|
|
}
|
|
super(client, iterableOrOptions, Channel);
|
|
|
|
if (options.lru) {
|
|
const lru = this[kLru] = [];
|
|
lru.add = item => {
|
|
lru.remove(item);
|
|
lru.unshift(item);
|
|
while (lru.length > options.lru) this.remove(lru[lru.length - 1]);
|
|
};
|
|
lru.remove = item => {
|
|
const index = lru.indexOf(item);
|
|
if (index > -1) lru.splice(index, 1);
|
|
};
|
|
}
|
|
}
|
|
|
|
get(key, peek = false) {
|
|
const item = super.get(key);
|
|
if (!item || !lruable.includes(item.type)) return item;
|
|
if (!peek && this[kLru]) this[kLru].add(key);
|
|
return item;
|
|
}
|
|
|
|
set(key, val) {
|
|
if (this[kLru] && lruable.includes(val.type)) this[kLru].add(key);
|
|
return super.set(key, val);
|
|
}
|
|
|
|
delete(key) {
|
|
const item = this.get(key, true);
|
|
if (!item) return false;
|
|
if (this[kLru] && lruable.includes(item.type)) this[kLru].remove(key);
|
|
return super.delete(key);
|
|
}
|
|
|
|
create(data, guild, cache = true) {
|
|
const existing = this.get(data.id);
|
|
if (existing) return existing;
|
|
|
|
const channel = Channel.create(this.client, data, guild);
|
|
|
|
if (!channel) {
|
|
this.client.emit(Constants.Events.DEBUG, `Failed to find guild for channel ${data.id} ${data.type}`);
|
|
return null;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* Data that can be resolved to give a Channel object. This can be:
|
|
* * A Channel object
|
|
* * A Snowflake
|
|
* @typedef {Channel|Snowflake} ChannelResolvable
|
|
*/
|
|
|
|
/**
|
|
* Resolves a ChannelResolvable to a Channel object.
|
|
* @method resolve
|
|
* @memberof ChannelStore
|
|
* @param {ChannelResolvable} channel The channel resolvable to resolve
|
|
* @returns {?Channel}
|
|
*/
|
|
|
|
/**
|
|
* Resolves a ChannelResolvable to a channel ID string.
|
|
* @method resolveID
|
|
* @memberof ChannelStore
|
|
* @param {ChannelResolvable} channel The channel resolvable to resolve
|
|
* @returns {?string}
|
|
*/
|
|
}
|
|
|
|
module.exports = ChannelStore;
|