feat(builders): add attachment command option type (#7203)

This commit is contained in:
Amitoj Singh
2022-02-13 16:02:14 +03:00
committed by GitHub
parent 0af9bc841f
commit ae0f35f51d
15 changed files with 751 additions and 685 deletions

View File

@@ -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()),

View File

@@ -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>
{
/**

View File

@@ -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';

View File

@@ -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
*

View File

@@ -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 };
}
}