Primeiro Bot
Crie seu primeiro bot Discord com discord-flow
1. Configuração do Discord
- 1.1Acesse o Discord Developer Portal
- 1.2Clique em New Application
- 1.3Na aba General Information, copie o Application ID e Public Key
- 1.4Na aba Bot, clique em Reset Token e copie o token
- 1.5Na aba OAuth2, gere um link de convite com a permissão bot e applications.commands
2. Criar o arquivo .env
bash
1DISCORD_PUBLIC_KEY=cole_sua_public_key2DISCORD_TOKEN=cole_seu_token3. Criar o bot
Crie o arquivo src/main.ts:
typescript
1import { FlowRegistry, FlowEngine, MemoryStore, createServer } from 'discord-flow';2 3// Criar o registry e engine4const registry = new FlowRegistry();5const engine = new FlowEngine(registry, new MemoryStore());6 7// Definir um flow com comando8registry.define('commands', (flow) => {9 flow.start('idle');10 11 flow.state('idle')12 .on.command({ 13 name: 'ping', 14 description: 'Responde com pong!' 15 }, () => ({16 response: { content: 'Pong! 🏓' }17 }))18 .on.command({19 name: 'hello',20 description: 'Diz olá'21 }, (ctx) => ({22 response: { content: `Olá, ${ctx.user.username}!` }23 }));24});25 26// Iniciar o servidor27createServer({28 publicKey: process.env.DISCORD_PUBLIC_KEY!,29 token: process.env.DISCORD_TOKEN!,30 engine,31 port: 300032}).start();4. Configurar ngrok
Para desenvolvimento local, use ngrok para expor sua porta:
bash
1# Terminal 1 - Inicie o bot2npx tsx --env-file=.env src/main.ts3 4# Terminal 2 - Inicie o ngrok5ngrok http 30005. Configurar Interactions URL
- 5.1Copie a URL do ngrok (ex:
https://abc123.ngrok.io) - 5.2No Discord Developer Portal, vá em General Information
- 5.3Em Interactions Endpoint URL, cole:
https://abc123.ngrok.io/interactions - 5.4Clique em Save Changes
6. Testar
Agora você pode usar os comandos no Discord:
/ping- Responde com Pong!/hello- Diz olá com seu nome
Parabéns!
Você criou seu primeiro bot com discord-flow. Explore os outros conceitos como States, Handlers e Components V2 para criar bots mais complexos.