# Plugin

`class` in `http` · v0.14.0

<https://seedcord.org/docs/packages/http/0.14.0/classes/plugin>

Base class for an HTTP plugin, binding `this.core` to the HTTP [`Core`](/docs/packages/http/0.14.0/interfaces/core).

To run on either transport, extend the base from `@seedcord/core/plugin`. Its `this.core` has
only the shared members.

```ts
abstract class Plugin<
    Opts extends HttpPluginOptions = { transport: "http" }
> extends Plugin<
    Opts,
    Core,
    keyof typeof HTTP_SCOPE
>
```

## Constructors

### constructor

```ts
Plugin(
    host: CoreBase,
    [spec],
    input: PluginSpecArgs<Opts, Fixed>
)
```

Constructs a new instance of the `Plugin` 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.

### 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

### 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.

### dispose

```ts
dispose?(): Promisable<void>
```

Runs during teardown, in `ShutdownPhase.Disconnect` by default. seedcord skips it when `init()`
has thrown.

When `init()` outlasts its timeout, seedcord calls this once that `init()` resolves, outside any
shutdown phase. The process must still be alive for that call to happen.

### 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
abstract init(): Promisable<void>
```

Runs in `StartupPhase.Configuration` by default.
Move it to a different phase with the [`PluginLifecycleSpec`](/docs/packages/core/0.14.0/interfaces/plugin-lifecycle-spec).

### onHmr

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

Override to reload plugin state on an HMR update.

### 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
ready?(): Promisable<void>
```

Runs in the Ready phase after every attached plugin's `init()` has resolved. The other Ready
tasks run alongside it, including the http server binding its port.

Override it when your work needs a logged-in client or a resolved application id.

### 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.
