Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Guild): add fetchWidget() for getting widget data #6180

Merged
merged 2 commits into from
Jul 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/client/Client.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -589,7 +589,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 @@ -615,7 +616,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 @@ -3525,7 +3526,7 @@ export interface GuildCreateOptions {
verificationLevel?: VerificationLevel | number;
}

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

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