diff --git a/packages/discord.js/src/index.js b/packages/discord.js/src/index.js index 93758f3abf2a1..c2e6b5adc7c1b 100644 --- a/packages/discord.js/src/index.js +++ b/packages/discord.js/src/index.js @@ -123,6 +123,7 @@ exports.GuildBan = require('./structures/GuildBan'); exports.GuildChannel = require('./structures/GuildChannel'); exports.GuildEmoji = require('./structures/GuildEmoji'); exports.GuildMember = require('./structures/GuildMember').GuildMember; +exports.GuildOnboarding = require('./structures/GuildOnboarding').GuildOnboarding; exports.GuildPreview = require('./structures/GuildPreview'); exports.GuildPreviewEmoji = require('./structures/GuildPreviewEmoji'); exports.GuildScheduledEvent = require('./structures/GuildScheduledEvent').GuildScheduledEvent; diff --git a/packages/discord.js/src/structures/Guild.js b/packages/discord.js/src/structures/Guild.js index 594163d945299..e220eb78a1aaa 100644 --- a/packages/discord.js/src/structures/Guild.js +++ b/packages/discord.js/src/structures/Guild.js @@ -5,6 +5,7 @@ const { makeURLSearchParams } = require('@discordjs/rest'); const { ChannelType, GuildPremiumTier, Routes, GuildFeature } = require('discord-api-types/v10'); const AnonymousGuild = require('./AnonymousGuild'); const GuildAuditLogs = require('./GuildAuditLogs'); +const GuildOnboarding = require('./GuildOnboarding'); const GuildPreview = require('./GuildPreview'); const GuildTemplate = require('./GuildTemplate'); const Integration = require('./Integration'); @@ -734,6 +735,15 @@ class Guild extends AnonymousGuild { return new GuildAuditLogs(this, data); } + /** + * Fetches the guild onboarding data for this guild. + * @returns {Promise} + */ + async fetchOnboarding() { + const data = await this.client.rest.get(`/guilds/${this.id}/onboarding`); + return new GuildOnboarding(data); + } + /** * The data for editing a guild. * @typedef {Object} GuildEditOptions diff --git a/packages/discord.js/src/structures/GuildOnboarding.js b/packages/discord.js/src/structures/GuildOnboarding.js new file mode 100644 index 0000000000000..e4ed510884c45 --- /dev/null +++ b/packages/discord.js/src/structures/GuildOnboarding.js @@ -0,0 +1,102 @@ +'use strict'; + +/** + * Represents the onboarding data of a guild. + */ +class GuildOnboarding { + constructor(data) { + /** + * The id of the guild this onboarding data is for + * @type {Snowflake} + */ + this.guildId = data.guild_id; + + /** + * The prompts shown during onboarding + * @type {GuildOnboardingPrompt[]} + */ + this.prompts = data.prompts.map(prompt => this._transformPrompt(prompt)); + + /** + * The ids of the channels that new members get opted into automatically + * @type {Snowflake[]} + */ + this.defaultChannelIds = data.default_channel_ids; + + /** + * Whether the onboarding is enabled + * @type {boolean} + */ + this.enabled = data.enabled; + } + + /** + * 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 + */ + + /** + * Transforms a raw prompt object into a GuildOnboardingPrompt + * @param {Object} prompt The raw prompt object + * @returns {GuildOnboardingPrompt} + * @private + */ + _transformPrompt(prompt) { + return { + id: prompt.id, + options: prompt.options.map(option => this._transformPromptOption(option)), + title: prompt.title, + singleSelect: prompt.single_select, + required: prompt.required, + inOnboarding: prompt.in_onboarding, + type: prompt.type, + }; + } + + /** + * 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 + */ + + /** + * Transforms a raw prompt option object into a GuildOnboardingPromptOption + * @param {Object} option The raw prompt option object + * @returns {GuildOnboardingPromptOption} + * @private + */ + _transformPromptOption(option) { + return { + 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, + }; + } +} + +exports.GuildOnboarding = GuildOnboarding; diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 128d20839db8f..7fff903096c2a 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1325,6 +1325,7 @@ export class Guild extends AnonymousGuild { options?: GuildAuditLogsFetchOptions, ): Promise>; public fetchIntegrations(): Promise>; + public fetchOnboarding(): Promise; public fetchOwner(options?: BaseFetchOptions): Promise; public fetchPreview(): Promise; public fetchTemplates(): Promise>; @@ -1520,6 +1521,17 @@ export class GuildMember extends PartialTextBasedChannel(Base) { public valueOf(): string; } +export class GuildOnboarding { + private constructor(data: any); + private _transformPrompt(prompt: any): GuildOnboardingPrompt; + private _transformPromptOption(option: any): GuildOnboardingPromptOption; + + public guildId: Snowflake; + public prompts: GuildOnboardingPrompt[]; + public defaultChannelIds: Snowflake[]; + public enabled: boolean; +} + export class GuildPreview extends Base { private constructor(client: Client, data: RawGuildPreviewData); public approximateMemberCount: number; @@ -5568,6 +5580,30 @@ 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; +} + export type HexColorString = `#${string}`; export interface IntegrationAccount {