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/multibots

Como 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 1
5const 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 2
17const 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 bots
29createMultiBotServer({
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: engine1
37 },
38 {
39 applicationId: process.env.BOT2_APP_ID,
40 publicKey: process.env.BOT2_PUBLIC_KEY,
41 token: process.env.BOT2_TOKEN,
42 engine: engine2
43 }
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/interactions
3# Bot 2 → Interactions Endpoint URL: https://seu-dominio.com/interactions

API

createMultiBotServer(options)

Cria o servidor multi-bot.

typescript
1interface MultiBotServerOptions {
2 port?: number; // Porta(padrão: 3000)
3 bots: BotConfig[]; // Lista de bots
4 autoRegisterCommands?: boolean; // Auto-registrar comandos(padrão: true)
5}
6
7interface BotConfig {
8 applicationId: string; // ID da aplicação Discord
9 publicKey: string; // Public Key para verificação
10 token: string; // Token do bot
11 engine: FlowEngine; // Engine com os flows do bot
12 guildId?: string; // Guild ID para comandos de teste
13}

Métodos do Servidor

typescript
1const server = createMultiBotServer({ ... });
2
3// Iniciar o servidor
4await server.start();
5
6// Adicionar bot dinamicamente
7server.addBot({
8 applicationId: 'NEW_APP_ID',
9 publicKey: '...',
10 token: '...',
11 engine: newEngine
12});
13
14// Remover bot
15server.removeBot('APP_ID_TO_REMOVE');
16
17// Listar bots registrados
18const bots = server.getBots();

Variáveis de Ambiente

bash
1# Bot 1
2BOT1_APP_ID=123456789012345678
3BOT1_PUBLIC_KEY=abc123...
4BOT1_TOKEN=MTIz...
5
6# Bot 2
7BOT2_APP_ID=987654321098765432
8BOT2_PUBLIC_KEY=def456...
9BOT2_TOKEN=OTg3...
10
11# Opcional: Guild para testes
12GUILD_ID=111222333444555666