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(ClientApplication): add role connections #8855

Merged
merged 4 commits into from
Dec 24, 2022
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
1 change: 1 addition & 0 deletions packages/discord.js/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Locale, string>}
*/
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<Locale, string>}
*/
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;
51 changes: 51 additions & 0 deletions packages/discord.js/src/structures/ClientApplication.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -137,6 +148,46 @@ class ClientApplication extends Application {
this._patch(app);
return this;
}

/**
* Gets this application's role connection metadata records
* @returns {Promise<ApplicationRoleConnectionMetadata[]>}
*/
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<Locale, string>} [nameLocalizations] The name localizations for the metadata field
* @property {string} description The description of the metadata field
* @property {?Object<Locale, string>} [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<ApplicationRoleConnectionMetadata[]>}
*/
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;
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 @@ -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}
Expand Down
26 changes: 26 additions & 0 deletions packages/discord.js/typings/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -453,6 +455,16 @@ export class ApplicationCommand<PermissionsFetchType = {}> 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<ApplicationFlagsString> {
Expand Down Expand Up @@ -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<ClientApplication>;
public fetchRoleConnectionMetadataRecords(): Promise<ApplicationRoleConnectionMetadata[]>;
public editRoleConnectionMetadataRecords(
records: ApplicationRoleConnectionMetadataEditOptions[],
): Promise<ApplicationRoleConnectionMetadata[]>;
}

export class ClientPresence extends Presence {
Expand Down Expand Up @@ -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'];
Expand Down