Skip to content

Commit

Permalink
chore: use collections
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Feb 9, 2023
1 parent f1f1dd5 commit f915cf3
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 87 deletions.
2 changes: 1 addition & 1 deletion packages/discord.js/src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
60 changes: 26 additions & 34 deletions packages/discord.js/src/structures/GuildOnboarding.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
'use strict';

const { _transformGuildOnboardingPrompt } = require('../util/Transformers');
const { Collection } = require('@discordjs/collection');
const Base = require('./Base');
const { GuildOnboardingPrompt } = require('./GuildOnboardingPrompt.js');

/**
* 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}
Expand All @@ -15,15 +22,21 @@ class GuildOnboarding {

/**
* The prompts shown during onboarding
* @type {GuildOnboardingPrompt[]}
* @type {Collection<Snowflake, GuildOnboardingPrompt>}
*/
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<Snowflake, GuildChannel>}
*/
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
Expand All @@ -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;
77 changes: 77 additions & 0 deletions packages/discord.js/src/structures/GuildOnboardingPrompt.js
Original file line number Diff line number Diff line change
@@ -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<Snowflake, GuildOnboardingPromptOption>}
*/
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;
85 changes: 85 additions & 0 deletions packages/discord.js/src/structures/GuildOnboardingPromptOption.js
Original file line number Diff line number Diff line change
@@ -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<Snowflake, GuildChannel>}
*/
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<Snowflake, Role>}
*/
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;
30 changes: 1 addition & 29 deletions packages/discord.js/src/util/Transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
53 changes: 30 additions & 23 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Snowflake, GuildOnboardingPrompt>;
public defaultChannels: Collection<Snowflake, GuildChannel>;
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<Snowflake, GuildOnboardingPromptOption>;
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<Snowflake, GuildChannel>;
public roles: Collection<Snowflake, Role>;
public emoji: GuildOnboardingPromptOptionEmoji;
public title: string;
public description: string | null;
}

export class GuildPreview extends Base {
private constructor(client: Client<true>, data: RawGuildPreviewData);
public approximateMemberCount: number;
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit f915cf3

Please sign in to comment.