-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathcommand-bot.ts
More file actions
38 lines (30 loc) · 975 Bytes
/
Copy pathcommand-bot.ts
File metadata and controls
38 lines (30 loc) · 975 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/**
* Slash-command router with middleware (/ping, /help, /weather).
*
* Run: bun run examples/command-bot.ts
*/
import { Client, type Middleware } from '../src/index.js'
const client = new Client({ commandPrefix: ['/', '!'] })
client.on('qr', ({ qrString }) => console.log('Scan QR:', qrString))
const loggingMiddleware: Middleware = async (ctx, next) => {
console.log(`[command] ${ctx.command} from ${ctx.senderId} args=${ctx.args.join(',')}`)
await next()
}
client.use(loggingMiddleware)
client.command('ping', async (ctx) => {
await ctx.reply('pong')
})
client.command('help|h|?', async (ctx) => {
await ctx.reply('Commands: /ping, /weather <city>, /help')
})
client.command('weather', async (ctx) => {
const city = ctx.args[0]
if (!city) {
await ctx.reply('Usage: /weather <city>')
return
}
await ctx.reply(`Weather in ${city}: sunny, 28 degrees`)
})
client.on('connect', ({ me }) => {
console.log('Command bot ready as', me.id)
})