Skip to content

Commit

Permalink
feat: linked roles (abalabahaha#19)
Browse files Browse the repository at this point in the history
* add role connection verification url into OAuthAppInfo

* Add Client#{edit,get}RoleConnectionMetadata

Interface may be subject to change

* add guild connection role tag

* add role connection metadata type enum

* add types
  • Loading branch information
TTtie authored Jan 11, 2023
1 parent a61d849 commit f388db4
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 0 deletions.
23 changes: 23 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ declare namespace Dysnomia {
MessageApplicationCommand<W> : T extends UserApplicationCommandStructure ?
UserApplicationCommand<W> : never;
type ApplicationCommandTypes = Constants["ApplicationCommandTypes"][keyof Constants["ApplicationCommandTypes"]];
type ApplicationRoleConnectionMetadataTypes = Constants["RoleConnectionMetadataTypes"][keyof Constants["RoleConnectionMetadataTypes"]];
type ChatInputApplicationCommand<W extends boolean = false> = ApplicationCommand<"CHAT_INPUT", W>;
type MessageApplicationCommand<W extends boolean = false> = ApplicationCommand<"MESSAGE", W>;
type MessageApplicationCommandStructure = ApplicationCommandStructureBase<"MESSAGE">;
Expand Down Expand Up @@ -262,6 +263,14 @@ declare namespace Dysnomia {
permission: boolean;
type: Constants["ApplicationCommandPermissionTypes"][keyof Constants["ApplicationCommandPermissionTypes"]];
}
interface ApplicationRoleConnectionMetadata {
description: string;
descriptionLocalizations?: Record<string, string>;
key: string;
name: string;
nameLocalizations?: Record<string, string>;
type: ApplicationRoleConnectionMetadataTypes;
}
interface ChatInputApplicationCommandStructure extends ApplicationCommandStructureBase<"CHAT_INPUT"> {
description: string;
descriptionLocalizations?: Record<string, string>;
Expand Down Expand Up @@ -1572,6 +1581,7 @@ declare namespace Dysnomia {
}
interface RoleTags {
bot_id?: string;
guild_connections?: true;
integration_id?: string;
premium_subscriber?: true;
}
Expand Down Expand Up @@ -1738,6 +1748,7 @@ declare namespace Dysnomia {
owner?: PartialUser;
primary_sku_id?: string;
privacy_policy_url?: string;
role_connections_verification_url?: string;
rpc_origins?: string[];
slug?: string;
/** @deprecated */
Expand Down Expand Up @@ -2235,6 +2246,16 @@ declare namespace Dysnomia {
NITRO_CLASSIC: 1;
NITRO: 2;
};
RoleConnectionMetadataTypes: {
INTEGER_LESS_THAN_OR_EQUAL: 1;
INTEGER_GREATER_THAN_OR_EQUAL: 2;
INTEGER_EQUAL: 3;
INTEGER_NOT_EQUAL: 4;
DATETIME_LESS_THAN_OR_EQUAL: 5;
DATETIME_GREATER_THAN_OR_EQUAL: 6;
BOOLEAN_EQUAL: 7;
BOOLEAN_NOT_EQUAL: 8;
};
StageInstancePrivacyLevel: {
PUBLIC: 1;
GUILD_ONLY: 2;
Expand Down Expand Up @@ -2625,6 +2646,7 @@ declare namespace Dysnomia {
editGuildWidget(guildID: string, options: Widget): Promise<Widget>;
editMessage(channelID: string, messageID: string, content: MessageContentEdit): Promise<Message>;
editRole(guildID: string, roleID: string, options: RoleOptions, reason?: string): Promise<Role>; // TODO not all options are available?
editRoleConnectionMetadata(metadata: ApplicationRoleConnectionMetadata[]): Promise<ApplicationRoleConnectionMetadata[]>;
editRolePosition(guildID: string, roleID: string, position: number): Promise<void>;
editSelf(options: EditSelfOptions): Promise<ExtendedUser>;
editStageInstance(channelID: string, options: StageInstanceOptions): Promise<StageInstance>;
Expand Down Expand Up @@ -2707,6 +2729,7 @@ declare namespace Dysnomia {
getRESTGuildStickers(guildID: string): Promise<Sticker[]>;
getRESTSticker(stickerID: string): Promise<Sticker>;
getRESTUser(userID: string): Promise<User>;
getRoleConnectionMetadata(): Promise<ApplicationRoleConnectionMetadata[]>;
getSelf(): Promise<ExtendedUser>;
getStageInstance(channelID: string): Promise<StageInstance>;
getThreadMembers(channelID: string): Promise<ThreadMember[]>;
Expand Down
29 changes: 29 additions & 0 deletions lib/Client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,23 @@ class Client extends EventEmitter {
return this.requestHandler.request("PATCH", Endpoints.GUILD_ROLE(guildID, roleID), true, options).then((role) => new Role(role, this.guilds.get(guildID)));
}

/**
* Updates the role connection metadata
* @param {Array<Object>} metadata An array of [role connection metadata objects](https://discord.com/developers/docs/resources/application-role-connection-metadata#application-role-connection-metadata-object)
* @returns {Promise<Object[]>}
*/
editRoleConnectionMetadata(metadata) {
for(const meta of metadata) {
meta.name_localizations = meta.nameLocalizations;
meta.description_localizations = meta.descriptionLocalizations;
}
return this.requestHandler.request("PUT", Endpoints.ROLE_CONNECTION_METADATA(this.application.id), true, metadata).then((metadata) => metadata.map((meta) => ({
...meta,
nameLocalizations: meta.name_localizations,
descriptionLocalizations: meta.description_localizations
})));
}

/**
* Edit a guild role's position. Note that role position numbers are highest on top and lowest at the bottom.
* @arg {String} guildID The ID of the guild the role is in
Expand Down Expand Up @@ -2803,6 +2820,18 @@ class Client extends EventEmitter {
return this.requestHandler.request("GET", Endpoints.USER(userID), true).then((user) => new User(user, this));
}

/**
* Gets the role connection metadata
* @returns {Promise<Object[]>}
*/
getRoleConnectionMetadata() {
return this.requestHandler.request("GET", Endpoints.ROLE_CONNECTION_METADATA(this.application.id), true).then((metadata) => metadata.map((meta) => ({
...meta,
nameLocalizations: meta.name_localizations,
descriptionLocalizations: meta.description_localizations
})));
}

/**
* Get properties of the bot user
* @returns {Promise<ExtendedUser>}
Expand Down
11 changes: 11 additions & 0 deletions lib/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,17 @@ module.exports.PremiumTypes = {
NITRO: 2
};

module.exports.RoleConnectionMetadataTypes = {
INTEGER_LESS_THAN_OR_EQUAL: 1,
INTEGER_GREATER_THAN_OR_EQUAL: 2,
INTEGER_EQUAL: 3,
INTEGER_NOT_EQUAL: 4,
DATETIME_LESS_THAN_OR_EQUAL: 5,
DATETIME_GREATER_THAN_OR_EQUAL: 6,
BOOLEAN_EQUAL: 7,
BOOLEAN_NOT_EQUAL: 8
};

module.exports.StageInstancePrivacyLevel = {
PUBLIC: 1,
GUILD_ONLY: 2
Expand Down
1 change: 1 addition & 0 deletions lib/rest/Endpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports.GUILDS =
module.exports.INTERACTION_RESPOND = (interactID, interactToken) => `/interactions/${interactID}/${interactToken}/callback`;
module.exports.INVITE = (inviteID) => `/invites/${inviteID}`;
module.exports.OAUTH2_APPLICATION = "/oauth2/applications/@me";
module.exports.ROLE_CONNECTION_METADATA = (applicationID) => `/applications/${applicationID}/role-connections/metadata`;
module.exports.STAGE_INSTANCE = (channelID) => `/stage-instances/${channelID}`;
module.exports.STAGE_INSTANCES = "/stage-instances";
module.exports.STICKER = (stickerID) => `/stickers/${stickerID}`;
Expand Down
3 changes: 3 additions & 0 deletions lib/structures/Role.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ class Role extends Base {
}
if(data.tags !== undefined) {
this.tags = data.tags;
if(this.tags.guild_connections === null) {
this.tags.guild_connections = true;
}
if(this.tags.premium_subscriber === null) {
this.tags.premium_subscriber = true;
}
Expand Down

0 comments on commit f388db4

Please sign in to comment.