> ## Documentation Index
> Fetch the complete documentation index at: https://illegalcord.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Events and Flux

> Subscribe to Discord Flux events using the declarative Illegalcord plugin field.

Discord state changes are dispatched through Flux. Illegalcord plugins can listen to those actions through the declarative `flux` field.

## Declarative flux

```typescript theme={null}
import { Logger } from "@utils/Logger";
import { EquicordDevs } from "@utils/constants";
import definePlugin from "@utils/types";
import type { Message } from "@vencord/discord-types";

const logger = new Logger("FluxExample");

interface MessageCreateEvent {
    message: Message;
}

export default definePlugin({
    name: "FluxExample",
    description: "Logs when a non-bot message event is observed.",
    authors: [EquicordDevs.irritably],

    flux: {
        MESSAGE_CREATE({ message }: MessageCreateEvent) {
            if (message.author?.bot) return;
            logger.debug("Observed MESSAGE_CREATE.");
        }
    }
});
```

`PluginManager` subscribes and unsubscribes declarative Flux handlers for you. It also wraps handlers and logs thrown errors.

## Common event names

| Event                | Typical use                              |
| -------------------- | ---------------------------------------- |
| `MESSAGE_CREATE`     | React to new messages.                   |
| `MESSAGE_UPDATE`     | React to message edits.                  |
| `MESSAGE_DELETE`     | React to message deletion.               |
| `CHANNEL_SELECT`     | React to channel navigation.             |
| `VOICE_STATE_UPDATE` | React to voice joins, leaves, and moves. |
| `PRESENCE_UPDATE`    | React to presence or activity changes.   |

Event payloads are internal Discord objects and can change. Type only the fields you read.

## Manual subscriptions

Use manual `FluxDispatcher.subscribe` only for scoped React effects or late conditional subscriptions. Always keep the same function reference and unsubscribe it.

```typescript theme={null}
import { FluxDispatcher } from "@webpack/common";

function onMessageCreate(event: unknown) {
    // Handle scoped event.
}

FluxDispatcher.subscribe("MESSAGE_CREATE", onMessageCreate);
FluxDispatcher.unsubscribe("MESSAGE_CREATE", onMessageCreate);
```

<Warning>
  Do not use both declarative `flux` and manual `subscribe` for the same event in the same plugin.
</Warning>
