Skip to main content

Class: InteractionHandler<Options>

The piece to be stored in Store instances.

Type parameters

NameType
Optionsextends Options = Options

Hierarchy

  • Piece<Options, "interaction-handlers">

    InteractionHandler

Constructors

constructor

new InteractionHandler<Options>(context, options): InteractionHandler<Options>

Type parameters

NameType
Optionsextends InteractionHandlerOptions = InteractionHandlerOptions

Parameters

NameType
contextLoaderContext
optionsOptions

Returns

InteractionHandler<Options>

Overrides

Piece.constructor

Defined in

projects/framework/src/lib/structures/InteractionHandler.ts:16

Properties

enabled

enabled: boolean

Whether or not the piece is enabled.

Inherited from

Piece.enabled

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:234


interactionHandlerType

Readonly interactionHandlerType: InteractionHandlerTypes

The type for this handler

Since

3.0.0

Defined in

projects/framework/src/lib/structures/InteractionHandler.ts:14


location

Readonly location: PieceLocation

The location metadata for the piece's file.

Inherited from

Piece.location

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:226


name

Readonly name: string

The name of the piece.

Inherited from

Piece.name

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:230


options

Readonly options: Options

The raw options passed to this Piece

Inherited from

Piece.options

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:238


store

Readonly store: InteractionHandlerStore

The store that contains the piece.

Inherited from

Piece.store

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:222

Accessors

container

get container(): Container

A reference to the Container object for ease of use.

Returns

Container

See

container

Inherited from

Piece.container

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:244

Methods

none

none(): None

Returns

None

Defined in

projects/framework/src/lib/structures/InteractionHandler.ts:77


onLoad

onLoad(): unknown

Per-piece listener that is called when the piece is loaded into the store. Useful to set-up asynchronous initialization tasks.

Returns

unknown

Inherited from

Piece.onLoad

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:249


onUnload

onUnload(): unknown

Per-piece listener that is called when the piece is unloaded from the store. Useful to set-up clean-up tasks.

Returns

unknown

Inherited from

Piece.onUnload

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:254


parse

parse(_interaction): Awaitable<Option<unknown>>

A custom function that will be called when checking if an interaction should be passed to this handler. You can use this method to not only filter by ids, but also pre-parse the data from the id for use in the run method.

By default, all interactions of the type you specified will run in a handler. You should override this method to change that behavior.

Parameters

NameType
_interactionInteraction

Returns

Awaitable<Option<unknown>>

An Option (or a Promise Promised Option) that indicates if this interaction should be handled by this handler, and any extra data that should be passed to the run method

Example

// Parsing a button handler
public override parse(interaction: ButtonInteraction) {
if (interaction.customId.startsWith('my-awesome-clicky-button')) {
// Returning a `some` here means that the run method should be called next!
return this.some({ isMyBotAwesome: true, awesomenessLevel: 9001 });
}

// Returning a `none` means this interaction shouldn't run in this handler
return this.none();
}

Example

// Getting data from a database based on the custom id
public override async parse(interaction: ButtonInteraction) {
// This code is purely for demonstration purposes only!
if (interaction.customId.startsWith('example-data')) {
const [, userId, channelId] = interaction.customId.split('.');

const dataFromDatabase = await container.prisma.exampleData.findFirst({ where: { userId, channelId } });

// Returning a `some` here means that the run method should be called next!
return this.some(dataFromDatabase);
}

// Returning a `none` means this interaction shouldn't run in this handler
return this.none();
}

Defined in

projects/framework/src/lib/structures/InteractionHandler.ts:67


reload

reload(): Promise<void>

Reloads the piece by loading the same path in the store.

Returns

Promise<void>

Inherited from

Piece.reload

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:262


run

run(interaction, parsedData?): unknown

Parameters

NameType
interactionInteraction
parsedData?unknown

Returns

unknown

Defined in

projects/framework/src/lib/structures/InteractionHandler.ts:22


some

some(): Some<never>

Returns

Some<never>

Defined in

projects/framework/src/lib/structures/InteractionHandler.ts:71

some<T>(data): Some<T>

Type parameters

Name
T

Parameters

NameType
dataT

Returns

Some<T>

Defined in

projects/framework/src/lib/structures/InteractionHandler.ts:72


toJSON

toJSON(): InteractionHandlerJSON

Defines the JSON.stringify behavior of this piece.

Returns

InteractionHandlerJSON

Overrides

Piece.toJSON

Defined in

projects/framework/src/lib/structures/InteractionHandler.ts:81


unload

unload(): Promise<void>

Unloads and disables the piece.

Returns

Promise<void>

Inherited from

Piece.unload

Defined in

node_modules/@sapphire/pieces/dist/esm/index.d.mts:258