mirror of
https://github.com/discordjs/discord.js.git
synced 2026-03-09 16:13:31 +01:00
feat(builders): add attachment command option type (#7203)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { APIActionRowComponent, ButtonStyle, ComponentType } from 'discord-api-types/v9';
|
||||
import { APIActionRowComponent, APIMessageComponent, ButtonStyle, ComponentType } from 'discord-api-types/v9';
|
||||
import { ActionRow, ButtonComponent, createComponent, SelectMenuComponent, SelectMenuOption } from '../../src';
|
||||
|
||||
describe('Action Row Components', () => {
|
||||
@@ -9,7 +9,7 @@ describe('Action Row Components', () => {
|
||||
});
|
||||
|
||||
test('GIVEN valid JSON input THEN valid JSON output is given', () => {
|
||||
const actionRowData: APIActionRowComponent = {
|
||||
const actionRowData: APIActionRowComponent<APIMessageComponent> = {
|
||||
type: ComponentType.ActionRow,
|
||||
components: [
|
||||
{
|
||||
@@ -45,7 +45,7 @@ describe('Action Row Components', () => {
|
||||
expect(() => createComponent({ type: 42, components: [] })).toThrowError();
|
||||
});
|
||||
test('GIVEN valid builder options THEN valid JSON output is given', () => {
|
||||
const rowWithButtonData: APIActionRowComponent = {
|
||||
const rowWithButtonData: APIActionRowComponent<APIMessageComponent> = {
|
||||
type: ComponentType.ActionRow,
|
||||
components: [
|
||||
{
|
||||
@@ -57,7 +57,7 @@ describe('Action Row Components', () => {
|
||||
],
|
||||
};
|
||||
|
||||
const rowWithSelectMenuData: APIActionRowComponent = {
|
||||
const rowWithSelectMenuData: APIActionRowComponent<APIMessageComponent> = {
|
||||
type: ComponentType.ActionRow,
|
||||
components: [
|
||||
{
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
SlashCommandMentionableOption,
|
||||
SlashCommandNumberOption,
|
||||
SlashCommandRoleOption,
|
||||
SlashCommandAttachmentOption,
|
||||
SlashCommandStringOption,
|
||||
SlashCommandSubcommandBuilder,
|
||||
SlashCommandSubcommandGroupBuilder,
|
||||
@@ -25,6 +26,7 @@ const getBooleanOption = () => new SlashCommandBooleanOption().setName('owo').se
|
||||
const getUserOption = () => new SlashCommandUserOption().setName('owo').setDescription('Testing 123');
|
||||
const getChannelOption = () => new SlashCommandChannelOption().setName('owo').setDescription('Testing 123');
|
||||
const getRoleOption = () => new SlashCommandRoleOption().setName('owo').setDescription('Testing 123');
|
||||
const getAttachmentOption = () => new SlashCommandAttachmentOption().setName('owo').setDescription('Testing 123');
|
||||
const getMentionableOption = () => new SlashCommandMentionableOption().setName('owo').setDescription('Testing 123');
|
||||
const getSubcommandGroup = () => new SlashCommandSubcommandGroupBuilder().setName('owo').setDescription('Testing 123');
|
||||
const getSubcommand = () => new SlashCommandSubcommandBuilder().setName('owo').setDescription('Testing 123');
|
||||
@@ -286,6 +288,8 @@ describe('Slash Commands', () => {
|
||||
|
||||
expect(() => getBuilder().addRoleOption(getRoleOption())).not.toThrowError();
|
||||
|
||||
expect(() => getBuilder().addAttachmentOption(getAttachmentOption())).not.toThrowError();
|
||||
|
||||
expect(() => getBuilder().addMentionableOption(getMentionableOption())).not.toThrowError();
|
||||
});
|
||||
|
||||
|
||||
@@ -52,27 +52,27 @@
|
||||
"homepage": "https://discord.js.org",
|
||||
"dependencies": {
|
||||
"@sindresorhus/is": "^4.4.0",
|
||||
"discord-api-types": "^0.26.1",
|
||||
"discord-api-types": "^0.27.0",
|
||||
"ts-mixer": "^6.0.0",
|
||||
"tslib": "^2.3.1",
|
||||
"zod": "^3.11.6"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.17.0",
|
||||
"@babel/plugin-proposal-decorators": "^7.17.0",
|
||||
"@babel/core": "^7.17.2",
|
||||
"@babel/plugin-proposal-decorators": "^7.17.2",
|
||||
"@babel/preset-env": "^7.16.11",
|
||||
"@babel/preset-typescript": "^7.16.5",
|
||||
"@babel/preset-typescript": "^7.16.7",
|
||||
"@discordjs/ts-docgen": "^0.3.4",
|
||||
"@types/jest": "^27.0.3",
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.10.2",
|
||||
"@typescript-eslint/parser": "^5.10.2",
|
||||
"@types/jest": "^27.4.0",
|
||||
"@types/node": "^16.11.24",
|
||||
"@typescript-eslint/eslint-plugin": "^5.11.0",
|
||||
"@typescript-eslint/parser": "^5.11.0",
|
||||
"babel-plugin-transform-typescript-metadata": "^0.3.2",
|
||||
"eslint": "^8.8.0",
|
||||
"eslint": "^8.9.0",
|
||||
"eslint-config-marine": "^9.3.2",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.7",
|
||||
"jest": "^27.5.1",
|
||||
"prettier": "^2.5.1",
|
||||
"tsup": "^5.11.13",
|
||||
"typedoc": "^0.22.11",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { type APIActionRowComponent, ComponentType } from 'discord-api-types/v9';
|
||||
import { type APIActionRowComponent, ComponentType, APIMessageComponent } from 'discord-api-types/v9';
|
||||
import type { ButtonComponent, SelectMenuComponent } from '..';
|
||||
import { Component } from './Component';
|
||||
import { createComponent } from './Components';
|
||||
@@ -12,11 +12,11 @@ export type ActionRowComponent = ButtonComponent | SelectMenuComponent;
|
||||
* Represents an action row component
|
||||
*/
|
||||
export class ActionRow<T extends ActionRowComponent = ActionRowComponent> extends Component<
|
||||
Omit<Partial<APIActionRowComponent> & { type: ComponentType.ActionRow }, 'components'>
|
||||
Omit<Partial<APIActionRowComponent<APIMessageComponent>> & { type: ComponentType.ActionRow }, 'components'>
|
||||
> {
|
||||
public readonly components: T[];
|
||||
|
||||
public constructor({ components, ...data }: Partial<APIActionRowComponent> = {}) {
|
||||
public constructor({ components, ...data }: Partial<APIActionRowComponent<APIMessageComponent>> = {}) {
|
||||
super({ type: ComponentType.ActionRow, ...data });
|
||||
this.components = (components?.map((c) => createComponent(c)) ?? []) as T[];
|
||||
}
|
||||
@@ -40,7 +40,7 @@ export class ActionRow<T extends ActionRowComponent = ActionRowComponent> extend
|
||||
return this;
|
||||
}
|
||||
|
||||
public toJSON(): APIActionRowComponent {
|
||||
public toJSON(): APIActionRowComponent<APIMessageComponent> {
|
||||
return {
|
||||
...this.data,
|
||||
components: this.components.map((component) => component.toJSON()),
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import type { JSONEncodable } from '../util/jsonEncodable';
|
||||
import type { APIBaseMessageComponent, APIMessageComponent, ComponentType } from 'discord-api-types/v9';
|
||||
import type { APIBaseComponent, APIMessageComponent, ComponentType } from 'discord-api-types/v9';
|
||||
|
||||
/**
|
||||
* Represents a discord component
|
||||
*/
|
||||
export abstract class Component<
|
||||
DataType extends Partial<APIBaseMessageComponent<ComponentType>> & {
|
||||
DataType extends Partial<APIBaseComponent<ComponentType>> & {
|
||||
type: ComponentType;
|
||||
} = APIBaseMessageComponent<ComponentType>,
|
||||
} = APIBaseComponent<ComponentType>,
|
||||
> implements JSONEncodable<APIMessageComponent>
|
||||
{
|
||||
/**
|
||||
|
||||
@@ -23,6 +23,7 @@ export * from './interactions/slashCommands/options/integer';
|
||||
export * from './interactions/slashCommands/options/mentionable';
|
||||
export * from './interactions/slashCommands/options/number';
|
||||
export * from './interactions/slashCommands/options/role';
|
||||
export * from './interactions/slashCommands/options/attachment';
|
||||
export * from './interactions/slashCommands/options/string';
|
||||
export * from './interactions/slashCommands/options/user';
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import { SlashCommandIntegerOption } from '../options/integer';
|
||||
import { SlashCommandMentionableOption } from '../options/mentionable';
|
||||
import { SlashCommandNumberOption } from '../options/number';
|
||||
import { SlashCommandRoleOption } from '../options/role';
|
||||
import { SlashCommandAttachmentOption } from '../options/attachment';
|
||||
import { SlashCommandStringOption } from '../options/string';
|
||||
import { SlashCommandUserOption } from '../options/user';
|
||||
import type { ToAPIApplicationCommandOptions } from '../SlashCommandBuilder';
|
||||
@@ -53,6 +54,17 @@ export class SharedSlashCommandOptions<ShouldOmitSubcommandFunctions = true> {
|
||||
return this._sharedAddOptionMethod(input, SlashCommandRoleOption);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an attachment option
|
||||
*
|
||||
* @param input A function that returns an option builder, or an already built builder
|
||||
*/
|
||||
public addAttachmentOption(
|
||||
input: SlashCommandAttachmentOption | ((builder: SlashCommandAttachmentOption) => SlashCommandAttachmentOption),
|
||||
) {
|
||||
return this._sharedAddOptionMethod(input, SlashCommandAttachmentOption);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a mentionable option
|
||||
*
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
import { APIApplicationCommandAttachmentOption, ApplicationCommandOptionType } from 'discord-api-types/v9';
|
||||
import { ApplicationCommandOptionBase } from '../mixins/ApplicationCommandOptionBase';
|
||||
|
||||
export class SlashCommandAttachmentOption extends ApplicationCommandOptionBase {
|
||||
public override readonly type = ApplicationCommandOptionType.Attachment as const;
|
||||
|
||||
public toJSON(): APIApplicationCommandAttachmentOption {
|
||||
this.runRequiredValidations();
|
||||
|
||||
return { ...this };
|
||||
}
|
||||
}
|
||||
@@ -47,19 +47,19 @@
|
||||
},
|
||||
"homepage": "https://discord.js.org",
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.17.0",
|
||||
"@babel/core": "^7.17.2",
|
||||
"@babel/preset-env": "^7.16.11",
|
||||
"@babel/preset-typescript": "^7.16.5",
|
||||
"@babel/preset-typescript": "^7.16.7",
|
||||
"@discordjs/ts-docgen": "^0.3.4",
|
||||
"@types/jest": "^27.0.3",
|
||||
"@types/node": "^16.11.6",
|
||||
"@typescript-eslint/eslint-plugin": "^5.10.2",
|
||||
"@typescript-eslint/parser": "^5.10.2",
|
||||
"eslint": "^8.8.0",
|
||||
"@types/jest": "^27.4.0",
|
||||
"@types/node": "^16.11.24",
|
||||
"@typescript-eslint/eslint-plugin": "^5.11.0",
|
||||
"@typescript-eslint/parser": "^5.11.0",
|
||||
"eslint": "^8.9.0",
|
||||
"eslint-config-marine": "^9.3.2",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.7",
|
||||
"jest": "^27.5.1",
|
||||
"prettier": "^2.5.1",
|
||||
"tsup": "^5.11.13",
|
||||
"typedoc": "^0.22.11",
|
||||
|
||||
@@ -52,23 +52,23 @@
|
||||
"@discordjs/rest": "workspace:^",
|
||||
"@sapphire/snowflake": "^3.1.0",
|
||||
"@types/ws": "^8.2.2",
|
||||
"discord-api-types": "^0.26.1",
|
||||
"discord-api-types": "^0.27.0",
|
||||
"node-fetch": "^2.6.7",
|
||||
"ws": "^8.4.2"
|
||||
"ws": "^8.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@discordjs/docgen": "^0.11.0",
|
||||
"@types/node": "^16.11.12",
|
||||
"@types/node": "^16.11.24",
|
||||
"dtslint": "^4.2.1",
|
||||
"eslint": "^8.8.0",
|
||||
"eslint": "^8.9.0",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-import": "^2.25.4",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"husky": "^7.0.4",
|
||||
"is-ci": "^3.0.1",
|
||||
"jest": "^27.4.7",
|
||||
"jest": "^27.5.1",
|
||||
"prettier": "^2.5.1",
|
||||
"tsd": "^0.19.0",
|
||||
"tsd": "^0.19.1",
|
||||
"tslint": "^6.1.3",
|
||||
"typescript": "^4.5.5"
|
||||
},
|
||||
|
||||
9
packages/discord.js/typings/index.d.ts
vendored
9
packages/discord.js/typings/index.d.ts
vendored
@@ -201,7 +201,7 @@ export interface ActionRowData extends BaseComponentData {
|
||||
}
|
||||
|
||||
export class ActionRow<T extends ActionRowComponent = ActionRowComponent> extends BuilderActionRow<T> {
|
||||
constructor(data?: ActionRowData | APIActionRowComponent);
|
||||
constructor(data?: ActionRowData | APIActionRowComponent<APIMessageComponent>);
|
||||
}
|
||||
|
||||
export class ActivityFlagsBitField extends BitField<ActivityFlagsString> {
|
||||
@@ -1500,6 +1500,7 @@ export interface MappedInteractionTypes<Cached extends boolean = boolean> {
|
||||
[ComponentType.Button]: ButtonInteraction<WrapBooleanCache<Cached>>;
|
||||
[ComponentType.SelectMenu]: SelectMenuInteraction<WrapBooleanCache<Cached>>;
|
||||
[ComponentType.ActionRow]: MessageComponentInteraction<WrapBooleanCache<Cached>>;
|
||||
[ComponentType.TextInput]: never;
|
||||
}
|
||||
|
||||
export class Message<Cached extends boolean = boolean> extends Base {
|
||||
@@ -1615,9 +1616,9 @@ export class MessageComponentInteraction<Cached extends CacheType = CacheType> e
|
||||
public readonly component: CacheTypeReducer<
|
||||
Cached,
|
||||
ActionRowComponent,
|
||||
Exclude<APIMessageComponent, APIActionRowComponent>,
|
||||
ActionRowComponent | Exclude<APIMessageComponent, APIActionRowComponent>,
|
||||
ActionRowComponent | Exclude<APIMessageComponent, APIActionRowComponent>
|
||||
Exclude<APIMessageComponent, APIActionRowComponent<APIMessageComponent>>,
|
||||
ActionRowComponent | Exclude<APIMessageComponent, APIActionRowComponent<APIMessageComponent>>,
|
||||
ActionRowComponent | Exclude<APIMessageComponent, APIActionRowComponent<APIMessageComponent>>
|
||||
>;
|
||||
public componentType: Exclude<ComponentType, ComponentType.ActionRow>;
|
||||
public customId: string;
|
||||
|
||||
@@ -53,27 +53,27 @@
|
||||
"@sapphire/async-queue": "^1.2.0",
|
||||
"@sapphire/snowflake": "^3.1.0",
|
||||
"@types/node-fetch": "^2.5.12",
|
||||
"discord-api-types": "^0.26.1",
|
||||
"discord-api-types": "^0.27.0",
|
||||
"form-data": "^4.0.0",
|
||||
"node-fetch": "^2.6.7",
|
||||
"tslib": "^2.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.17.0",
|
||||
"@babel/plugin-proposal-decorators": "^7.17.0",
|
||||
"@babel/core": "^7.17.2",
|
||||
"@babel/plugin-proposal-decorators": "^7.17.2",
|
||||
"@babel/preset-env": "^7.16.11",
|
||||
"@babel/preset-typescript": "^7.16.7",
|
||||
"@discordjs/ts-docgen": "^0.3.4",
|
||||
"@types/jest": "^27.4.0",
|
||||
"@typescript-eslint/eslint-plugin": "^5.10.2",
|
||||
"@typescript-eslint/parser": "^5.10.2",
|
||||
"@typescript-eslint/eslint-plugin": "^5.11.0",
|
||||
"@typescript-eslint/parser": "^5.11.0",
|
||||
"babel-plugin-const-enum": "^1.2.0",
|
||||
"babel-plugin-transform-typescript-metadata": "^0.3.2",
|
||||
"eslint": "^8.8.0",
|
||||
"eslint": "^8.9.0",
|
||||
"eslint-config-marine": "^9.3.2",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.7",
|
||||
"jest": "^27.5.1",
|
||||
"nock": "^13.2.4",
|
||||
"prettier": "^2.5.1",
|
||||
"tsup": "^5.11.13",
|
||||
|
||||
@@ -49,30 +49,30 @@
|
||||
},
|
||||
"homepage": "https://discord.js.org",
|
||||
"dependencies": {
|
||||
"@types/ws": "^8.2.0",
|
||||
"discord-api-types": "^0.26.1",
|
||||
"@types/ws": "^8.2.2",
|
||||
"discord-api-types": "^0.27.0",
|
||||
"prism-media": "^1.3.2",
|
||||
"tiny-typed-emitter": "^2.1.0",
|
||||
"tslib": "^2.3.1",
|
||||
"ws": "^8.4.2"
|
||||
"ws": "^8.5.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.17.0",
|
||||
"@babel/core": "^7.17.2",
|
||||
"@babel/preset-env": "^7.16.11",
|
||||
"@babel/preset-typescript": "^7.16.0",
|
||||
"@discordjs/ts-docgen": "^0.3.2",
|
||||
"@types/jest": "^27.0.2",
|
||||
"@types/node": "^16.11.7",
|
||||
"@typescript-eslint/eslint-plugin": "^5.10.2",
|
||||
"@typescript-eslint/parser": "^5.10.2",
|
||||
"eslint": "^8.8.0",
|
||||
"@babel/preset-typescript": "^7.16.7",
|
||||
"@discordjs/ts-docgen": "^0.3.4",
|
||||
"@types/jest": "^27.4.0",
|
||||
"@types/node": "^16.11.24",
|
||||
"@typescript-eslint/eslint-plugin": "^5.11.0",
|
||||
"@typescript-eslint/parser": "^5.11.0",
|
||||
"eslint": "^8.9.0",
|
||||
"eslint-config-marine": "^9.3.2",
|
||||
"eslint-config-prettier": "^8.3.0",
|
||||
"eslint-plugin-prettier": "^4.0.0",
|
||||
"jest": "^27.4.7",
|
||||
"jest": "^27.5.1",
|
||||
"jest-websocket-mock": "^2.3.0",
|
||||
"mock-socket": "^9.1.2",
|
||||
"prettier": "^2.4.1",
|
||||
"prettier": "^2.5.1",
|
||||
"tsup": "^5.11.13",
|
||||
"tweetnacl": "^1.0.3",
|
||||
"typedoc": "^0.22.11",
|
||||
|
||||
Reference in New Issue
Block a user