# Tasks

`class` in `plugin-tasks` · v0.1.0-next.1

<https://seedcord.org/docs/packages/plugin-tasks/0.1.0-next.1/classes/tasks>

Runs the task classes in a folder. A task with a cron pattern runs on that schedule. Any task can
also run once at a later time through [`schedule`](/docs/packages/plugin-tasks/0.1.0-next.1/classes/tasks#schedule).

The plugin stores scheduled tasks in the bot's `config.kv`, under its attach key. With a driver that
keeps its records, a pending job is still there after a restart. A job whose time passed while the
bot was down runs as soon as the bot starts. If you rename the attach key, the plugin doesn't
read or delete the jobs stored under the old one.

```ts
class Tasks extends Plugin<{ transport: "any"; runtime: "server" }>
```

## Examples

```ts
seedcord.attach('tasks', Tasks, { path: './tasks', timezone: 'UTC' });
```

## Constructors

### constructor

```ts
Tasks(host: CoreBase, options: TasksOptions)
```

Constructs a new instance of the `Tasks` class

## Properties

### core

```ts
protected core: TCore
```

The host, typed to the transport whose `Plugin` base this class extends.

### dispatchMembers

```ts
readonly dispatchMembers?: DispatchMemberFunctions
```

Adds values that every handler can read. Each key becomes a member, like `this.tag`, and its
function returns the value. Gates and `Notice.render` read the same values on `ctx`.

Seedcord calls each function once per interaction, and once per event that has a handler.

`attach()` throws if seedcord already uses one of these keys, like `reply`, if another plugin on the
bot uses it, or if the bot attaches this plugin twice.

### id

```ts
public static readonly id
```

### kv

```ts
protected kv: Kv
```

This plugin's records in the bot's `config.kv`, stored under its attach key. Two attachments of
one plugin class each get their own records. Renaming the attach key leaves the records under
the old key behind.

### logger

```ts
protected readonly logger: Logger
```

Logs under the plugin's class name, on the channel its attach key sets.

## Methods

### cancel

```ts
public async cancel(id: string): Promise<boolean>
```

Deletes a job so it never runs.

### codegen

```ts
async codegen?(): Promise<unknown>
```

Returns data that `seedcord codegen` writes into the bot's `seedcord-gen.ts` as a type.
Read it back with `Generated<typeof YourPlugin>`. Return plain data, or classes and objects
that a project file or your plugin's package exports. The class needs a `static readonly id`.

`seedcord codegen` calls this without running `init()`. Read only what the constructor stored
and the bot's files.

### crons

```ts
public crons(): CronStatus[]
```

Every cron job and its state.

### dispose

```ts
public override async dispose(): Promise<void>
```

Stops every timer, aborts each run's `signal`, and waits for in-flight runs. The driver still holds pending jobs.

### fetch

```ts
fetch?(
    request: Request
): Promisable<Response | undefined>
```

Answers a web request sent to the bot. seedcord calls this on each plugin in attach order and
answers with the first `Response`. On an http bot, seedcord calls it for interactions too.
Return `undefined` to pass the request on. If this method throws, seedcord logs the error on
this plugin's logger and answers the request with a 500.

On an http bot, check the path before you await anything because every interaction waits for
this method. On a server, requests reach it once the Ready phase begins. It can run before
`ready()` finishes, and before an `init()` that you moved to Ready.

### init

```ts
public async init(): Promise<void>
```

Loads the task classes under `path`, then reads back the jobs that earlier runs of the bot stored.

### jobs

```ts
public jobs(): ScheduledJob[]
```

The jobs waiting to run in this process.

### nextRun

```ts
public nextRun(Task: NoDataTaskClass): Date | null
```

When a cron job runs next, or `null` while it's paused or once it doesn't have any runs left.

### onHmr

```ts
onHmr(_event: HmrUpdateEvent): Promisable<void>
```

Override to reload plugin state on an HMR update.

### pause

```ts
public pause(Task: NoDataTaskClass): void
```

Stops a cron job's schedule until [`resume`](/docs/packages/plugin-tasks/0.1.0-next.1/classes/tasks#resume). Scheduled tasks and `trigger()` still
run the task. The task is still paused after a save reloads it. After a restart, it
follows its `paused` option again.

### prepareCommands

```ts
prepareCommands?(
    commands: readonly RESTPostAPIApplicationCommandsJSONBody[]
): Promisable<void>
```

Changes commands before seedcord sends them to Discord. Write your changes onto these objects,
like setting `name_localizations`. Each command appears once, even when it deploys to several guilds.

If it throws, the deploy stops, and so do `seedcord codegen`, a Workers build, `seedcord commands deploy`,
and `seedcord commands list`.

The CLI calls this without running `init()`. Read only what the constructor stored and the bot's files.

### ready

```ts
public override ready(): void
```

Starts the cron jobs and the timers for pending jobs once the bot has logged in.

### registerCommandFiles

```ts
protected registerCommandFiles(patterns: string[]): void
```

Tells `seedcord dev` which files your `prepareCommands` reads. Saving one of them in `seedcord dev`
shows the command refresh prompt.

### registerCriticalFiles

```ts
protected registerCriticalFiles(
    patterns: string[]
): void
```

Registers critical file patterns that should trigger a full restart when changed in Dev HMR.

### rejectOptions

```ts
protected rejectOptions(reason: string): never
```

Rejects the options this plugin got.

### resume

```ts
public resume(Task: NoDataTaskClass): void
```

Starts a cron job that `pause()`, the `paused` option, or `seedcord dev` stopped. It runs at its next match
and skips the ones it missed.

### schedule

```ts
public async schedule<TTask, TIn>(
    Task: TTask,
    options: ScheduleOptions<DataOf<TTask>, TIn>
): Promise<ScheduledJob>
```

Stores a job that runs `Task` once, at the time `options` gives. If that time has passed, the job
runs right away, or in `ready()` if the bot hasn't logged in yet. Once the promise resolves, the
driver holds the job, and it's still there after a restart. It waits under `seedcord dev` unless you set `runInDev`.

If the process dies during a run, the job runs again after a restart. Write a task so a second
run of one job is safe.

### trigger

```ts
public async trigger(Task: NoDataTaskClass): Promise<void>
```

Runs a task now, outside its schedule, even while it's paused. If a `protect`ed task is
mid-run, it skips this run and calls `onOverrun()`.
