# Seedcord

`class` in `gateway` · v0.11.0

<https://seedcord.org/docs/packages/gateway/0.11.0/classes/seedcord>

The gateway bot host. Opens a discord.js gateway session, discovers handlers, and runs
coordinated startup and shutdown. Attach plugins with `attach()`.

```ts
class Seedcord
    extends ServerHost<"gateway">
    implements SeedcordInstance
```

## Constructors

### constructor

```ts
Seedcord(config: GatewayConfig)
```

Constructs a new instance of the `Seedcord` class

## Properties

### applicationId

```ts
public get applicationId(): string
```

The bot's Discord application id. Throws if you read it before login.

### bot

```ts
public readonly bot: Bot
```

### bus

```ts
readonly bus: Bus
```

### config

```ts
public readonly config: GatewayConfig
```

### kv

```ts
readonly kv: Kv
```

### port

```ts
get port(): number | undefined
```

The port the bot's web server listens on. `undefined` until `start()` opens it, and when the bot doesn't open one.

### rateLimiter

```ts
readonly rateLimiter: IRateLimiter
```

### rest

```ts
public readonly rest: REST
```

The discord.js client's REST client. Its token is set during the Login phase.

### shutdown

```ts
readonly shutdown: ShutdownTasks
```

Add a task that runs while the bot shuts down.

### startup

```ts
readonly startup: StartupTasks
```

Add a task that runs while the bot starts.

### url

```ts
get url(): URL
```

The address people reach your bot at, from `server.url` in your config. You only need to set
`server.url` if a plugin or your own code reads this.

### username

```ts
public get username(): string | undefined
```

The bot's discord username, populated after login.

## Methods

### [Symbol.asyncDispose]

```ts
async [Symbol.asyncDispose](): Promise<void>
```

Runs the coordinated shutdown without calling `process.exit`. A new `Seedcord` can be
constructed afterwards. `await using` calls this when its scope ends.

### attach

```ts
attach<Key, Ctor>(
    this: this,
    key: AttachKeyAssert<Key, this>,
    Plugin: AttachableCtor<Ctor, BotT, BotRt>,
    args: PluginArgs<Ctor>
): this & Attached<Key, InstanceType<Ctor>>
```

Attaches a plugin under `key`. Read the instance back as `core[key]`. `seedcord codegen`
writes the `Core` augmentation that types it there.

Put one dot in the key to nest the plugin under a group. `'services.users'` reads back as
`core.services.users`. Each name holds one plugin or one group.

Startup runs each plugin's `init()` in attach order within its phase. A node bot starts in
`start()`. An edge bot starts on its first request.

Attaching a plugin whose `transport` or `runtime` differs from this host fails to compile.
Take `CoreBase` as your constructor's first parameter. `attach()` passes it, so you don't.
A narrower one fails to compile here.

### fetch

```ts
async fetch(request: Request): Promise<Response>
```

Answers one web request: the health path, then each plugin's `fetch` in attach order. When
every plugin passes, an http bot's interactions engine gets a POST, and anything else gets a
404. Mount it in your own server when you set `server: { port: false }`, and send it
every path so plugin routes reach the bot.

Rejects with `CoreFetchBeforeStart` until `start()` is called. Until every plugin's `ready()`
finishes, the health path answers 503. Any other request waits until the Ready phase begins.
Answers 503 once shutdown begins or `start()` fails.

### start

```ts
public async start(): Promise<this>
```

Starts the bot and runs all initialization tasks
