Skip to content

Commit 1fe7247

Browse files
feat: Support new application properties and patch endpoint (#9709)
* feat: support new application endpoints * chore: edit comment * fix(ClientApplication): handle flags properly * types: `readonly` * chore: update route * feat: add to core * refactor(ClientApplication): add to user manager * chore: remove comments --------- Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
1 parent 44a3cbf commit 1fe7247

File tree

4 files changed

+122
-1
lines changed

4 files changed

+122
-1
lines changed

packages/core/src/api/applications.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/* eslint-disable jsdoc/check-param-names */
22

33
import type { RequestData, REST } from '@discordjs/rest';
4-
import { type RESTGetCurrentApplicationResult, Routes } from 'discord-api-types/v10';
4+
import {
5+
type RESTGetCurrentApplicationResult,
6+
type RESTPatchCurrentApplicationJSONBody,
7+
type RESTPatchCurrentApplicationResult,
8+
Routes,
9+
} from 'discord-api-types/v10';
510

611
export class ApplicationsAPI {
712
public constructor(private readonly rest: REST) {}
@@ -15,4 +20,18 @@ export class ApplicationsAPI {
1520
public async getCurrent({ signal }: Pick<RequestData, 'signal'> = {}) {
1621
return this.rest.get(Routes.currentApplication(), { signal }) as Promise<RESTGetCurrentApplicationResult>;
1722
}
23+
24+
/**
25+
* Edits properties of the application associated with the requesting bot user.
26+
*
27+
* @see {@link https://discord.com/developers/docs/resources/application#edit-current-application}
28+
* @param body - The new application data
29+
* @param options - The options for editing the application
30+
*/
31+
public async editCurrent(body: RESTPatchCurrentApplicationJSONBody, { signal }: Pick<RequestData, 'signal'> = {}) {
32+
return this.rest.patch(Routes.currentApplication(), {
33+
body,
34+
signal,
35+
}) as Promise<RESTPatchCurrentApplicationResult>;
36+
}
1837
}

packages/discord.js/src/structures/ClientApplication.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const Team = require('./Team');
66
const Application = require('./interfaces/Application');
77
const ApplicationCommandManager = require('../managers/ApplicationCommandManager');
88
const ApplicationFlagsBitField = require('../util/ApplicationFlagsBitField');
9+
const DataResolver = require('../util/DataResolver');
910
const PermissionsBitField = require('../util/PermissionsBitField');
1011

1112
/**
@@ -119,6 +120,16 @@ class ClientApplication extends Application {
119120
this.botRequireCodeGrant ??= null;
120121
}
121122

123+
if ('bot' in data) {
124+
/**
125+
* The bot associated with this application.
126+
* @type {?User}
127+
*/
128+
this.bot = this.client.users._add(data.bot);
129+
} else {
130+
this.bot ??= null;
131+
}
132+
122133
if ('bot_public' in data) {
123134
/**
124135
* If this application's bot is public
@@ -129,6 +140,16 @@ class ClientApplication extends Application {
129140
this.botPublic ??= null;
130141
}
131142

143+
if ('interactions_endpoint_url' in data) {
144+
/**
145+
* This application's interaction endpoint URL.
146+
* @type {?string}
147+
*/
148+
this.interactionsEndpointURL = data.interactions_endpoint_url;
149+
} else {
150+
this.interactionsEndpointURL ??= null;
151+
}
152+
132153
if ('role_connections_verification_url' in data) {
133154
/**
134155
* This application's role connection verification entry point URL
@@ -168,6 +189,55 @@ class ClientApplication extends Application {
168189
return !this.name;
169190
}
170191

192+
/**
193+
* Options used for editing an application.
194+
* @typedef {Object} ClientApplicationEditOptions
195+
* @property {string} [customInstallURL] The application's custom installation URL
196+
* @property {string} [description] The application's description
197+
* @property {string} [roleConnectionsVerificationURL] The application's role connection verification URL
198+
* @property {ClientApplicationInstallParams} [installParams]
199+
* Settings for the application's default in-app authorization
200+
* @property {ApplicationFlagsResolvable} [flags] The flags for the application
201+
* @property {?(BufferResolvable|Base64Resolvable)} [icon] The application's icon
202+
* @property {?(BufferResolvable|Base64Resolvable)} [coverImage] The application's cover image
203+
* @property {string} [interactionsEndpointURL] The application's interaction endpoint URL
204+
* @property {string[]} [tags] The application's tags
205+
*/
206+
207+
/**
208+
* Edits this application.
209+
* @param {ClientApplicationEditOptions} [options] The options for editing this application
210+
* @returns {Promise<ClientApplication>}
211+
*/
212+
async edit({
213+
customInstallURL,
214+
description,
215+
roleConnectionsVerificationURL,
216+
installParams,
217+
flags,
218+
icon,
219+
coverImage,
220+
interactionsEndpointURL,
221+
tags,
222+
} = {}) {
223+
const data = await this.client.rest.patch(Routes.currentApplication(), {
224+
body: {
225+
custom_install_url: customInstallURL,
226+
description,
227+
role_connections_verification_url: roleConnectionsVerificationURL,
228+
install_params: installParams,
229+
flags: flags === undefined ? undefined : ApplicationFlagsBitField.resolve(flags),
230+
icon: icon && (await DataResolver.resolveImage(icon)),
231+
cover_image: coverImage && (await DataResolver.resolveImage(coverImage)),
232+
interactions_endpoint_url: interactionsEndpointURL,
233+
tags,
234+
},
235+
});
236+
237+
this._patch(data);
238+
return this;
239+
}
240+
171241
/**
172242
* Obtains this application from Discord.
173243
* @returns {Promise<ClientApplication>}

packages/discord.js/src/util/ApplicationFlagsBitField.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,19 @@ class ApplicationFlagsBitField extends BitField {
2323
* @param {BitFieldResolvable} [bits=0] Bit(s) to read from
2424
*/
2525

26+
/**
27+
* Bitfield of the packed bits
28+
* @type {number}
29+
* @name ApplicationFlagsBitField#bitfield
30+
*/
31+
32+
/**
33+
* Data that can be resolved to give an application flag bit field. This can be:
34+
* * A string (see {@link ApplicationFlagsBitField.Flags})
35+
* * An application flag
36+
* * An instance of ApplicationFlagsBitField
37+
* * An Array of ApplicationFlagsResolvable
38+
* @typedef {string|number|ApplicationFlagsBitField|ApplicationFlagsResolvable[]} ApplicationFlagsResolvable
39+
*/
40+
2641
module.exports = ApplicationFlagsBitField;

packages/discord.js/typings/index.d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,8 @@ export class ApplicationFlagsBitField extends BitField<ApplicationFlagsString> {
496496
public static resolve(bit?: BitFieldResolvable<ApplicationFlagsString, number>): number;
497497
}
498498

499+
export type ApplicationFlagsResolvable = BitFieldResolvable<ApplicationFlagsString, number>;
500+
499501
export type AutoModerationRuleResolvable = AutoModerationRule | Snowflake;
500502

501503
export abstract class Base {
@@ -1019,6 +1021,7 @@ export class ClientApplication extends Application {
10191021
private constructor(client: Client<true>, data: RawClientApplicationData);
10201022
public botPublic: boolean | null;
10211023
public botRequireCodeGrant: boolean | null;
1024+
public bot: User | null;
10221025
public commands: ApplicationCommandManager;
10231026
public guildId: Snowflake | null;
10241027
public get guild(): Guild | null;
@@ -1030,8 +1033,10 @@ export class ClientApplication extends Application {
10301033
public customInstallURL: string | null;
10311034
public owner: User | Team | null;
10321035
public get partial(): boolean;
1036+
public interactionsEndpointURL: string | null;
10331037
public roleConnectionsVerificationURL: string | null;
10341038
public rpcOrigins: string[];
1039+
public edit(optins: ClientApplicationEditOptions): Promise<ClientApplication>;
10351040
public fetch(): Promise<ClientApplication>;
10361041
public fetchRoleConnectionMetadataRecords(): Promise<ApplicationRoleConnectionMetadata[]>;
10371042
public editRoleConnectionMetadataRecords(
@@ -6518,6 +6523,18 @@ export interface WelcomeScreenEditOptions {
65186523
welcomeChannels?: WelcomeChannelData[];
65196524
}
65206525

6526+
export interface ClientApplicationEditOptions {
6527+
customInstallURL?: string;
6528+
description?: string;
6529+
roleConnectionsVerificationURL?: string;
6530+
installParams?: ClientApplicationInstallParams;
6531+
flags?: ApplicationFlagsResolvable;
6532+
icon?: BufferResolvable | Base64Resolvable | null;
6533+
coverImage?: BufferResolvable | Base64Resolvable | null;
6534+
interactionsEndpointURL?: string;
6535+
tags?: readonly string[];
6536+
}
6537+
65216538
export interface ClientApplicationInstallParams {
65226539
scopes: OAuth2Scopes[];
65236540
permissions: Readonly<PermissionsBitField>;

0 commit comments

Comments
 (0)