flora is a fast and secure runtime that lets you write discord bots for your servers, with a rich TypeScript SDK, without worrying about running infrastructure. [mirror]
1---
2outline: deep
3---
4
5# Examples
6
7This page covers the common flows like prefix commands, slash commands,
8subcommands, embeds, KV usage, and deployment via the CLI.
9
10## Minimal bot
11
12```ts
13on('ready', async () => console.log('ready'))
14
15const ping = prefix({
16 name: 'ping',
17 description: 'pong',
18 run: async (ctx) => ctx.reply('pong')
19})
20
21createBot({ prefix: '!', commands: [ping] })
22```
23
24## Prefix command with args
25
26```ts
27const math = prefix({
28 name: 'add',
29 description: 'Add two numbers',
30 run: async (ctx) => {
31 const [a, b] = ctx.args.map((x) => Number(x))
32 await ctx.reply(`sum: ${a + b}`)
33 }
34})
35
36createBot({ prefix: '!', commands: [math] })
37```
38
39## Slash command
40
41```ts
42const echo = slash({
43 name: 'echo',
44 description: 'Echo text',
45 options: [{ name: 'text', description: 'Text to echo', type: 'string', required: true }],
46 run: async (ctx) => {
47 const text = ctx.options.text as string
48 await ctx.reply({ content: text, ephemeral: true })
49 }
50})
51
52createBot({ slashCommands: [echo] })
53```
54
55## Slash subcommands
56
57```ts
58const notes = slash({
59 name: 'notes',
60 description: 'Manage notes',
61 subcommands: [
62 {
63 name: 'get',
64 description: 'Get a note',
65 options: [{ name: 'key', description: 'Key', type: 'string', required: true }],
66 run: async (ctx) => {
67 const key = ctx.options.key as string
68 const store = kv.store('notes')
69 const value = await store.get(key)
70 await ctx.reply(value ?? 'missing')
71 }
72 },
73 {
74 name: 'set',
75 description: 'Set a note',
76 options: [
77 { name: 'key', description: 'Key', type: 'string', required: true },
78 { name: 'value', description: 'Value', type: 'string', required: true }
79 ],
80 run: async (ctx) => {
81 const { key, value } = ctx.options as { key: string; value: string }
82 const store = kv.store('notes')
83 await store.set(key, value)
84 await ctx.reply(`saved ${key}`)
85 }
86 }
87 ]
88})
89
90createBot({ slashCommands: [notes] })
91```
92
93## Embeds
94
95```ts
96const info = embed()
97 .setTitle('Build info')
98 .setDescription('Nightly deployment')
99 .setColor(0x3366ff)
100 .addField('Region', 'us-east', true)
101 .addField('Version', 'v0.1.0', true)
102 .toJSON()
103
104await ctx.reply({ embeds: [info] })
105```
106
107## Components
108
109```ts
110const row = actionRow().addComponents(
111 button().setCustomId('primary').setLabel('Primary').setStyle(ButtonStyle.Primary),
112 button().setCustomId('danger').setLabel('Danger').setStyle(ButtonStyle.Danger)
113)
114
115await ctx.reply({ content: 'Buttons', components: [row.toJSON()] })
116```
117
118## KV store
119
120```ts
121const prefs = kv.store('prefs')
122
123await prefs.set('color', 'green', {
124 metadata: { source: 'setup' }
125})
126
127const color = await prefs.get('color')
128await ctx.reply(`color: ${color}`)
129
130const page = await prefs.list({ prefix: 'user:', limit: 100 })
131```
132
133## Deploy with the CLI
134
1351. Save your script (for example `src/main.ts`).
1362. Login once:
137
138```bash
139flora login <token>
140```
141
1423. Deploy to a guild:
143
144```bash
145flora deploy --guild 123456789012345678 src/main.ts --root src
146```
147
1484. Check the deployment:
149
150```bash
151flora get --guild 123456789012345678
152```
153
154## Logs
155
156```bash
157flora logs --guild 123456789012345678 --limit 200
158flora logs --guild 123456789012345678 --follow
159```
160
161## KV via CLI
162
163```bash
164flora kv create-store --guild 123456789012345678 --name notes
165flora kv set --guild 123456789012345678 --store notes --key welcome "hi there"
166flora kv get --guild 123456789012345678 --store notes welcome
167flora kv list-keys --guild 123456789012345678 --store notes
168```
169
170See the [cli page](/docs/cli) for more info.