vitalyavolyn / node-vk-bot

Create and control VK bots easily.
MIT License
34 stars 11 forks source link

Pass `reply` function to callbacks as an argument #44

Closed vitalyavolyn closed 6 years ago

vitalyavolyn commented 6 years ago

Right now, if command module is in another file, sending messages works like this

// index.ts
import module from './modules/hello'

const bot =  new Bot({...})
export default bot

bot.get(module.regex, module.callback)

// modules/hello.ts

import bot from '../'
export const regex = /Hi/i
export function callback (msg, exec) {
  bot.send('Hi', msg.peer_id)
}

There's a way to make it shorter - pass a reply fn that sends text to peer_id of the message

// index.ts
import module from './modules/hello'

const bot =  new Bot({...})

bot.get(module.regex, module.callback)

// modules/hello.ts
export const regex = /Hi/i
export function callback (msg, exec, reply) {
  reply('Hi')
}