From 8689a1debb6f5fac8095f8f393773a200ec72e7d Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Fri, 14 Jul 2023 01:58:41 +0100 Subject: [PATCH 1/8] feat: support new application endpoints --- .../src/structures/ClientApplication.js | 71 +++++++++++++++++++ packages/discord.js/typings/index.d.ts | 17 +++++ 2 files changed, 88 insertions(+) diff --git a/packages/discord.js/src/structures/ClientApplication.js b/packages/discord.js/src/structures/ClientApplication.js index 69f51340bb5d..b8bd0f18fc1a 100644 --- a/packages/discord.js/src/structures/ClientApplication.js +++ b/packages/discord.js/src/structures/ClientApplication.js @@ -6,6 +6,7 @@ const Team = require('./Team'); const Application = require('./interfaces/Application'); const ApplicationCommandManager = require('../managers/ApplicationCommandManager'); const ApplicationFlagsBitField = require('../util/ApplicationFlagsBitField'); +const DataResolver = require('../util/DataResolver'); const PermissionsBitField = require('../util/PermissionsBitField'); /** @@ -119,6 +120,16 @@ class ClientApplication extends Application { this.botRequireCodeGrant ??= null; } + if ('bot' in data) { + /** + * A partial user for the bot associated with this application. + * @type {?User} + */ + this.bot = data.bot; + } else { + this.bot ??= null; + } + if ('bot_public' in data) { /** * If this application's bot is public @@ -129,6 +140,16 @@ class ClientApplication extends Application { this.botPublic ??= null; } + if ('interactions_endpoint_url' in data) { + /** + * This application's interaction endpoint URL. + * @type {?string} + */ + this.interactionsEndpointURL = data.interactions_endpoint_url; + } else { + this.interactionsEndpointURL ??= null; + } + if ('role_connections_verification_url' in data) { /** * This application's role connection verification entry point URL @@ -168,6 +189,56 @@ class ClientApplication extends Application { return !this.name; } + /** + * Options used for editing an application. + * @typedef {Object} ClientApplicationEditOptions + * @property {string} [customInstallURL] The application's custom installation URL + * @property {string} [description] The application's description + * @property {string} [roleConnectionsVerificationURL] The application's role connection verification URL + * @property {ClientApplicationInstallParams} [installParams] + * Settings for the application's default in-app authorization + * @property {ApplicationFlagsResolvable} [flags] The flags for the application + * @property {?(BufferResolvable|Base64Resolvable)} [icon] The application's icon + * @property {?(BufferResolvable|Base64Resolvable)} [coverImage] The application's cover image + * @property {string} [interactionsEndpointURL] The application's interaction endpoint URL + * @property {string[]} [tags] The application's tags + */ + + /** + * Edits this application. + * @param {ClientApplicationEditOptions} [options] The options for editing this application + * @returns {Promise} + */ + async edit({ + customInstallURL, + description, + roleConnectionsVerificationURL, + installParams, + flags, + icon, + coverImage, + interactionsEndpointURL, + tags, + } = {}) { + // TODO: `Routes.oauth2CurrentApplication()` throws bots cannot use this endpoint. Wait for discord-api-types? + const data = await this.client.rest.patch('/applications/@me', { + body: { + custom_install_url: customInstallURL, + description, + role_connections_verification_url: roleConnectionsVerificationURL, + install_params: installParams, + flags, + icon: icon && (await DataResolver.resolveImage(icon)), + cover_image: coverImage && (await DataResolver.resolveImage(coverImage)), + interactions_endpoint_url: interactionsEndpointURL, + tags, + }, + }); + + this._patch(data); + return this; + } + /** * Obtains this application from Discord. * @returns {Promise} diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 08305031c2ed..4e0091292699 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -496,6 +496,8 @@ export class ApplicationFlagsBitField extends BitField { public static resolve(bit?: BitFieldResolvable): number; } +export type ApplicationFlagsResolvable = BitFieldResolvable; + export type AutoModerationRuleResolvable = AutoModerationRule | Snowflake; export abstract class Base { @@ -1018,6 +1020,7 @@ export class ClientApplication extends Application { private constructor(client: Client, data: RawClientApplicationData); public botPublic: boolean | null; public botRequireCodeGrant: boolean | null; + public bot: PartialUser | null; public commands: ApplicationCommandManager; public guildId: Snowflake | null; public get guild(): Guild | null; @@ -1029,8 +1032,10 @@ export class ClientApplication extends Application { public customInstallURL: string | null; public owner: User | Team | null; public get partial(): boolean; + public interactionsEndpointURL: string | null; public roleConnectionsVerificationURL: string | null; public rpcOrigins: string[]; + public edit(optins: ClientApplicationEditOptions): Promise; public fetch(): Promise; public fetchRoleConnectionMetadataRecords(): Promise; public editRoleConnectionMetadataRecords( @@ -6513,6 +6518,18 @@ export interface WelcomeScreenEditOptions { welcomeChannels?: WelcomeChannelData[]; } +export interface ClientApplicationEditOptions { + customInstallURL?: string; + description?: string; + roleConnectionsVerificationURL?: string; + installParams?: ClientApplicationInstallParams; + flags?: ApplicationFlagsResolvable; + icon?: BufferResolvable | Base64Resolvable | null; + coverImage?: BufferResolvable | Base64Resolvable | null; + interactionsEndpointURL?: string; + tags?: string[]; +} + export interface ClientApplicationInstallParams { scopes: OAuth2Scopes[]; permissions: Readonly; From ddac31d3b4a067c7715b247ead41395e65081071 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Fri, 14 Jul 2023 02:20:35 +0100 Subject: [PATCH 2/8] chore: edit comment --- packages/discord.js/src/structures/ClientApplication.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/src/structures/ClientApplication.js b/packages/discord.js/src/structures/ClientApplication.js index b8bd0f18fc1a..77b4e62bf939 100644 --- a/packages/discord.js/src/structures/ClientApplication.js +++ b/packages/discord.js/src/structures/ClientApplication.js @@ -220,7 +220,7 @@ class ClientApplication extends Application { interactionsEndpointURL, tags, } = {}) { - // TODO: `Routes.oauth2CurrentApplication()` throws bots cannot use this endpoint. Wait for discord-api-types? + // TODO: `Routes.currentApplication()` (https://github.com/discordjs/discord-api-types/pull/728) const data = await this.client.rest.patch('/applications/@me', { body: { custom_install_url: customInstallURL, From 53290aa4e46a327e6f4259e7028369dbe549a375 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Fri, 14 Jul 2023 02:28:53 +0100 Subject: [PATCH 3/8] fix(ClientApplication): handle flags properly --- .../src/structures/ClientApplication.js | 2 +- .../src/util/ApplicationFlagsBitField.js | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/packages/discord.js/src/structures/ClientApplication.js b/packages/discord.js/src/structures/ClientApplication.js index 77b4e62bf939..d6ff9a4f599b 100644 --- a/packages/discord.js/src/structures/ClientApplication.js +++ b/packages/discord.js/src/structures/ClientApplication.js @@ -227,7 +227,7 @@ class ClientApplication extends Application { description, role_connections_verification_url: roleConnectionsVerificationURL, install_params: installParams, - flags, + flags: flags === undefined ? undefined : ApplicationFlagsBitField.resolve(flags), icon: icon && (await DataResolver.resolveImage(icon)), cover_image: coverImage && (await DataResolver.resolveImage(coverImage)), interactions_endpoint_url: interactionsEndpointURL, diff --git a/packages/discord.js/src/util/ApplicationFlagsBitField.js b/packages/discord.js/src/util/ApplicationFlagsBitField.js index 6a5a9fe9fec1..f9ad6abacfd7 100644 --- a/packages/discord.js/src/util/ApplicationFlagsBitField.js +++ b/packages/discord.js/src/util/ApplicationFlagsBitField.js @@ -23,4 +23,19 @@ class ApplicationFlagsBitField extends BitField { * @param {BitFieldResolvable} [bits=0] Bit(s) to read from */ +/** + * Bitfield of the packed bits + * @type {number} + * @name ApplicationFlagsBitField#bitfield + */ + +/** + * Data that can be resolved to give an application flag bit field. This can be: + * * A string (see {@link ApplicationFlagsBitField.Flags}) + * * An application flag + * * An instance of ApplicationFlagsBitField + * * An Array of ApplicationFlagsResolvable + * @typedef {string|number|ApplicationFlagsBitField|ApplicationFlagsResolvable[]} ApplicationFlagsResolvable + */ + module.exports = ApplicationFlagsBitField; From 4cd79184f57320e417dcf75d019ced8341920115 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Mon, 14 Aug 2023 11:32:52 +0100 Subject: [PATCH 4/8] types: `readonly` --- packages/discord.js/typings/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 4e0091292699..27a6b5434405 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -6527,7 +6527,7 @@ export interface ClientApplicationEditOptions { icon?: BufferResolvable | Base64Resolvable | null; coverImage?: BufferResolvable | Base64Resolvable | null; interactionsEndpointURL?: string; - tags?: string[]; + tags?: readonly string[]; } export interface ClientApplicationInstallParams { From da4ef0f73787beb62c8a24143367576b91031cae Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Tue, 22 Aug 2023 11:04:27 +0100 Subject: [PATCH 5/8] chore: update route --- packages/discord.js/src/structures/ClientApplication.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/packages/discord.js/src/structures/ClientApplication.js b/packages/discord.js/src/structures/ClientApplication.js index d6ff9a4f599b..bb8035f8869c 100644 --- a/packages/discord.js/src/structures/ClientApplication.js +++ b/packages/discord.js/src/structures/ClientApplication.js @@ -220,8 +220,7 @@ class ClientApplication extends Application { interactionsEndpointURL, tags, } = {}) { - // TODO: `Routes.currentApplication()` (https://github.com/discordjs/discord-api-types/pull/728) - const data = await this.client.rest.patch('/applications/@me', { + const data = await this.client.rest.patch(Routes.currentApplication(), { body: { custom_install_url: customInstallURL, description, From 3931d8aad673fbee9d82b43b5001a43be0d7bec0 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Tue, 22 Aug 2023 18:52:16 +0100 Subject: [PATCH 6/8] feat: add to core --- packages/core/src/api/applications.ts | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/packages/core/src/api/applications.ts b/packages/core/src/api/applications.ts index 4c288c74e4e1..daa7a800420f 100644 --- a/packages/core/src/api/applications.ts +++ b/packages/core/src/api/applications.ts @@ -1,7 +1,14 @@ /* eslint-disable jsdoc/check-param-names */ import type { RequestData, REST } from '@discordjs/rest'; -import { type RESTGetCurrentApplicationResult, Routes } from 'discord-api-types/v10'; +import { + type RESTGetCurrentApplicationResult, + // @ts-expect-error discord-api-types + type RESTPatchCurrentApplicationJSONBody, + // @ts-expect-error discord-api-types + type RESTPatchCurrentApplicationResult, + Routes, +} from 'discord-api-types/v10'; export class ApplicationsAPI { public constructor(private readonly rest: REST) {} @@ -15,4 +22,18 @@ export class ApplicationsAPI { public async getCurrent({ signal }: Pick = {}) { return this.rest.get(Routes.currentApplication(), { signal }) as Promise; } + + /** + * Edits properties of the application associated with the requesting bot user. + * + * @see {@link https://discord.com/developers/docs/resources/application#edit-current-application} + * @param body - The new application data + * @param options - The options for editing the application + */ + public async editCurrent(body: RESTPatchCurrentApplicationJSONBody, { signal }: Pick = {}) { + return this.rest.patch(Routes.currentApplication(), { + body, + signal, + }) as Promise; + } } From dc67800a493e96cd62ccfa7d64a4875dd95c72c8 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Tue, 22 Aug 2023 23:32:13 +0100 Subject: [PATCH 7/8] refactor(ClientApplication): add to user manager --- packages/discord.js/src/structures/ClientApplication.js | 4 ++-- packages/discord.js/typings/index.d.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/discord.js/src/structures/ClientApplication.js b/packages/discord.js/src/structures/ClientApplication.js index bb8035f8869c..065b1fa1bbf9 100644 --- a/packages/discord.js/src/structures/ClientApplication.js +++ b/packages/discord.js/src/structures/ClientApplication.js @@ -122,10 +122,10 @@ class ClientApplication extends Application { if ('bot' in data) { /** - * A partial user for the bot associated with this application. + * The bot associated with this application. * @type {?User} */ - this.bot = data.bot; + this.bot = this.client.users._add(data.bot); } else { this.bot ??= null; } diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 27a6b5434405..b651c51931e5 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -1020,7 +1020,7 @@ export class ClientApplication extends Application { private constructor(client: Client, data: RawClientApplicationData); public botPublic: boolean | null; public botRequireCodeGrant: boolean | null; - public bot: PartialUser | null; + public bot: User | null; public commands: ApplicationCommandManager; public guildId: Snowflake | null; public get guild(): Guild | null; From 1a8e4edbcebdb0174bf362b34706a9142be08cd7 Mon Sep 17 00:00:00 2001 From: Jiralite <33201955+Jiralite@users.noreply.github.com> Date: Sat, 7 Oct 2023 17:19:47 +0100 Subject: [PATCH 8/8] chore: remove comments --- packages/core/src/api/applications.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/packages/core/src/api/applications.ts b/packages/core/src/api/applications.ts index daa7a800420f..97445935d231 100644 --- a/packages/core/src/api/applications.ts +++ b/packages/core/src/api/applications.ts @@ -3,9 +3,7 @@ import type { RequestData, REST } from '@discordjs/rest'; import { type RESTGetCurrentApplicationResult, - // @ts-expect-error discord-api-types type RESTPatchCurrentApplicationJSONBody, - // @ts-expect-error discord-api-types type RESTPatchCurrentApplicationResult, Routes, } from 'discord-api-types/v10';