Skip to content

Commit

Permalink
feat(Guild): add fetchWidget() for getting widget data (#6180)
Browse files Browse the repository at this point in the history
  • Loading branch information
advaith1 committed Jul 27, 2021
1 parent 2f1cc1f commit b22272f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
6 changes: 3 additions & 3 deletions src/client/Client.js
Expand Up @@ -404,11 +404,11 @@ class Client extends BaseClient {
}

/**
* Obtains the widget of a guild from Discord, available for guilds with the widget enabled.
* @param {GuildResolvable} guild The guild to fetch the widget for
* Obtains the widget data of a guild from Discord, available for guilds with the widget enabled.
* @param {GuildResolvable} guild The guild to fetch the widget data for
* @returns {Promise<Widget>}
*/
async fetchWidget(guild) {
async fetchGuildWidget(guild) {
const id = this.guilds.resolveId(guild);
if (!id) throw new TypeError('INVALID_TYPE', 'guild', 'GuildResolvable');
const data = await this.api.guilds(id, 'widget.json').get();
Expand Down
47 changes: 30 additions & 17 deletions src/structures/Guild.js
Expand Up @@ -669,29 +669,42 @@ class Guild extends AnonymousGuild {
}

/**
* Data for the Guild Widget object
* @typedef {Object} GuildWidget
* Fetches the guild widget data, requires the widget to be enabled.
* @returns {Promise<Widget>}
* @example
* // Fetches the guild widget data
* guild.fetchWidget()
* .then(widget => console.log(`The widget shows ${widget.channels.size} channels`))
* .catch(console.error);
*/
fetchWidget() {
return this.client.fetchGuildWidget(this.id);
}

/**
* Data for the Guild Widget Settings object
* @typedef {Object} GuildWidgetSettings
* @property {boolean} enabled Whether the widget is enabled
* @property {?GuildChannel} channel The widget channel
* @property {?GuildChannel} channel The widget invite channel
*/

/**
* The Guild Widget object
* @typedef {Object} GuildWidgetData
* The Guild Widget Settings object
* @typedef {Object} GuildWidgetSettingsData
* @property {boolean} enabled Whether the widget is enabled
* @property {?GuildChannelResolvable} channel The widget channel
* @property {?GuildChannelResolvable} channel The widget invite channel
*/

/**
* Fetches the guild widget.
* @returns {Promise<GuildWidget>}
* Fetches the guild widget settings.
* @returns {Promise<GuildWidgetSettings>}
* @example
* // Fetches the guild widget
* guild.fetchWidget()
* // Fetches the guild widget settings
* guild.fetchWidgetSettings()
* .then(widget => console.log(`The widget is ${widget.enabled ? 'enabled' : 'disabled'}`))
* .catch(console.error);
*/
async fetchWidget() {
async fetchWidgetSettings() {
const data = await this.client.api.guilds(this.id).widget.get();
this.widgetEnabled = data.enabled;
this.widgetChannelId = data.channel_id;
Expand Down Expand Up @@ -1234,18 +1247,18 @@ class Guild extends AnonymousGuild {
}

/**
* Edits the guild's widget.
* @param {GuildWidgetData} widget The widget for the guild
* @param {string} [reason] Reason for changing the guild's widget
* Edits the guild's widget settings.
* @param {GuildWidgetSettingsData} settings The widget settings for the guild
* @param {string} [reason] Reason for changing the guild's widget settings
* @returns {Promise<Guild>}
*/
setWidget(widget, reason) {
setWidgetSettings(settings, reason) {
return this.client.api
.guilds(this.id)
.widget.patch({
data: {
enabled: widget.enabled,
channel_id: this.channels.resolveId(widget.channel),
enabled: settings.enabled,
channel_id: this.channels.resolveId(settings.channel),
},
reason,
})
Expand Down
11 changes: 6 additions & 5 deletions typings/index.d.ts
Expand Up @@ -298,7 +298,7 @@ export class Client<Ready extends boolean = boolean> extends BaseClient {
public fetchSticker(id: Snowflake): Promise<Sticker>;
public fetchPremiumStickerPacks(): Promise<Collection<Snowflake, StickerPack>>;
public fetchWebhook(id: Snowflake, token?: string): Promise<Webhook>;
public fetchWidget(guild: GuildResolvable): Promise<Widget>;
public fetchGuildWidget(guild: GuildResolvable): Promise<Widget>;
public generateInvite(options?: InviteGenerationOptions): string;
public login(token?: string): Promise<string>;
public isReady(): this is Client<true>;
Expand Down Expand Up @@ -591,7 +591,8 @@ export class Guild extends AnonymousGuild {
public fetchVanityData(): Promise<Vanity>;
public fetchWebhooks(): Promise<Collection<Snowflake, Webhook>>;
public fetchWelcomeScreen(): Promise<WelcomeScreen>;
public fetchWidget(): Promise<GuildWidget>;
public fetchWidget(): Promise<Widget>;
public fetchWidgetSettings(): Promise<GuildWidgetSettings>;
public leave(): Promise<Guild>;
public setAFKChannel(afkChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
public setAFKTimeout(afkTimeout: number, reason?: string): Promise<Guild>;
Expand All @@ -617,7 +618,7 @@ export class Guild extends AnonymousGuild {
public setSystemChannel(systemChannel: ChannelResolvable | null, reason?: string): Promise<Guild>;
public setSystemChannelFlags(systemChannelFlags: SystemChannelFlagsResolvable, reason?: string): Promise<Guild>;
public setVerificationLevel(verificationLevel: VerificationLevel | number, reason?: string): Promise<Guild>;
public setWidget(widget: GuildWidgetData, reason?: string): Promise<Guild>;
public setWidgetSettings(settings: GuildWidgetSettingsData, reason?: string): Promise<Guild>;
public toJSON(): unknown;
}

Expand Down Expand Up @@ -3527,7 +3528,7 @@ export interface GuildCreateOptions {
verificationLevel?: VerificationLevel | number;
}

export interface GuildWidget {
export interface GuildWidgetSettings {
enabled: boolean;
channel: GuildChannel | null;
}
Expand Down Expand Up @@ -3618,7 +3619,7 @@ export interface GuildPruneMembersOptions {
roles?: RoleResolvable[];
}

export interface GuildWidgetData {
export interface GuildWidgetSettingsData {
enabled: boolean;
channel: GuildChannelResolvable | null;
}
Expand Down

0 comments on commit b22272f

Please sign in to comment.