-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathadvertised-echo.js
60 lines (50 loc) · 1.34 KB
/
advertised-echo.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#!/usr/bin/env node
const Bot = require('../../lib/index.js')
const bot = new Bot()
async function main() {
try {
const username = process.env.KB_USERNAME
const paperkey = process.env.KB_PAPERKEY
await bot.init(username, paperkey)
const info = bot.myInfo()
console.log(`Echo bot initialized with username ${info.username}.`)
await bot.chat.clearCommands()
await bot.chat.advertiseCommands({
advertisements: [
{
type: 'public',
commands: [
{
name: 'echo',
description: 'Sends out your message to the current channel.',
usage: '[your text]',
},
],
},
],
})
const onMessage = async message => {
if (message.content.type !== 'text') {
return
}
if (!message.content.text.body.startsWith('!echo ')) {
return
}
bot.chat.send(message.conversationId, {
body: message.content.text.body.substr(6),
})
}
const onError = e => console.error(e)
console.log(`Listening for messages...`)
await bot.chat.watchAllChannelsForNewMessages(onMessage, onError)
} catch (error) {
console.error(error)
}
}
async function shutDown() {
await bot.deinit()
process.exit()
}
process.on('SIGINT', shutDown)
process.on('SIGTERM', shutDown)
main()