Skip to main content

Function: ApplyOptions()

ApplyOptions<T>(optionsOrFn: T | (parameters: ApplyOptionsCallbackParameters) => T): ClassDecorator

Decorator function that applies given options to any Sapphire piece

Type parameters

Type parameter
T extends PieceOptions

Parameters

ParameterTypeDescription
optionsOrFnT | (parameters: ApplyOptionsCallbackParameters) => TThe options or function that returns options to pass to the piece constructor

Returns

ClassDecorator

Examples

import { ApplyOptions } from '@sapphire/decorators';
import { Command } from '@sapphire/framework';
import type { Message } from 'discord.js';

@ApplyOptions<Command.Options>({
description: 'ping pong',
enabled: true
})
export class UserCommand extends Command {
public override async messageRun(message: Message) {
const msg = await message.channel.send('Ping?');

return msg.edit(
`Pong! Client Latency ${Math.round(this.container.client.ws.ping)}ms. API Latency ${msg.createdTimestamp - message.createdTimestamp}ms.`
);
}
}
import { ApplyOptions } from '@sapphire/decorators';
import { Listener } from '@sapphire/framework';
import { GatewayDispatchEvents, GatewayMessageDeleteDispatch } from 'discord.js';

@ApplyOptions<Listener.Options>(({ container }) => ({
description: 'Handle Raw Message Delete events',
emitter: container.client.ws,
event: GatewayDispatchEvents.MessageDelete
}))
export class UserListener extends Listener {
public override run(data: GatewayMessageDeleteDispatch['d']): void {
if (!data.guild_id) return;

const guild = this.container.client.guilds.cache.get(data.guild_id);
if (!guild || !guild.channels.cache.has(data.channel_id)) return;

// Do something with the data
}
}

Source

piece-decorators.ts:52