after Function
UNSTABLE
The after()
function is currently marked as unstable and may change
in the future.
The after()
function allows you to execute a callback after a
command has been executed. This is useful for performing actions that
should occur after the command has completed, such as logging or
cleanup tasks.
Usage
src/app/commands/ping.ts
import {
type CommandData,
type ChatInputCommand,
unstable_after as after,
} from 'commandkit';
export const command: CommandData = {};
export const chatInput: ChatInputCommand = async (ctx) => {
after(() => {
// This code will be executed after the command has been executed
// Perform any cleanup here
console.log('Command has been executed');
});
await ctx.interaction.reply({
content: 'Hello World',
ephemeral: true,
});
};
info
The after()
function will always be called, regardless of whether
the command execution was successful or not.
Cancelling the after function
You can cancel the after function by calling the cancelAfter
function with the ID of the after function.
src/app/commands/ping.ts
import {
unstable_after as after,
unstable_cancelAfter as cancelAfter,
} from 'commandkit';
export const chatInput: ChatInputCommand = async (ctx) => {
const id = after(() => {
console.log('This will run after the command has finished executing.');
});
if (something) {
cancelAfter(id);
}
};