forked from keybase/keybase-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam-inviter.js
executable file
·61 lines (51 loc) · 1.73 KB
/
team-inviter.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
61
#!/usr/bin/env node
const Bot = require('../../lib/index.js')
const bot = new Bot()
const teamName = process.env.KB_TEAM
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(`Bot initialized with username ${info.username}.`)
console.log(`Listening for all messages...`)
await bot.chat.watchAllChannelsForNewMessages(
async msg => {
try {
console.log(msg)
if (msg.channel.membersType !== 'impteamnative' && msg.channel.membersType !== 'impteamupgrade') {
// Only react to direct messages.
console.log(`Invalid type - ${msg.channel.membersType}`)
return
}
if (msg.content.type === 'text' && msg.content.text.body.toLowerCase().includes('invite me')) {
await bot.team.addMembers({
team: teamName,
usernames: [{username: msg.sender.username, role: 'reader'}],
})
await bot.chat.send(msg.channel, {
body: `There you go! Welcome to ${teamName}!`,
})
return
}
await bot.chat.send(msg.channel, {
body: `Hi ${msg.sender.username}! I'm a proof of concept of a bot that invites users into an open team - in this case ${teamName}. Reply with "invite me" and I'll add you there!`,
})
} catch (err) {
console.error(err)
}
},
e => console.error(e)
)
} catch (error) {
console.error(error)
}
}
async function shutDown() {
await bot.deinit()
process.exit()
}
process.on('SIGINT', shutDown)
process.on('SIGTERM', shutDown)
main()