# Seedcord

`class` in `http` · v0.14.0

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

The HTTP-interactions bot on Cloudflare Workers. Default-export it from `bot.ts`. Cloudflare
calls `fetch` for every request.

The constructor stores the config. The first request that needs the bot in each isolate reads
the bot token, plus the public key, command ids, and emoji ids that `seedcord build` wrote into
the worker. If `DISCORD_BOT_TOKEN` is unset, the token comes from `BOT_TOKEN`. It loads the
handler and subscriber folders, then runs the startup tasks and every plugin's `init()` and
`ready()`. Requests that arrive meanwhile wait for it.

Under `seedcord dev` the bot starts when the CLI starts it. It reads the public key and emoji
ids from Discord and deploys your commands, the way a node bot does.

```ts
class Seedcord extends PluginHost<"http", "edge">
```

## Constructors

### constructor

```ts
Seedcord(config: HttpEdgeConfig)
```

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 the bot starts.

### bus

```ts
readonly bus: Bus
```

### config

```ts
public readonly config: HttpEdgeConfig
```

### kv

```ts
readonly kv: Kv
```

### rateLimiter

```ts
readonly rateLimiter: IRateLimiter
```

### rest

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

Discord REST client built from `bot.restOptions`, with both sweepers off.

### shutdown

```ts
public readonly shutdown: import("@seedcord/core/node").ShutdownTasks
```

### startup

```ts
public readonly startup: StartupTasks
```

Add a task that runs during startup, on the first request that starts the bot in each isolate.

### url

```ts
public 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.

## Methods

### 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
public async fetch(
    incoming: Request,
    _env: unknown,
    ctx: EngineContext
): Promise<Response>
```

Answers one web request: each plugin's `fetch` in attach order, then Discord's interactions.
When every plugin passes, the interactions engine gets a POST and anything else gets a 404.
Until you attach a plugin with `fetch`, that 404 comes back before the bot starts.

Cloudflare passes `env` and `ctx`, but this method reads only `ctx`. Import your bindings as
`env` from `cloudflare:workers`. `cf workers types` writes their types. Work that continues
after the 202 response runs under `ctx.waitUntil`.
