Skip to content

Commit

Permalink
feat: entity select types (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
TTtie authored Oct 14, 2022
1 parent 75d58b7 commit 28f5a87
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 45 deletions.
78 changes: 38 additions & 40 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ declare namespace Dysnomia {

// Message
type ActionRowComponents = Button | SelectMenu;
type BaseSelectMenuTypes = Exclude<SelectMenuTypes, SelectMenuExtendedTypes>;
type Button = InteractionButton | URLButton;
type ButtonStyles = Constants["ButtonStyles"][keyof Constants["ButtonStyles"]];
type ButtonStyleNormal = Exclude<ButtonStyles, ButtonStyleLink>;
Expand All @@ -158,6 +159,10 @@ declare namespace Dysnomia {
type MessageContentEdit = string | AdvancedMessageContentEdit;
type MFALevel = Constants["MFALevels"][keyof Constants["MFALevels"]];
type PossiblyUncachedMessage = Message | { channel: TextableChannel | { id: string; guild?: Uncached }; guildID?: string; id: string };
type SelectMenu = BaseSelectMenu | ChannelSelectMenu | StringSelectMenu;
type SelectMenuTypes = Constants["ComponentTypes"][keyof Pick<Constants["ComponentTypes"], "STRING_SELECT" | "USER_SELECT" | "ROLE_SELECT" | "MENTIONABLE_SELECT" | "CHANNEL_SELECT">];
type SelectMenuExtendedTypes = Constants["ComponentTypes"][keyof Pick<Constants["ComponentTypes"], "STRING_SELECT" | "CHANNEL_SELECT">];


// Permission
type PermissionType = Constants["PermissionOverwriteTypes"][keyof Constants["PermissionOverwriteTypes"]];
Expand Down Expand Up @@ -1126,12 +1131,9 @@ declare namespace Dysnomia {
resolved?: CommandInteractionResolvedData;
options?: InteractionDataOptions[];
}
interface CommandInteractionResolvedData {
channels?: Collection<AnyChannel>;
members?: Collection<Member>;
interface CommandInteractionResolvedData extends InteractionResolvedData {
attachments?: Collection<Attachment>;
messages?: Collection<Message>;
roles?: Collection<Role>;
users?: Collection<User>;
}

interface ComponentInteractionButtonData {
Expand All @@ -1140,36 +1142,10 @@ declare namespace Dysnomia {
}

interface ComponentInteractionSelectMenuData {
component_type: Constants["ComponentTypes"]["SELECT_MENU"];
custom_id: string;
values: string[];
}
interface InteractionAutocomplete {
choices: ApplicationCommandOptionsChoice[];
}
interface CommandInteractionData {
id: string;
name: string;
type: ApplicationCommandTypes;
target_id?: string;
resolved?: CommandInteractionResolvedData;
options?: InteractionDataOptions[];
}
interface CommandInteractionResolvedData {
channels?: Collection<AnyChannel>;
members?: Collection<Member>;
messages?: Collection<Message>;
roles?: Collection<Role>;
users?: Collection<User>;
}
interface InteractionComponentButtonData {
component_type: Constants["ComponentTypes"]["BUTTON"];
custom_id: string;
}
interface InteractionComponentSelectMenuData {
component_type: Constants["ComponentTypes"]["SELECT_MENU"];
component_type: SelectMenuTypes;
custom_id: string;
values: string[];
resolved?: InteractionResolvedData;
}
interface InteractionDataOptionsBase<T extends ApplicationCommandOptionsTypes, V = unknown> {
focused?: T extends ApplicationCommandOptionsTypesWithAutocomplete ? boolean : never;
Expand All @@ -1189,6 +1165,12 @@ declare namespace Dysnomia {
custom_id: string;
components: ModalContentActionRow[];
}
interface InteractionResolvedData {
channels?: Collection<AnyChannel>;
members?: Collection<Member>;
roles?: Collection<Role>;
users?: Collection<User>;
}
interface InteractionResponseAutocomplete {
data: ApplicationCommandOptionsChoice[];
type: Constants["InteractionResponseTypes"]["APPLICATION_COMMAND_AUTOCOMPLETE_RESULT"];
Expand Down Expand Up @@ -1316,6 +1298,10 @@ declare namespace Dysnomia {
label?: string;
type: Constants["ComponentTypes"]["BUTTON"];
}
interface ChannelSelectMenu extends SelectMenuBase {
channel_types?: GuildChannelTypes[];
type: Constants["ComponentTypes"]["CHANNEL_SELECT"];
}
interface CreateStickerOptions extends Required<Pick<EditStickerOptions, "name" | "tags" | "description">> {
file: FileContent;
}
Expand All @@ -1324,14 +1310,20 @@ declare namespace Dysnomia {
name?: string;
tags?: string;
}
interface SelectMenu {
interface BaseSelectMenu extends SelectMenuBase {
type: BaseSelectMenuTypes;
}
interface SelectMenuBase {
custom_id: string;
disabled?: boolean;
max_values?: number;
min_values?: number;
options: SelectMenuOptions[];
placeholder?: string;
type: Constants["ComponentTypes"]["SELECT_MENU"];
type: SelectMenuTypes;
}
interface StringSelectMenu extends SelectMenuBase {
options: SelectMenuOptions[];
type: Constants["ComponentTypes"]["STRING_SELECT"];
}
interface SelectMenuOptions {
default?: boolean;
Expand Down Expand Up @@ -1873,10 +1865,16 @@ declare namespace Dysnomia {
GUILD_STAGE_VOICE: 13;
};
ComponentTypes: {
ACTION_ROW: 1;
BUTTON: 2;
SELECT_MENU: 3;
TEXT_INPUT: 4;
ACTION_ROW: 1;
BUTTON: 2;
STRING_SELECT: 3;
/** @deprecated */
SELECT_MENU: 3;
TEXT_INPUT: 4;
USER_SELECT: 5;
ROLE_SELECT: 6;
MENTIONABLE_SELECT: 7;
CHANNEL_SELECT: 8;
};
ConnectionVisibilityTypes: {
NONE: 0;
Expand Down
12 changes: 8 additions & 4 deletions lib/Constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,14 @@ module.exports.ChannelTypes = {
};

module.exports.ComponentTypes = {
ACTION_ROW: 1,
BUTTON: 2,
SELECT_MENU: 3,
TEXT_INPUT: 4
ACTION_ROW: 1,
BUTTON: 2,
STRING_SELECT: 3, SELECT_MENU: 3, // [DEPRECATED]
TEXT_INPUT: 4,
USER_SELECT: 5,
ROLE_SELECT: 6,
MENTIONABLE_SELECT: 7,
CHANNEL_SELECT: 8
};

module.exports.ConnectionVisibilityTypes = {
Expand Down
54 changes: 53 additions & 1 deletion lib/structures/ComponentInteraction.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
"use strict";

const Interaction = require("./Interaction");
const Member = require("./Member");
const User = require("./User");
const Role = require("./Role");
const Channel = require("./Channel");
const Message = require("./Message");
const Collection = require("../util/Collection");
const Permission = require("./Permission");
const {InteractionResponseTypes} = require("../Constants");

Expand All @@ -13,6 +18,11 @@ const {InteractionResponseTypes} = require("../Constants");
* @prop {Object} data The data attached to the interaction
* @prop {Number} data.component_type The type of Message Component
* @prop {String} data.custom_id The ID of the Message Component
* @prop {Object?} data.resolved resolved objects within the interaction (e.x. the user for a user option) (select menus only)
* @prop {Collection<Channel>?} data.resolved.channels resolved channels
* @prop {Collection<Member>?} data.resolved.members resolved members
* @prop {Collection<Role>?} data.resolved.roles resolved roles
* @prop {Collection<User>?} data.resolved.users resolved users
* @prop {Array<String>?} data.values The value of the run selected options (Select Menus Only)
* @prop {String?} guildID The ID of the guild in which the interaction was created
* @prop {Member?} member The member who triggered the interaction (This is only sent when the interaction is invoked within a guild)
Expand All @@ -27,7 +37,49 @@ class ComponentInteraction extends Interaction {
id: data.channel_id
};

this.data = data.data;
this.data = JSON.parse(JSON.stringify(data.data));

if(data.data.resolved !== undefined) {
//Users
if(data.data.resolved.users !== undefined) {
const usermap = new Collection(User);
Object.entries(data.data.resolved.users).forEach(([id, user]) => {
usermap.set(id, this._client.users.update(user, client));
});
this.data.resolved.users = usermap;
}
//Members
if(data.data.resolved.members !== undefined) {
const membermap = new Collection(Member);
Object.entries(data.data.resolved.members).forEach(([id, member]) => {
member.id = id;
member.user = {id};
if(this.channel.guild) {
membermap.set(id, this.channel.guild.members.update(member, this.channel.guild));
} else {
const guild = this._client.guilds.get(data.guild_id);
membermap.set(id, guild.members.update(member, guild));
}
});
this.data.resolved.members = membermap;
}
//Roles
if(data.data.resolved.roles !== undefined) {
const rolemap = new Collection(Role);
Object.entries(data.data.resolved.roles).forEach(([id, role]) => {
rolemap.set(id, new Role(role, this.channel.guild));
});
this.data.resolved.roles = rolemap;
}
//Channels
if(data.data.resolved.channels !== undefined) {
const channelmap = new Collection(Channel);
Object.entries(data.data.resolved.channels).forEach(([id, channel]) => {
channelmap.set(id, Channel.from(channel, this._client) || new Channel(channel, this._client));
});
this.data.resolved.channels = channelmap;
}
}

if(data.guild_id !== undefined) {
this.guildID = data.guild_id;
Expand Down

0 comments on commit 28f5a87

Please sign in to comment.