# Plugin

`class` in `core/plugin` · v0.6.0

<https://docs.seedcord.org/packages/core/0.6.0/classes/plugin>

Base class for a seedcord plugin. Extend it and implement `init()`. `this.core` is the running bot.

Your constructor takes the host first and passes it to `super()`. Add your own parameters after it. `attach()` types them at the call site.

```ts
abstract class Plugin<
    Opts extends PluginOptions = {},
    TCore extends CoreBase = CoreBase
>
    implements Initializeable, HmrAware
```

## Examples

```ts
class Analytics extends Plugin<{ transport: 'gateway' }> {
    constructor(
        host: CoreBase,
        private readonly apiKey: string
    ) {
        super(host, { init: { phase: StartupPhase.Login } });
    }

    public async init(): Promise<void> {
        await this.connect(this.apiKey);
    }
}

seedcord.attach('analytics', Analytics, apiKey);
```

## Constructors

### constructor

```ts
Plugin(host: CoreBase, spec?: PluginLifecycleSpec)
```

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.

### logger

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

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

## Methods

### dispose

```ts
public async dispose(): Promise<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.

### init

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

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

### onHmr

```ts
public async onHmr(_event: HmrUpdateEvent): Promise<void>
```

Override to reload plugin state on an HMR update.

### ready

```ts
public async ready(): Promise<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.

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

Throws a `SeedcordError` that contains the plugin class name and the reason.
