Skip to content

Commit

Permalink
Add rename slash command
Browse files Browse the repository at this point in the history
  • Loading branch information
maxwofford committed Aug 8, 2019
1 parent 13ef6e7 commit 9d5e911
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import infoInteraction from './interactions/info'
import statsInteraction from './interactions/stats'
import helloInteraction from './interactions/hello'
import triggerInteraction from './interactions/trigger'
import renameInteraction from './interactions/rename'

const controller = new Botkit.slackbot({
clientId: process.env.SLACK_CLIENT_ID,
Expand Down Expand Up @@ -92,6 +93,10 @@ controller.on('slash_command', (bot, message) => {
case '/stats':
statsInteraction(bot, message)
break

case '/rename':
renameInteraction(bot, message)
break

default:
bot.replyPrivate(message, `I don't know how to do that ¯\_(ツ)_/¯`)
Expand Down
28 changes: 28 additions & 0 deletions src/interactions/rename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { airFind } from '../utils'

const renameInteraction = (bot, message) => {
const { user, channel } = message

const callerInfo = {
clubRecord: null,
leaderRecord: null
}

Promise.all([
airFind('Leaders', 'Slack ID', user).then(l => callerInfo.leaderRecord = l),
airFind('Clubs', 'Slack Channel ID', channel).then(c => callerInfo.leaderRecord = c)
]).then(() => {
if (!callerInfo.leaderRecord) {

}

if (!callerInfo.clubRecord) {

}

console.log(callerInfo.leaderRecord.fields)
console.log(callerInfo.clubRecord.fields)

})
}
export default renameInteraction
23 changes: 23 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ import Airtable from 'airtable'

const base = new Airtable({apiKey: process.env.AIRTABLE_KEY}).base(process.env.AIRTABLE_BASE)

// usage: airFind('Club', 'Slack Channel ID', slackChannelID)
export const airFind = (baseName, fieldName, value) => new Promise((resolve, reject) => {
airGet(baseName, fieldName, value)
.then(results => resolve(results[0]))
.catch(err => reject(err))
})

export const airGet = (baseName, fieldName, value) => new Promise((resolve, reject) => {
const results = []
base(baseName).select({
filterByFormula: `{${fieldName}} = "${value}"`
}).eachPage((records, fetchNextPage) => {
records.forEach(record => results.push(record))
fetchNextPage()
}, err => {
if (err) {
console.error(err)
reject(err)
}
resolve(results)
})
})

const getLeaderFrom = user => new Promise((resolve, reject) => {
base('Leaders').select({
filterByFormula: `{Slack ID} = "${user}"`
Expand Down

0 comments on commit 9d5e911

Please sign in to comment.