Multi-Bot Server
Execute múltiplos bots Discord em um único servidor HTTP
Por que usar?
- • Uma porta - Mais fácil de configurar ngrok/proxy
- • Menos recursos - Um único processo Node.js
- • API limpa - Adicione e remova bots dinamicamente
Instalação
bash
npm install @discord-flow/multibotsComo Funciona
O Discord envia o application_id em toda interação. O servidor identifica automaticamente qual bot deve processar a requisição.
Exemplo Básico
typescript
1import { FlowRegistry, FlowEngine, MemoryStore } from 'discord-flow';2import { createMultiBotServer } from '@discord-flow/multibots';3 4// Bot 15const registry1 = new FlowRegistry();6const engine1 = new FlowEngine(registry1, new MemoryStore());7 8registry1.define('bot1', (flow) => {9 flow.start('idle');10 flow.state('idle')11 .on.command({ name: 'ping1', description: 'Ping Bot 1' }, () => ({12 response: { content: 'Pong from Bot 1!' }13 }));14});15 16// Bot 217const registry2 = new FlowRegistry();18const engine2 = new FlowEngine(registry2, new MemoryStore());19 20registry2.define('bot2', (flow) => {21 flow.start('idle');22 flow.state('idle')23 .on.command({ name: 'ping2', description: 'Ping Bot 2' }, () => ({24 response: { content: 'Pong from Bot 2!' }25 }));26});27 28// Servidor único para ambos os bots29createMultiBotServer({30 port: 3000,31 bots: [32 {33 applicationId: process.env.BOT1_APP_ID,34 publicKey: process.env.BOT1_PUBLIC_KEY,35 token: process.env.BOT1_TOKEN,36 engine: engine137 },38 {39 applicationId: process.env.BOT2_APP_ID,40 publicKey: process.env.BOT2_PUBLIC_KEY,41 token: process.env.BOT2_TOKEN,42 engine: engine243 }44 ]45}).start();Configuração Discord
Ambos os bots devem apontar para o mesmo endpoint:
bash
1# No Discord Developer Portal, configure:2# Bot 1 → Interactions Endpoint URL: https://seu-dominio.com/interactions3# Bot 2 → Interactions Endpoint URL: https://seu-dominio.com/interactionsAPI
createMultiBotServer(options)
Cria o servidor multi-bot.
typescript
1interface MultiBotServerOptions {2 port?: number; // Porta(padrão: 3000)3 bots: BotConfig[]; // Lista de bots4 autoRegisterCommands?: boolean; // Auto-registrar comandos(padrão: true)5}6 7interface BotConfig {8 applicationId: string; // ID da aplicação Discord9 publicKey: string; // Public Key para verificação10 token: string; // Token do bot11 engine: FlowEngine; // Engine com os flows do bot12 guildId?: string; // Guild ID para comandos de teste13}Métodos do Servidor
typescript
1const server = createMultiBotServer({ ... });2 3// Iniciar o servidor4await server.start();5 6// Adicionar bot dinamicamente7server.addBot({8 applicationId: 'NEW_APP_ID',9 publicKey: '...',10 token: '...',11 engine: newEngine12});13 14// Remover bot15server.removeBot('APP_ID_TO_REMOVE');16 17// Listar bots registrados18const bots = server.getBots();Variáveis de Ambiente
bash
1# Bot 12BOT1_APP_ID=1234567890123456783BOT1_PUBLIC_KEY=abc123...4BOT1_TOKEN=MTIz...5 6# Bot 27BOT2_APP_ID=9876543210987654328BOT2_PUBLIC_KEY=def456...9BOT2_TOKEN=OTg3...10 11# Opcional: Guild para testes12GUILD_ID=111222333444555666