Skip to content

Commit

Permalink
yee add bet
Browse files Browse the repository at this point in the history
  • Loading branch information
chee committed May 31, 2017
0 parents commit 33cbe80
Show file tree
Hide file tree
Showing 3 changed files with 1,547 additions and 0 deletions.
45 changes: 45 additions & 0 deletions index.js
@@ -0,0 +1,45 @@
require('dotenv').config()

const EVENT_MESSAGE = 'message'
const TYPE_MESSAGE = 'message'

const MSG_CLEAR = /clear\s+orders/
const MSG_ORDER = /(?:no,?\s+)?order\s+me\s+(.*)/
const MSG_LIST = /show\s+(?:all\s+)orders/

const REPLY_CLEAR = '⚡! all gone'
const REPLY_ORDER = '📝 got it: '

const replyClear = () => '⚡ all gone'
const replyOrder = order => `📝 got it (${order})`
const replyList = orders => Object.entries(orders).map(([user, order], index) => `${index}. ${order} (by ${user})`).join('\n')

let orders = {}

const clear = () => { orders = {} }
const add = (user, order) => { orders[user] = order }

const SlackBot = require('slackbots')

const bot = new SlackBot({
token: process.env.token,
name: 'fneh'
})

bot.on(EVENT_MESSAGE, data => {
if (data.type !== TYPE_MESSAGE) return
let match
if (match = data.text.match(MSG_CLEAR)) {
clear()
bot.postMessage(data.channel, replyClear())
} else if (match = data.text.match(MSG_ORDER)) {
const order = match[1]
bot.getUsers().then(({members}) => {
const member = members.find(member => member.id === data.user)
add(member.real_name || member.name, order)
bot.postMessage(data.channel, replyOrder(order))
})
} else if (match = data.text.match(MSG_LIST)) {
bot.postMessage(data.channel, replyList(orders))
}
})

0 comments on commit 33cbe80

Please sign in to comment.