FlowEngine

Processa interações e gerencia estados

Importação

typescript
import { FlowEngine, FlowRegistry, MemoryStore } from 'discord-flow';

Construtor

typescript
1const registry = new FlowRegistry();
2const store = new MemoryStore();
3const engine = new FlowEngine(registry, store);

Parâmetros

registry

Instância de FlowRegistry com flows definidos

store

Instância de Store para persistência de estados

Métodos

process()

Processa um evento de interação e retorna a resposta.

typescript
1engine.process(event: FlowEvent): Promise
2
3interface FlowEvent {
4 type: 'command' | 'button' | 'modal' | 'select';
5 key: string;
6 interaction: DiscordInteraction;
7}
8
9// Exemplo
10const response = await engine.process({
11 type: 'command',
12 key: 'ping',
13 interaction: discordInteraction
14});

getRegistry()

Retorna o registry associado ao engine.

typescript
1engine.getRegistry(): FlowRegistry
2
3// Exemplo
4const registry = engine.getRegistry();
5const commands = registry.getCommands();

Persistência (Stores)

O engine aceita diferentes implementações de store para salvar o estado dos usuários. Todos os stores suportam TTL (Time To Live) para expiração automática.

MemoryStore

Para desenvolvimento. Agora suporta TTL.

typescript
1import { MemoryStore } from '@discord-flow/store';
2
3const store = new MemoryStore({
4 ttl: 3600, // expira em 1 hora
5 cleanupInterval: 60 // limpa a cada 60s
6});

PostgresStore

typescript
1import { Pool } from 'pg';
2import { PostgresStore } from '@discord-flow/store';
3
4const pool = new Pool({ ... });
5
6// Criar tabela(executar uma vez)
7await PostgresStore.createTable(pool);
8
9const store = new PostgresStore(pool, {
10 tableName: 'bot_states',
11 ttl: 86400 // 24 horas
12});

SQLiteStore

typescript
1import Database from 'better-sqlite3';
2import { SQLiteStore } from '@discord-flow/store';
3
4const db = new Database('bot.db');
5SQLiteStore.createTable(db);
6
7const store = new SQLiteStore(db);

RedisStore

typescript
1import { createClient } from 'redis';
2import { RedisStore } from '@discord-flow/store';
3
4const client = createClient();
5const store = new RedisStore(client, {
6 ttl: 3600
7});

Migrações

Use o MigrationRunner para gerenciar mudanças de esquema nos dados dos estados.

typescript
1import { MigrationRunner } from '@discord-flow/store';
2
3const runner = new MigrationRunner(store);
4
5runner.register({
6 version: 1,
7 name: 'add_user_preferences',
8 up: async (store) => {
9 // Lógica de migração...
10 },
11 down: async (store) => {
12 // Rollback...
13 }
14});
15
16await runner.run();