Skip to content

Commit

Permalink
feat: Support widget image URL (#9782)
Browse files Browse the repository at this point in the history
* feat: add widget image URL

* docs(GuildManager): correct parameter type

Co-authored-by: space <spaceeec@yahoo.com>

---------

Co-authored-by: space <spaceeec@yahoo.com>
  • Loading branch information
Jiralite and SpaceEEC committed Sep 17, 2023
1 parent 3109798 commit b6a2441
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 1 deletion.
16 changes: 15 additions & 1 deletion packages/discord.js/src/managers/GuildManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const process = require('node:process');
const { setTimeout, clearTimeout } = require('node:timers');
const { Collection } = require('@discordjs/collection');
const { makeURLSearchParams } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v10');
const { Routes, RouteBases } = require('discord-api-types/v10');
const CachedManager = require('./CachedManager');
const { Guild } = require('../structures/Guild');
const GuildChannel = require('../structures/GuildChannel');
Expand Down Expand Up @@ -278,6 +278,20 @@ class GuildManager extends CachedManager {
const data = await this.client.rest.get(Routes.userGuilds(), { query: makeURLSearchParams(options) });
return data.reduce((coll, guild) => coll.set(guild.id, new OAuth2Guild(this.client, guild)), new Collection());
}

/**
* Returns a URL for the PNG widget of a guild.
* @param {GuildResolvable} guild The guild of the widget image
* @param {GuildWidgetStyle} [style] The style for the widget image
* @returns {string}
*/
widgetImageURL(guild, style) {
const urlSearchParams = String(makeURLSearchParams({ style }));

return `${RouteBases.api}${Routes.guildWidgetImage(this.resolveId(guild))}${
urlSearchParams ? `?${urlSearchParams}` : ''
}`;
}
}

module.exports = GuildManager;
9 changes: 9 additions & 0 deletions packages/discord.js/src/structures/Guild.js
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,15 @@ class Guild extends AnonymousGuild {
};
}

/**
* Returns a URL for the PNG widget of the guild.
* @param {GuildWidgetStyle} [style] The style for the widget image
* @returns {string}
*/
widgetImageURL(style) {
return this.client.guilds.widgetImageURL(this.id, style);
}

/**
* Options used to fetch audit logs.
* @typedef {Object} GuildAuditLogsFetchOptions
Expand Down
9 changes: 9 additions & 0 deletions packages/discord.js/src/structures/Widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ class Widget extends Base {
this._patch(data);
return this;
}

/**
* Returns a URL for the PNG widget of the guild.
* @param {GuildWidgetStyle} [style] The style for the widget image
* @returns {string}
*/
imageURL(style) {
return this.client.guilds.widgetImageURL(this.id, style);
}
}

module.exports = Widget;
5 changes: 5 additions & 0 deletions packages/discord.js/src/util/APITypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/GuildVerificationLevel}
*/

/**
* @external GuildWidgetStyle
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/GuildWidgetStyle}
*/

/**
* @external IntegrationExpireBehavior
* @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/IntegrationExpireBehavior}
Expand Down
4 changes: 4 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ import {
AttachmentFlags,
RoleFlags,
TeamMemberRole,
GuildWidgetStyle,
} from 'discord-api-types/v10';
import { ChildProcess } from 'node:child_process';
import { EventEmitter } from 'node:events';
Expand Down Expand Up @@ -1384,6 +1385,7 @@ export class Guild extends AnonymousGuild {
public fetchWelcomeScreen(): Promise<WelcomeScreen>;
public fetchWidget(): Promise<Widget>;
public fetchWidgetSettings(): Promise<GuildWidgetSettings>;
public widgetImageURL(style?: GuildWidgetStyle): string;
public leave(): Promise<Guild>;
public disableInvites(disabled?: boolean): Promise<Guild>;
public setAFKChannel(afkChannel: VoiceChannelResolvable | null, reason?: string): Promise<Guild>;
Expand Down Expand Up @@ -3450,6 +3452,7 @@ export class Widget extends Base {
private constructor(client: Client<true>, data: RawWidgetData);
private _patch(data: RawWidgetData): void;
public fetch(): Promise<Widget>;
public imageURL(style?: GuildWidgetStyle): string;
public id: Snowflake;
public name: string;
public instantInvite?: string;
Expand Down Expand Up @@ -3969,6 +3972,7 @@ export class GuildManager extends CachedManager<Snowflake, Guild, GuildResolvabl
public create(options: GuildCreateOptions): Promise<Guild>;
public fetch(options: Snowflake | FetchGuildOptions): Promise<Guild>;
public fetch(options?: FetchGuildsOptions): Promise<Collection<Snowflake, OAuth2Guild>>;
public widgetImageURL(guild: GuildResolvable, style?: GuildWidgetStyle): string;
}

export interface AddOrRemoveGuildMemberRoleOptions {
Expand Down

0 comments on commit b6a2441

Please sign in to comment.