From 22e2bbb0d24e3f30516f262308d5786f2f666713 Mon Sep 17 00:00:00 2001 From: Almeida Date: Sat, 24 Dec 2022 08:00:39 +0000 Subject: [PATCH] feat(ClientApplication): add role connections (#8855) * feat(ClientApplication): add role connections * fix: constructor and edit types * fix: rename typedef Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com> --- packages/discord.js/src/index.js | 1 + .../ApplicationRoleConnectionMetadata.js | 43 ++++++++++++++++ .../src/structures/ClientApplication.js | 51 +++++++++++++++++++ packages/discord.js/src/util/APITypes.js | 5 ++ packages/discord.js/typings/index.d.ts | 26 ++++++++++ 5 files changed, 126 insertions(+) create mode 100644 packages/discord.js/src/structures/ApplicationRoleConnectionMetadata.js diff --git a/packages/discord.js/src/index.js b/packages/discord.js/src/index.js index 5a7b1f11a..93758f3ab 100644 --- a/packages/discord.js/src/index.js +++ b/packages/discord.js/src/index.js @@ -88,6 +88,7 @@ exports.Activity = require('./structures/Presence').Activity; exports.AnonymousGuild = require('./structures/AnonymousGuild'); exports.Application = require('./structures/interfaces/Application'); exports.ApplicationCommand = require('./structures/ApplicationCommand'); +exports.ApplicationRoleConnectionMetadata = require('./structures/ApplicationRoleConnectionMetadata'); exports.AutocompleteInteraction = require('./structures/AutocompleteInteraction'); exports.AutoModerationActionExecution = require('./structures/AutoModerationActionExecution'); exports.AutoModerationRule = require('./structures/AutoModerationRule'); diff --git a/packages/discord.js/src/structures/ApplicationRoleConnectionMetadata.js b/packages/discord.js/src/structures/ApplicationRoleConnectionMetadata.js new file mode 100644 index 000000000..9ea80872c --- /dev/null +++ b/packages/discord.js/src/structures/ApplicationRoleConnectionMetadata.js @@ -0,0 +1,43 @@ +'use strict'; + +class ApplicationRoleConnectionMetadata { + constructor(data) { + /** + * The name of this metadata field + * @type {string} + */ + this.name = data.name; + + /** + * The name localizations for this metadata field + * @type {?Object} + */ + this.nameLocalizations = data.name_localizations ?? null; + + /** + * The description of this metadata field + * @type {string} + */ + this.description = data.description; + + /** + * The description localizations for this metadata field + * @type {?Object} + */ + this.descriptionLocalizations = data.description_localizations ?? null; + + /** + * The dictionary key for this metadata field + * @type {string} + */ + this.key = data.key; + + /** + * The type of this metadata field + * @type {ApplicationRoleConnectionMetadataType} + */ + this.type = data.type; + } +} + +exports.ApplicationRoleConnectionMetadata = ApplicationRoleConnectionMetadata; diff --git a/packages/discord.js/src/structures/ClientApplication.js b/packages/discord.js/src/structures/ClientApplication.js index 811147cd4..afee47d3c 100644 --- a/packages/discord.js/src/structures/ClientApplication.js +++ b/packages/discord.js/src/structures/ClientApplication.js @@ -1,6 +1,7 @@ 'use strict'; const { Routes } = require('discord-api-types/v10'); +const { ApplicationRoleConnectionMetadata } = require('./ApplicationRoleConnectionMetadata'); const Team = require('./Team'); const Application = require('./interfaces/Application'); const ApplicationCommandManager = require('../managers/ApplicationCommandManager'); @@ -108,6 +109,16 @@ class ClientApplication extends Application { this.botPublic ??= null; } + if ('role_connections_verification_url' in data) { + /** + * This application's role connection verification entry point URL + * @type {?string} + */ + this.roleConnectionsVerificationURL = data.role_connections_verification_url; + } else { + this.roleConnectionsVerificationURL ??= null; + } + /** * The owner of this OAuth application * @type {?(User|Team)} @@ -137,6 +148,46 @@ class ClientApplication extends Application { this._patch(app); return this; } + + /** + * Gets this application's role connection metadata records + * @returns {Promise} + */ + async fetchRoleConnectionMetadataRecords() { + const metadata = await this.client.rest.get(Routes.applicationRoleConnectionMetadata(this.client.user.id)); + return metadata.map(data => new ApplicationRoleConnectionMetadata(data)); + } + + /** + * Data for creating or editing an application role connection metadata. + * @typedef {Object} ApplicationRoleConnectionMetadataEditOptions + * @property {string} name The name of the metadata field + * @property {?Object} [nameLocalizations] The name localizations for the metadata field + * @property {string} description The description of the metadata field + * @property {?Object} [descriptionLocalizations] The description localizations for the metadata field + * @property {string} key The dictionary key of the metadata field + * @property {ApplicationRoleConnectionMetadataType} type The type of the metadata field + */ + + /** + * Updates this application's role connection metadata records + * @param {ApplicationRoleConnectionMetadataEditOptions[]} records The new role connection metadata records + * @returns {Promise} + */ + async editRoleConnectionMetadataRecords(records) { + const newRecords = await this.client.rest.put(Routes.applicationRoleConnectionMetadata(this.client.user.id), { + body: records.map(record => ({ + type: record.type, + key: record.key, + name: record.name, + name_localizations: record.nameLocalizations, + description: record.description, + description_localizations: record.descriptionLocalizations, + })), + }); + + return newRecords.map(data => new ApplicationRoleConnectionMetadata(data)); + } } module.exports = ClientApplication; diff --git a/packages/discord.js/src/util/APITypes.js b/packages/discord.js/src/util/APITypes.js index e356c5587..18af763d2 100644 --- a/packages/discord.js/src/util/APITypes.js +++ b/packages/discord.js/src/util/APITypes.js @@ -173,6 +173,11 @@ * @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/ApplicationFlags} */ +/** + * @external ApplicationRoleConnectionMetadataType + * @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/ApplicationRoleConnectionMetadataType} + */ + /** * @external AutoModerationActionType * @see {@link https://discord-api-types.dev/api/discord-api-types-v10/enum/AutoModerationActionType} diff --git a/packages/discord.js/typings/index.d.ts b/packages/discord.js/typings/index.d.ts index 16be44dbd..85df818ac 100644 --- a/packages/discord.js/typings/index.d.ts +++ b/packages/discord.js/typings/index.d.ts @@ -150,6 +150,8 @@ import { GatewayAutoModerationActionExecutionDispatchData, APIAutoModerationRule, ForumLayoutType, + ApplicationRoleConnectionMetadataType, + APIApplicationRoleConnectionMetadata, } from 'discord-api-types/v10'; import { ChildProcess } from 'node:child_process'; import { EventEmitter } from 'node:events'; @@ -453,6 +455,16 @@ export class ApplicationCommand extends Base { private static isAPICommandData(command: object): command is RESTPostAPIApplicationCommandsJSONBody; } +export class ApplicationRoleConnectionMetadata { + private constructor(data: APIApplicationRoleConnectionMetadata); + public name: string; + public nameLocalizations: LocalizationMap | null; + public description: string; + public descriptionLocalizations: LocalizationMap | null; + public key: string; + public type: ApplicationRoleConnectionMetadataType; +} + export type ApplicationResolvable = Application | Activity | Snowflake; export class ApplicationFlagsBitField extends BitField { @@ -966,8 +978,13 @@ export class ClientApplication extends Application { public customInstallURL: string | null; public owner: User | Team | null; public get partial(): boolean; + public roleConnectionsVerificationURL: string | null; public rpcOrigins: string[]; public fetch(): Promise; + public fetchRoleConnectionMetadataRecords(): Promise; + public editRoleConnectionMetadataRecords( + records: ApplicationRoleConnectionMetadataEditOptions[], + ): Promise; } export class ClientPresence extends Presence { @@ -4480,6 +4497,15 @@ export type ApplicationCommandResolvable = ApplicationCommand | Snowflake; export type ApplicationFlagsString = keyof typeof ApplicationFlags; +export interface ApplicationRoleConnectionMetadataEditOptions { + name: string; + nameLocalizations?: LocalizationMap | null; + description: string; + descriptionLocalizations?: LocalizationMap | null; + key: string; + type: ApplicationRoleConnectionMetadataType; +} + export interface AuditLogChange { key: APIAuditLogChange['key']; old?: APIAuditLogChange['old_value'];