From 2cd069f2bfa1952c3b8c93529101b44ed20f001e Mon Sep 17 00:00:00 2001 From: almeidx Date: Thu, 9 Feb 2023 21:17:11 +0000 Subject: [PATCH] chore: use collections --- packages/discord.js/src/structures/Guild.js | 2 +- .../src/structures/GuildOnboarding.js | 60 ++++++------- .../src/structures/GuildOnboardingPrompt.js | 77 +++++++++++++++++ .../structures/GuildOnboardingPromptOption.js | 85 +++++++++++++++++++ packages/discord.js/src/util/Transformers.js | 30 +------ packages/discord.js/typings/index.d.ts | 53 +++++++----- 6 files changed, 220 insertions(+), 87 deletions(-) create mode 100644 packages/discord.js/src/structures/GuildOnboardingPrompt.js create mode 100644 packages/discord.js/src/structures/GuildOnboardingPromptOption.js diff --git a/packages/discord.js/src/structures/Guild.js b/packages/discord.js/src/structures/Guild.js index e220eb78a1aaa..35fae521e8055 100644 --- a/packages/discord.js/src/structures/Guild.js +++ b/packages/discord.js/src/structures/Guild.js @@ -741,7 +741,7 @@ class Guild extends AnonymousGuild { */ async fetchOnboarding() { const data = await this.client.rest.get(`/guilds/${this.id}/onboarding`); - return new GuildOnboarding(data); + return new GuildOnboarding(this.client, data); } /** diff --git a/packages/discord.js/src/structures/GuildOnboarding.js b/packages/discord.js/src/structures/GuildOnboarding.js index 7921719211265..f80d06eab4d4d 100644 --- a/packages/discord.js/src/structures/GuildOnboarding.js +++ b/packages/discord.js/src/structures/GuildOnboarding.js @@ -1,12 +1,19 @@ 'use strict'; -const { _transformGuildOnboardingPrompt } = require('../util/Transformers'); +const { Collection } = require('@discordjs/collection'); +const Base = require('./Base'); +const { GuildOnboardingPrompt } = require('./GuildOnboardingPrompt'); /** * Represents the onboarding data of a guild. + * @extends {Base} */ -class GuildOnboarding { - constructor(data) { +class GuildOnboarding extends Base { + constructor(client, data) { + super(client); + + const guild = this.guild; + /** * The id of the guild this onboarding data is for * @type {Snowflake} @@ -15,15 +22,21 @@ class GuildOnboarding { /** * The prompts shown during onboarding - * @type {GuildOnboardingPrompt[]} + * @type {Collection} */ - this.prompts = data.prompts.map(prompt => _transformGuildOnboardingPrompt(prompt)); + this.prompts = data.prompts.reduce((prompts, prompt) => { + prompts.set(prompt.id, new GuildOnboardingPrompt(client, prompt, this.guildId)); + return prompts; + }, new Collection()); /** * The ids of the channels that new members get opted into automatically - * @type {Snowflake[]} + * @type {Collection} */ - this.defaultChannelIds = data.default_channel_ids; + this.defaultChannels = data.default_channels.reduce((channels, channelId) => { + channels.set(channelId, guild.channels.cache.get(channelId)); + return channels; + }, new Collection()); /** * Whether the onboarding is enabled @@ -33,34 +46,13 @@ class GuildOnboarding { } /** - * The data for a guild onboarding prompt - * @typedef {Object} GuildOnboardingPrompt - * @property {Snowflake} id The id of the prompt - * @property {GuildOnboardingPromptOption[]} options The options of the prompt - * @property {string} title The title of the prompt - * @property {boolean} singleSelect Whether only option of the prompt can be selected at a time - * @property {boolean} required Whether the prompt is required in the onboarding flow - * @property {boolean} inOnboarding Whether the prompt is in the onboarding flow - * @property {GuildOnboardingPromptType} type The type of the prompt - */ - - /** - * The data for an option of a guilds onboarding prompt - * @typedef {Object} GuildOnboardingPromptOption - * @property {Snowflake} id The id of the option - * @property {Snowflake[]} channelIds The ids of the channels opted in when this option is selected - * @property {Snowflake[]} roleIds The ids of the roles assigned when this option is selected - * @property {GuildOnboardingPromptOptionEmoji} emoji The emoji of the option - * @property {string} title The title of the option - * @property {?string} description The description of the option - */ - - /** - * The data for an emoji of a guilds onboarding prompt option - * @typedef {Object} GuildOnboardingPromptOptionEmoji - * @property {?Snowflake} id The id of the emoji - * @property {?string} name The name of the emoji + * The guild this onboarding is from + * @type {Guild} + * @readonly */ + get guild() { + return this.client.guilds.cache.get(this.guildId); + } } exports.GuildOnboarding = GuildOnboarding; diff --git a/packages/discord.js/src/structures/GuildOnboardingPrompt.js b/packages/discord.js/src/structures/GuildOnboardingPrompt.js new file mode 100644 index 0000000000000..2ed24d8ebc1fa --- /dev/null +++ b/packages/discord.js/src/structures/GuildOnboardingPrompt.js @@ -0,0 +1,77 @@ +'use strict'; + +const { Collection } = require('@discordjs/collection'); +const Base = require('./Base'); +const { GuildOnboardingPromptOption } = require('./GuildOnboardingPromptOption'); + +/** + * Represents the data of a prompt of a guilds onboarding. + * @extends {Base} + */ +class GuildOnboardingPrompt extends Base { + constructor(client, data, guildId) { + super(client); + + /** + * The id of the guild this onboarding prompt is from + * @type {Snowflake} + */ + this.guildId = guildId; + + /** + * The id of the prompt + * @type {Snowflake} + */ + this.id = data.id; + + /** + * The options of the prompt + * @type {Collection} + */ + this.options = data.options.reduce((options, option) => { + options.set(option.id, new GuildOnboardingPromptOption(client, option, guildId)); + return options; + }, new Collection()); + + /** + * The title of the prompt + * @type {string} + */ + this.title = data.title; + + /** + * Whether only option of the prompt can be selected at a time + * @type {boolean} + */ + this.singleSelect = data.single_select; + + /** + * Whether the prompt is required in the onboarding flow + * @type {boolean} + */ + this.required = data.required; + + /** + * Whether the prompt is in the onboarding flow + * @type {boolean} + */ + this.inOnboarding = data.in_onboarding; + + /** + * The type of the prompt + * @type {GuildOnboardingPromptType} + */ + this.type = data.type; + } + + /** + * The guild this onboarding prompt is from + * @type {Guild} + * @readonly + */ + get guild() { + return this.client.guilds.cache.get(this.guildId); + } +} + +exports.GuildOnboardingPrompt = GuildOnboardingPrompt; diff --git a/packages/discord.js/src/structures/GuildOnboardingPromptOption.js b/packages/discord.js/src/structures/GuildOnboardingPromptOption.js new file mode 100644 index 0000000000000..ed1e5f70d0f60 --- /dev/null +++ b/packages/discord.js/src/structures/GuildOnboardingPromptOption.js @@ -0,0 +1,85 @@ +'use strict'; + +const { Collection } = require('@discordjs/collection'); +const Base = require('./Base'); + +/** + * Represents the data of an option from a prompt of a guilds onboarding. + * @extends {Base} + */ +class GuildOnboardingPromptOption extends Base { + constructor(client, data, guildId) { + super(client); + + const guild = this.guild; + + /** + * The id of the guild this onboarding prompt option is from + * @type {Snowflake} + */ + this.guildId = guildId; + + /** + * The id of the option + * @type {Snowflake} + */ + this.id = data.id; + + /** + * The channels opted in when this option is selected + * @type {Collection} + */ + this.channels = data.channel_ids.reduce((channels, channelId) => { + channels.set(channelId, guild.channels.cache.get(channelId)); + return channels; + }, new Collection()); + + /** + * The roles assigned when this option is selected + * @type {Collection} + */ + this.roles = data.role_ids.reduce((roles, roleId) => { + roles.set(roleId, guild.roles.cache.get(roleId)); + return roles; + }, new Collection()); + + /** + * The data for an emoji of a guilds onboarding prompt option + * @typedef {Object} GuildOnboardingPromptOptionEmoji + * @property {?Snowflake} id The id of the emoji + * @property {?string} name The name of the emoji + */ + + /** + * The emoji of the option + * @type {GuildOnboardingPromptOptionEmoji} + */ + this.emoji = { + id: data.emoji_id, + name: data.emoji_name, + }; + + /** + * The title of the option + * @type {string} + */ + this.title = data.title; + + /** + * The description of the option + * @type {?string} + */ + this.description = data.description; + } + + /** + * The guild this onboarding prompt option is from + * @type {Guild} + * @readonly + */ + get guild() { + return this.client.guilds.cache.get(this.guildId); + } +} + +exports.GuildOnboardingPromptOption = GuildOnboardingPromptOption; diff --git a/packages/discord.js/src/util/Transformers.js b/packages/discord.js/src/util/Transformers.js index 0ce2dbf7c410c..6b096dfc9f9ec 100644 --- a/packages/discord.js/src/util/Transformers.js +++ b/packages/discord.js/src/util/Transformers.js @@ -16,32 +16,4 @@ function toSnakeCase(obj) { return Object.fromEntries(Object.entries(obj).map(([key, value]) => [snakeCase(key), toSnakeCase(value)])); } -/** - * Transforms a raw prompt object into a GuildOnboardingPrompt - * @param {Object} prompt The raw prompt object - * @returns {GuildOnboardingPrompt} - * @ignore - */ -function _transformGuildOnboardingPrompt(prompt) { - return { - id: prompt.id, - options: prompt.options.map(option => ({ - id: option.id, - channelIds: option.channel_ids, - roleIds: option.role_ids, - emoji: { - id: option.emoji_id, - name: option.emoji_name, - }, - title: option.title, - description: option.description, - })), - title: prompt.title, - singleSelect: prompt.single_select, - required: prompt.required, - inOnboarding: prompt.in_onboarding, - type: prompt.type, - }; -} - -module.exports = { toSnakeCase, _transformGuildOnboardingPrompt }; +module.exports = { toSnakeCase }; diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index a630e8cdd551d..8cdddcbc8c2a2 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1521,14 +1521,40 @@ export class GuildMember extends PartialTextBasedChannel(Base) { public valueOf(): string; } -export class GuildOnboarding { - private constructor(data: any); +export class GuildOnboarding extends Base { + private constructor(client: Client, data: any); + public get guild(): Guild; public guildId: Snowflake; - public prompts: GuildOnboardingPrompt[]; - public defaultChannelIds: Snowflake[]; + public prompts: Collection; + public defaultChannels: Collection; public enabled: boolean; } +export class GuildOnboardingPrompt extends Base { + private constructor(client: Client, data: any, guildId: Snowflake); + public id: Snowflake; + public get guild(): Guild; + public guildId: Snowflake; + public options: Collection; + public title: string; + public singleSelect: boolean; + public required: boolean; + public inOnboarding: boolean; + public type: any; +} + +export class GuildOnboardingPromptOption extends Base { + private constructor(client: Client, data: any, guildId: Snowflake); + public id: Snowflake; + public get guild(): Guild; + public guildId: Snowflake; + public channels: Collection; + public roles: Collection; + public emoji: GuildOnboardingPromptOptionEmoji; + public title: string; + public description: string | null; +} + export class GuildPreview extends Base { private constructor(client: Client, data: RawGuildPreviewData); public approximateMemberCount: number; @@ -5577,25 +5603,6 @@ export type GuildTemplateResolvable = string; export type GuildVoiceChannelResolvable = VoiceBasedChannel | Snowflake; -export interface GuildOnboardingPrompt { - id: Snowflake; - options: GuildOnboardingPromptOption[]; - title: string; - singleSelect: boolean; - required: boolean; - inOnboarding: boolean; - type: any; -} - -export interface GuildOnboardingPromptOption { - id: Snowflake; - channelIds: Snowflake[]; - roleIds: Snowflake[]; - emoji: GuildOnboardingPromptOptionEmoji; - title: string; - description: string | null; -} - export interface GuildOnboardingPromptOptionEmoji { id: Snowflake | null; name: string | null;