Skip to content

Commit

Permalink
feat: add guild onboarding
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Feb 9, 2023
1 parent 0e4224b commit 2b321ab
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/discord.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions packages/discord.js/src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -734,6 +735,15 @@ class Guild extends AnonymousGuild {
return new GuildAuditLogs(this, data);
}

/**
* Fetches the guild onboarding data for this guild.
* @returns {Promise<GuildOnboarding>}
*/
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
Expand Down
102 changes: 102 additions & 0 deletions packages/discord.js/src/structures/GuildOnboarding.js
Original file line number Diff line number Diff line change
@@ -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;
36 changes: 36 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1325,6 +1325,7 @@ export class Guild extends AnonymousGuild {
options?: GuildAuditLogsFetchOptions<T>,
): Promise<GuildAuditLogs<T>>;
public fetchIntegrations(): Promise<Collection<Snowflake | string, Integration>>;
public fetchOnboarding(): Promise<GuildOnboarding>;
public fetchOwner(options?: BaseFetchOptions): Promise<GuildMember>;
public fetchPreview(): Promise<GuildPreview>;
public fetchTemplates(): Promise<Collection<GuildTemplate['code'], GuildTemplate>>;
Expand Down Expand Up @@ -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<true>, data: RawGuildPreviewData);
public approximateMemberCount: number;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 2b321ab

Please sign in to comment.