# EventHandler

`class` in `gateway` · v0.1.0-next.3

<https://docs.seedcord.org/packages/gateway/0.1.0-next.3/classes/event-handler>

Base class for a Discord client event handler.

Pass the event name(s) as the generic, the same one(s) as `@RegisterEvent`. A single-event handler reads `this.event` (the payload tuple) directly. A handler registered for several events branches with `this.match`, keyed by event name, since the union of payload tuples is not directly readable.

```ts
abstract class EventHandler<
    Names extends ValidNonInteractionKeys
> extends BaseHandler<ClientEvents[Names]>
```

## Examples

```ts
\@RegisterEvent([Events.MessageCreate], [Events.MessageUpdate])
class PingPong extends EventHandler<Events.MessageCreate | Events.MessageUpdate> {
    async execute() {
        await this.match({
            [Events.MessageCreate]: (message) => message.reply('pong'),
            [Events.MessageUpdate]: (_oldMessage, edited) => edited.reply('pong')
        });
    }
}
```

## Constructors

### constructor

```ts
EventHandler(
    event: ClientEvents[Names],
    core: Core,
    eventName?: Names
)
```

Constructs a new instance of the `EventHandler` class

## Properties

### event

```ts
protected readonly event: SingleEventPayload<Names>
```

## Methods

### match

```ts
protected async match<Ret>(
    arms: EventMatchArms<Names, Ret>
): Promise<Ret>
```

Run the arm for whichever event fired. Use this only when the handler is registered for several events. A single-event handler reads `this.event` directly. On a multi-event handler `this.event` is `never`, so match is the only way to read the payload.

Provide one arm per registered event, keyed by its name, and each arm receives that event's own payload tuple. The arms must cover every event in the generic, a missing event or an unknown key is a compile error.
