TaskContext
TaskContext
Execution context provided to task functions.
This class provides access to the task instance, custom data, CommandKit instance, and a temporary store for sharing data between prepare and execute functions.
Example
import { task } from '@commandkit/tasks';
export const reminderTask = task({
name: 'reminder',
async execute(ctx) {
// Access custom data passed to the task
const { userId, message } = ctx.data;
// Access CommandKit and Discord.js client
const user = await ctx.commandkit.client.users.fetch(userId);
await user.send(`Reminder: ${message}`);
// Use the store to share data between prepare and execute
const processedCount = ctx.store.get('processedCount') || 0;
ctx.store.set('processedCount', processedCount + 1);
},
});
Signature
class TaskContext<T extends Record<string, any> = Record<string, any>> {
public readonly store = new Map<string, any>();
constructor(_data: TaskContextData<T>)
task: Task
client: Client
data: T
commandkit: CommandKit
}
store
property
Temporary key-value store for sharing data between prepare and execute functions.
This store is useful for passing computed values or state between the prepare and execute phases of task execution.
Example
export const conditionalTask = task({
name: 'conditional-task',
async prepare(ctx) {
const shouldRun = await checkConditions();
ctx.store.set('shouldRun', shouldRun);
return shouldRun;
},
async execute(ctx) {
const shouldRun = ctx.store.get('shouldRun');
if (shouldRun) {
await performAction();
}
},
});
constructor
method
(_data: TaskContextData<T>) => TaskContext
Creates a new task execution context.
task
property
Gets the task instance being executed.
client
property
Client
Gets the Discord.js client.
data
property
T
Gets the custom data passed to the task execution.
commandkit
property
Gets the CommandKit instance for accessing bot functionality.
This provides access to the Discord.js client, CommandKit store, and other bot-related functionality.