createServer
Cria um servidor HTTP para receber interações do Discord
Importação
typescript
import { createServer } from 'discord-flow';Uso Básico
typescript
1const server = createServer({2 publicKey: process.env.DISCORD_PUBLIC_KEY,3 token: process.env.DISCORD_TOKEN,4 engine5});6 7server.start();Configuração
typescript
1interface ServerConfig {2 // Obrigatórios3 publicKey: string; // Public Key do Discord4 engine: FlowEngine; // Engine configurado5 6 // Opcionais7 token?: string; // Token do bot(para auto-registro)8 port?: number; // Porta(default: 3000)9 hostname?: string; // Host(default: 'localhost')10 path?: string; // Path(default: '/interactions')11 guildId?: string; // Guild para registro(se vazio, global)12}Opções
publicKey
Public Key da aplicação Discord. Usado para verificar assinaturas.
token
Token do bot. Quando fornecido, registra comandos automaticamente.
guildId
ID do servidor. Se fornecido, registra comandos apenas neste servidor.
Retorno
typescript
1interface ServerInstance {2 start(): Promise; 3}Registro Automático
Quando você fornece o token, o servidor registra automaticamente todos os comandos definidos nos flows:
typescript
1registry.define('commands', (flow) => {2 flow.start('idle');3 4 // Este comando será registrado automaticamente5 flow.state('idle')6 .on.command({ 7 name: 'ping', 8 description: 'Responde com pong' 9 }, () => ({10 response: { content: 'Pong!' }11 }));12});13 14createServer({15 publicKey: process.env.DISCORD_PUBLIC_KEY,16 token: process.env.DISCORD_TOKEN, // Habilita auto-registro17 engine18}).start();19 20// Console:21// discord-flow22// ➜ Local: http://localhost:3000/interactions23// ✓ 1 comando registrado24// /pingDesenvolvimento com ngrok
bash
1# Terminal 1: Inicie o bot2npx tsx --env-file=.env src/main.ts3 4# Terminal 2: Inicie o ngrok5ngrok http 30006 7# Configure no Discord Developer Portal:8# Interactions Endpoint URL: https://xxxx.ngrok.io/interactions