Primeiro Bot

Crie seu primeiro bot Discord com discord-flow

1. Configuração do Discord

  1. 1.1Acesse o Discord Developer Portal
  2. 1.2Clique em New Application
  3. 1.3Na aba General Information, copie o Application ID e Public Key
  4. 1.4Na aba Bot, clique em Reset Token e copie o token
  5. 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_key
2DISCORD_TOKEN=cole_seu_token

3. Criar o bot

Crie o arquivo src/main.ts:

typescript
1import { FlowRegistry, FlowEngine, MemoryStore, createServer } from 'discord-flow';
2
3// Criar o registry e engine
4const registry = new FlowRegistry();
5const engine = new FlowEngine(registry, new MemoryStore());
6
7// Definir um flow com comando
8registry.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 servidor
27createServer({
28 publicKey: process.env.DISCORD_PUBLIC_KEY!,
29 token: process.env.DISCORD_TOKEN!,
30 engine,
31 port: 3000
32}).start();

4. Configurar ngrok

Para desenvolvimento local, use ngrok para expor sua porta:

bash
1# Terminal 1 - Inicie o bot
2npx tsx --env-file=.env src/main.ts
3
4# Terminal 2 - Inicie o ngrok
5ngrok http 3000

5. Configurar Interactions URL

  1. 5.1Copie a URL do ngrok (ex: https://abc123.ngrok.io)
  2. 5.2No Discord Developer Portal, vá em General Information
  3. 5.3Em Interactions Endpoint URL, cole: https://abc123.ngrok.io/interactions
  4. 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.