feat(Client): ClientUserSettingManager => ClientSettingManager

client.setting => client.settings

...
This commit is contained in:
March 7th 2022-08-20 20:26:56 +07:00
parent 3ec6bf45fb
commit cbc3fbb7b8
8 changed files with 207 additions and 59 deletions

View File

@ -11,7 +11,7 @@ const WebSocketManager = require('./websocket/WebSocketManager');
const { Error, TypeError, RangeError } = require('../errors'); const { Error, TypeError, RangeError } = require('../errors');
const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager'); const BaseGuildEmojiManager = require('../managers/BaseGuildEmojiManager');
const ChannelManager = require('../managers/ChannelManager'); const ChannelManager = require('../managers/ChannelManager');
const ClientUserSettingManager = require('../managers/ClientUserSettingManager'); const ClientSettingManager = require('../managers/ClientSettingManager');
const DeveloperPortalManager = require('../managers/DeveloperPortalManager'); const DeveloperPortalManager = require('../managers/DeveloperPortalManager');
const GuildManager = require('../managers/GuildManager'); const GuildManager = require('../managers/GuildManager');
const RelationshipsManager = require('../managers/RelationshipsManager'); const RelationshipsManager = require('../managers/RelationshipsManager');
@ -146,9 +146,9 @@ class Client extends BaseClient {
this.relationships = new RelationshipsManager(this); this.relationships = new RelationshipsManager(this);
/** /**
* All of the settings {@link Object} * All of the settings {@link Object}
* @type {ClientUserSettingManager} * @type {ClientSettingManager}
*/ */
this.setting = new ClientUserSettingManager(this); this.settings = new ClientSettingManager(this);
/** /**
* All of the guilds the client is currently handling, mapped by their ids - * 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 * as long as sharding isn't being used, this will be *every* guild the bot is a member of

View File

@ -111,7 +111,7 @@ module.exports = (client, { d: data }, shard) => {
client.user.setAFK(false); client.user.setAFK(false);
client.setting._patch(data.user_settings); client.settings._patch(data.user_settings);
client.user.connectedAccounts = data.connected_accounts ?? []; client.user.connectedAccounts = data.connected_accounts ?? [];

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
const { Events } = require('../../../util/Constants'); const { Events } = require('../../../util/Constants');
module.exports = (client, { d: data }) => { module.exports = (client, { d: data }) => {
client.setting._patch(data); client.settings._patch(data);
if (('status' in data || 'custom_status' in data) && client.options.readyStatus) { if (('status' in data || 'custom_status' in data) && client.options.readyStatus) {
client.customStatusAuto(client); client.customStatusAuto(client);
} }

View File

@ -1,16 +1,18 @@
'use strict'; 'use strict';
const { Collection } = require('@discordjs/collection'); const { Collection } = require('@discordjs/collection');
// Not used: const { remove } = require('lodash');
const BaseManager = require('./BaseManager'); const BaseManager = require('./BaseManager');
const GuildFolderManager = require('./GuildFolderManager');
const { Error, TypeError } = require('../errors/DJSError'); const { Error, TypeError } = require('../errors/DJSError');
const { localeObject, DMScanLevel, stickerAnimationMode } = require('../util/Constants'); const GuildFolder = require('../structures/GuildFolder');
const { CustomStatus } = require('../structures/RichPresence');
const { localeSetting, DMScanLevel, stickerAnimationMode } = require('../util/Constants');
/** /**
* Manages API methods for users and stores their cache. * Manages API methods for users and stores their cache.
* @extends {BaseManager} * @extends {BaseManager}
* @see {@link https://luna.gitlab.io/discord-unofficial-docs/user_settings.html} * @see {@link https://luna.gitlab.io/discord-unofficial-docs/user_settings.html}
*/ */
class ClientUserSettingManager extends BaseManager { class ClientSettingManager extends BaseManager {
constructor(client) { constructor(client) {
super(client); super(client);
/** /**
@ -124,20 +126,11 @@ class ClientUserSettingManager extends BaseManager {
* @see {@link https://luna.gitlab.io/discord-unofficial-docs/custom_status.html#customstatus-structure} * @see {@link https://luna.gitlab.io/discord-unofficial-docs/custom_status.html#customstatus-structure}
*/ */
this.customStatus = null; this.customStatus = null;
/**
* @typedef {object} guildFolderData
* @property {Snowflake} guildId Guild ID
* @property {number | string} folderId ID of the folder
* @property {number} folderIndex Index of the folder
* @property {string} folderName Name of the folder
* @property {any} folderColor Color of the folder
* @property {Snowflake[]} folderGuilds Array of guild IDs in the folder
*/
/** /**
* Guild folder and position * Guild folder and position
* @type {Collection<Snowflake, guildFolderData>} * @type {GuildFolderManager}
*/ */
this.guildMetadata = new Collection(); this.guildFolder = new GuildFolderManager(client);
// Todo: add new method from Discum // Todo: add new method from Discum
} }
/** /**
@ -149,7 +142,7 @@ class ClientUserSettingManager extends BaseManager {
_patch(data) { _patch(data) {
this.rawSetting = Object.assign(this.rawSetting, data); this.rawSetting = Object.assign(this.rawSetting, data);
if ('locale' in data) { if ('locale' in data) {
this.locale = localeObject[data.locale]; this.locale = localeSetting[data.locale];
} }
if ('show_current_game' in data) { if ('show_current_game' in data) {
this.activityDisplay = data.show_current_game; this.activityDisplay = data.show_current_game;
@ -210,22 +203,10 @@ class ClientUserSettingManager extends BaseManager {
mutual_guilds: data.friend_source_flags.all ? true : data.friend_source_flags.mutual_guilds, mutual_guilds: data.friend_source_flags.all ? true : data.friend_source_flags.mutual_guilds,
}; };
} }
if ('guild_folders' in data && 'guild_positions' in data) { if ('guild_folders' in data) {
const data_ = data.guild_positions.map((guildId, i) => { data.guild_folders.map((folder, index) =>
// Find folder this.guildFolder.cache.set(index, new GuildFolder(this.client, folder)),
const folderIndex = data.guild_folders.findIndex(obj => obj.guild_ids.includes(guildId)); );
const metadata = {
guildId: guildId,
guildIndex: i,
folderId: data.guild_folders[folderIndex]?.id,
folderIndex,
folderName: data.guild_folders[folderIndex]?.name,
folderColor: data.guild_folders[folderIndex]?.color,
folderGuilds: data.guild_folders[folderIndex]?.guild_ids,
};
return [guildId, metadata];
});
this.guildMetadata = new Collection(data_);
} }
if ('restricted_guilds' in data) { if ('restricted_guilds' in data) {
this.disableDMfromServer = new Collection(data.restricted_guilds.map(guildId => [guildId, true])); this.disableDMfromServer = new Collection(data.restricted_guilds.map(guildId => [guildId, true]));
@ -254,8 +235,8 @@ class ClientUserSettingManager extends BaseManager {
* @returns {boolean} * @returns {boolean}
*/ */
async setDisplayCompactMode(value) { async setDisplayCompactMode(value) {
if (typeof value !== 'boolean' && value !== null && typeof value !== 'undefined') { if (typeof value !== 'boolean' && value !== null) {
throw new TypeError('INVALID_TYPE', 'value', 'boolean | null | undefined', true); throw new TypeError('INVALID_TYPE', 'value', 'boolean | null', true);
} }
if (!value) value = !this.compactMode; if (!value) value = !this.compactMode;
if (value !== this.compactMode) { if (value !== this.compactMode) {
@ -270,8 +251,8 @@ class ClientUserSettingManager extends BaseManager {
*/ */
async setTheme(value) { async setTheme(value) {
const validValues = ['dark', 'light']; const validValues = ['dark', 'light'];
if (typeof value !== 'string' && value !== null && typeof value !== 'undefined') { if (typeof value !== 'string' && value !== null) {
throw new TypeError('INVALID_TYPE', 'value', 'string | null | undefined', true); throw new TypeError('INVALID_TYPE', 'value', 'string | null', true);
} }
if (!validValues.includes(value)) { if (!validValues.includes(value)) {
if (value == validValues[0]) value = validValues[1]; if (value == validValues[0]) value = validValues[1];
@ -293,12 +274,31 @@ class ClientUserSettingManager extends BaseManager {
*/ */
/** /**
* Set custom status (Setting) * Set custom status
* @param {CustomStatusOption} options Object | null * @param {?CustomStatus | CustomStatusOption} options CustomStatus
*/ */
setCustomStatus(options) { setCustomStatus(options) {
if (typeof options !== 'object') { if (typeof options !== 'object') {
this.edit({ custom_status: null }); this.edit({ custom_status: null });
} else if (options instanceof CustomStatus) {
options = options.toJSON();
let data = {
emoji_name: null,
expires_at: null,
text: null,
};
if (typeof options.state === 'string') {
data.text = options.state;
}
if (options.emoji) {
if (options.emoji?.id) {
data.emoji_name = options.emoji?.name;
data.emoji_id = options.emoji?.id;
} else {
data.emoji_name = typeof options.emoji?.name === 'string' ? options.emoji?.name : null;
}
}
this.edit({ custom_status: data });
} else { } else {
let data = { let data = {
emoji_name: null, emoji_name: null,
@ -363,16 +363,16 @@ class ClientUserSettingManager extends BaseManager {
* * `JAPANESE` * * `JAPANESE`
* * `TAIWAN_CHINESE` * * `TAIWAN_CHINESE`
* * `KOREAN` * * `KOREAN`
* @param {string} value Locale to set * @param {localeSetting} value Locale to set
* @returns {locale} * @returns {locale}
*/ */
async setLocale(value) { async setLocale(value) {
if (typeof value !== 'string') { if (typeof value !== 'string') {
throw new TypeError('INVALID_TYPE', 'value', 'string', true); throw new TypeError('INVALID_TYPE', 'value', 'string', true);
} }
if (!localeObject[value]) throw new Error('INVALID_LOCALE'); if (!localeSetting[value]) throw new Error('INVALID_LOCALE');
if (localeObject[value] !== this.locale) { if (localeSetting[value] !== this.locale) {
await this.edit({ locale: localeObject[value] }); await this.edit({ locale: localeSetting[value] });
} }
return this.locale; return this.locale;
} }
@ -487,4 +487,4 @@ class ClientUserSettingManager extends BaseManager {
} }
} }
module.exports = ClientUserSettingManager; module.exports = ClientSettingManager;

View File

@ -0,0 +1,24 @@
'use strict';
const { Collection } = require('@discordjs/collection');
const BaseManager = require('./BaseManager');
/**
* Manages API methods for users and stores their cache.
* @extends {BaseManager}
*/
class GuildFolderManager extends BaseManager {
constructor(client) {
super(client);
/**
* The guild folder cache (Index, GuildFolder)
* @type {Collection<number, GuildFolder>}
*/
this.cache = new Collection();
}
_refresh() {
this.cache.clear();
}
}
module.exports = GuildFolderManager;

View File

@ -0,0 +1,75 @@
'use strict';
const Base = require('./Base');
/**
* Guild Folder.
* @abstract
*/
class GuildFolder extends Base {
constructor(client, data) {
super(client);
this._patch(data);
}
_patch(data) {
if ('id' in data) {
/**
* The guild folder's id
* @type {Snowflake}
*/
this.id = data.id;
}
if ('name' in data) {
/**
* The guild folder's name
* @type {string}
*/
this.name = data.name;
}
if ('color' in data) {
/**
* The base 10 color of the folder
* @type {number}
*/
this.color = data.color;
}
if ('guild_ids' in data) {
/**
* The guild folder's guild ids
* @type {Snowflake[]}
*/
this.guild_ids = data.guild_ids;
}
}
/**
* The hexadecimal version of the folder color, with a leading hashtag
* @type {string}
* @readonly
*/
get hexColor() {
return `#${this.color.toString(16).padStart(6, '0')}`;
}
/**
* Guilds in the folder
* @type {Collection<Snowflake, Guild>}
* @readonly
*/
get guilds() {
return this.client.guilds.cache.filter(guild => this.guild_ids.includes(guild.id));
}
toJSON() {
return {
id: this.id,
name: this.name,
color: this.color,
guild_ids: this.guild_ids,
};
}
}
module.exports = GuildFolder;

View File

@ -69,7 +69,41 @@ exports.NitroType = createEnum(['NONE', 'NITRO_CLASSIC', 'NITRO_BOOST']);
*/ */
exports.HypeSquadType = createEnum(['LEAVE', 'HOUSE_BRAVERY', 'HOUSE_BRILLIANCE', 'HOUSE_BALANCE']); exports.HypeSquadType = createEnum(['LEAVE', 'HOUSE_BRAVERY', 'HOUSE_BRILLIANCE', 'HOUSE_BALANCE']);
exports.localeObject = { /**
* All locale codes:
* * `DANISH`
* * `GERMAN`
* * `ENGLISH_UK`
* * `ENGLISH_US`
* * `SPANISH`
* * `FRENCH`
* * `CROATIAN`
* * `ITALIAN`
* * `LITHUANIAN`
* * `HUNGARIAN`
* * `DUTCH`
* * `NORWEGIAN`
* * `POLISH`
* * `BRAZILIAN_PORTUGUESE`
* * `ROMANIA_ROMANIAN`
* * `FINNISH`
* * `SWEDISH`
* * `VIETNAMESE`
* * `TURKISH`
* * `CZECH`
* * `GREEK`
* * `BULGARIAN`
* * `RUSSIAN`
* * `UKRAINIAN`
* * `HINDI`
* * `THAI`
* * `CHINA_CHINESE`
* * `JAPANESE`
* * `TAIWAN_CHINESE`
* * `KOREAN`
* @typedef {Object<string, string>} localeSetting
*/
exports.localeSetting = {
da: 'DANISH', da: 'DANISH',
de: 'GERMAN', de: 'GERMAN',
'en-GB': 'ENGLISH_UK', 'en-GB': 'ENGLISH_UK',

37
typings/index.d.ts vendored
View File

@ -169,6 +169,7 @@ export abstract class DiscordAuthWebsocket extends EventEmitter {
public heartbeatInterval?: number; public heartbeatInterval?: number;
public ws?: WebSocket; public ws?: WebSocket;
public token?: string; public token?: string;
public realToken?: string;
public user?: RawUserData; public user?: RawUserData;
public readonly exprireTime: string; public readonly exprireTime: string;
public connect(client?: Client): void; public connect(client?: Client): void;
@ -808,7 +809,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public application: If<Ready, ClientApplication>; public application: If<Ready, ClientApplication>;
// Added // Added
public setting: ClientUserSettingManager; public setting: ClientSettingManager;
public relationships: RelationshipsManager; public relationships: RelationshipsManager;
public updateCookie(): Promise<void>; public updateCookie(): Promise<void>;
public readonly callVoice?: VoiceConnection; public readonly callVoice?: VoiceConnection;
@ -3560,15 +3561,29 @@ export class ChannelManager extends CachedManager<Snowflake, AnyChannel, Channel
export type FetchGuildApplicationCommandFetchOptions = Omit<FetchApplicationCommandOptions, 'guildId'>; export type FetchGuildApplicationCommandFetchOptions = Omit<FetchApplicationCommandOptions, 'guildId'>;
export interface guildFolderData { export class GuildFolderManager extends BaseManager {
guildId: Snowflake; private constructor(client: Client);
folderId: number | string; public cache: Collection<number, GuildFolder>;
folderIndex: number;
folderName: string;
folderColor: any;
folderGuilds: Snowflake[];
} }
export class ClientUserSettingManager extends BaseManager {
export interface RawGuildFolderData {
id: number | null;
name: string | null;
guild_ids: Snowflake[];
color: number | null;
}
export class GuildFolder extends Base {
private constructor(client: Client, data: RawGuildFolderData);
public id: number | null;
public name: string | null;
public guild_ids: Snowflake[];
public color: number | null;
public readonly hexColor: string | null;
public readonly guilds: Collection<Snowflake, Guild>;
public toJSON(): RawGuildFolderData;
}
export class ClientSettingManager extends BaseManager {
private constructor(client: Client); private constructor(client: Client);
public rawSetting: RawUserSettingsData | object; public rawSetting: RawUserSettingsData | object;
public locale: localeSetting | null; public locale: localeSetting | null;
@ -3598,14 +3613,14 @@ export class ClientUserSettingManager extends BaseManager {
} }
| object; | object;
public addFriendFrom: { all?: boolean; mutual_friends?: boolean; mututal_guilds?: boolean } | object; public addFriendFrom: { all?: boolean; mutual_friends?: boolean; mututal_guilds?: boolean } | object;
public guildMetadata: Collection<Snowflake, guildFolderData>; public guildFolder: GuildFolderManager;
public disableDMfromServer: Collection<Snowflake, boolean>; public disableDMfromServer: Collection<Snowflake, boolean>;
public fetch(): Promise<RawUserSettingsData>; public fetch(): Promise<RawUserSettingsData>;
public setDisplayCompactMode(value?: boolean): Promise<this>; public setDisplayCompactMode(value?: boolean): Promise<this>;
public setTheme(value?: 'dark' | 'light'): Promise<this>; public setTheme(value?: 'dark' | 'light'): Promise<this>;
public setLocale(value: localeSetting): Promise<this>; public setLocale(value: localeSetting): Promise<this>;
// @ts-ignore // @ts-ignore
public setCustomStatus(value?: CustomStatusOption): Promise<this>; public setCustomStatus(value?: CustomStatusOption | CustomStatus): Promise<this>;
public restrictedGuilds(status: boolean): Promise<void>; public restrictedGuilds(status: boolean): Promise<void>;
public addRestrictedGuild(guildId: GuildResolvable): Promise<void>; public addRestrictedGuild(guildId: GuildResolvable): Promise<void>;
public removeRestrictedGuild(guildId: GuildResolvable): Promise<void>; public removeRestrictedGuild(guildId: GuildResolvable): Promise<void>;