Skip to content

Commit

Permalink
Refactor names of functions to send messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dreeves committed Sep 28, 2017
1 parent 5708d29 commit 99e5dd0
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
28 changes: 14 additions & 14 deletions lib/bid.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// --------------------------------- 80chars ---------------------------------->
const botils = require('./botils.js')
const shout = botils.shout
const whisp = botils.whisp
const shoutDelayed = botils.shoutDelayed
const randint = botils.randint
const isEmpty = botils.isEmpty
const keyfn = botils.keyfn
const holla = botils.holla
const whisp = botils.whisp
const blurt = botils.blurt
const randint = botils.randint
const isEmpty = botils.isEmpty
const keyfn = botils.keyfn

const rkey = keyfn("auctions")

Expand Down Expand Up @@ -50,17 +50,17 @@ var bidMissing = function(bids) {
return Object.keys(bids).some(function(x) { return !bids[x] })
}

// Fetches the hash of bids, h, and then shouts the string indicated by the
// Fetches the hash of bids, h, and then hollas the string indicated by the
// template, substituting $SUMMARY and $STATUS with bidSummary(h) and
// bidStatus(h), respectively.
// (The goofiness with passing in a template and substituting is that hgetall
// is asynchronous. If it were synchronous we'd just fetch the hash of bids and
// then use that to format the output when ready to output it. Instead we need
// to pass a callback function to hgetall and let that function do whatever it's
// going to do with the bid hash -- in our case shout it in the channel.)
// going to do with the bid hash -- in our case holla it in the channel.)
var bidAsyncShout = function(res, chan, template) {
redis.hgetall(rkey(chan, "bids"), function(err, obj) {
shout(res, template.replace("$SUMMARY", bidSummary(obj))
holla(res, template.replace("$SUMMARY", bidSummary(obj))
.replace("$STATUS", bidStatus(obj)))
})
}
Expand Down Expand Up @@ -95,18 +95,18 @@ var bidPay = function() {
return (r === 1 ? y : n)
}

// Add text as user's bid, shout the results if user is the last one to bid
// Add text as user's bid, holla the results if user is the last one to bid
var bidProc = function(res, chan, user, text, rurl) {
redis.hset(rkey(chan, "bids"), user, text,
function(err, obj) {
redis.hgetall(rkey(chan, "bids"),
function(err, obj) { // obj is now the hash from users to bids
whisp(res, "Got your bid: " + text)
if(bidMissing(obj)) {
shoutDelayed(rurl, "New bid from " + user + "! " + bidStatus(obj))
blurt(rurl, "New bid from " + user + "! " + bidStatus(obj))
} else {
bidReset(chan)
shoutDelayed(rurl,
blurt(rurl,
"Got final bid from " + user + "! :tada: Results:\n"
+ bidSummary(obj) + "\n\n_" + bidPay() + "_")
}
Expand Down Expand Up @@ -154,15 +154,15 @@ var handleSlash = function(req, res) {
help(res)
} else if(text === "debug") {
whisp(res, urtext + "whispered reply. obj = " + JSON.stringify(obj))
shoutDelayed(rurl, "We can also reply publicly w/out echoing the cmd!")
blurt(rurl, "We can also reply publicly w/out echoing the cmd!")
} else { // if the text is anything else then it's a normal bid
// could check if user has an old bid so we can say "Updated your bid"
bidProc(res, chan, user, text, rurl)
}
} else { //------------------------------- no active auction in this channel
if(!isEmpty(others)) { bidStart(res, chan, user, text, others) }
else if(text === "") { whisp(res, urtext + "No current auction") }
else if(text === "status") { shout(res, "No current auction") }
else if(text === "status") { holla(res, "No current auction") }
else if(text === "abort") { whisp(res, urtext + "No current auction") }
else if(text === "help") { help(res) }
else if(text === "debug") { whisp(res, urtext + "No current auction") }
Expand Down
13 changes: 9 additions & 4 deletions lib/botils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
// --------------------------------- 80chars ---------------------------------->
var request = require('request')

// There are 3 ways to send messages to the channel:
// 1. whisp: reply to the user who typed the slash command so only they see it
// 2. holla: echo the slash command publicly & reply (holla back) publicly
// 3. blurt: say something publicly & asynchronously, no echoing slash command

// Respond with string txt to everyone in the channel, echoing the slash command
var shout = (res, txt) => {
var holla = (res, txt) => {
res.send({ "response_type": "in_channel", "text": txt })
}

Expand All @@ -16,7 +21,7 @@ var whisp = (res, txt, att) => {
}

// Post string txt to everyone in the channel, no echoing of the slash command
var shoutDelayed = (rurl, txt) => {
var blurt = (rurl, txt) => {
request.post(rurl, { json: {
"response_type": "in_channel", // in_channel vs ephemeral
"text": txt}
Expand All @@ -42,9 +47,9 @@ var keyfn = (botname) => {
}

module.exports = {
shout,
holla,
whisp,
shoutDelayed,
blurt,
bern,
randint,
isEmpty,
Expand Down
8 changes: 4 additions & 4 deletions lib/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ var scheduleCharge = function(res, charge) {
var message = "Charging " + charge.user +" $"+ charge.amount +" in "+
delayMinutes + " minutes";
if (charge.reason) { message += " (" + charge.reason + ")"; }
botils.shout(res, message);
botils.holla(res, message);
}
);
}
Expand All @@ -93,7 +93,7 @@ var runCharge = function(charge, res) {
if (res && resText.match(/couldn't charge/i)) {
botils.whisp(res, resText);
} else if (res) {
botils.shout(res, resText);
botils.holla(res, resText);
}
});
}).on('error', (e) => { console.error(e); });
Expand All @@ -111,7 +111,7 @@ var list = function(res, user_id) {
message += "\n";
});
if (message === "") { message = "No pending charges for you!"; }
botils.shout(res, message);
botils.holla(res, message);
});
}

Expand All @@ -133,7 +133,7 @@ var cancel = function(res, user_id, reason) {
} else if (message === "") {
botils.whisp(res, "No pending charges matched " + reason + "!");
} else {
botils.shout(res, message);
botils.holla(res, message);
}
});
}
Expand Down
14 changes: 7 additions & 7 deletions lib/karma.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,27 @@ var handleSlash = function(req, res) {

if (text === "on") {
redis.set(rkey(team_id, "on"), 1, function(err, obj) {
botils.shout(res, "now tracking karma");
botils.holla(res, "now tracking karma");
beebot.startBot(team_id);
});
} else if (text === "off") {
redis.del(rkey(team_id, "on"), function(err, obj) {
botils.shout(res, "stopped tracking karma");
botils.holla(res, "stopped tracking karma");
beebot.startBot(team_id);
});
} else if (text === "reset") {
redis.del(rkey(team_id, "on"), function(err, obj) {
if (!err) {
redis.del(rkey(team_id), function(err, obj) {
if (!err) {
botils.shout(res, "reset the karma table and stopped tracking.");
botils.holla(res, "reset the karma table and stopped tracking.");
beebot.startBot(team_id);
} else {
botils.shout(res,"something went wrong when trying to reset karma");
botils.holla(res,"something went wrong when trying to reset karma");
}
});
} else {
botils.shout(res, "something went wrong when trying to reset karma");
botils.holla(res, "something went wrong when trying to reset karma");
}
});
} else if (text.match(/^list/)) {
Expand All @@ -111,7 +111,7 @@ var handleSlash = function(req, res) {
function(err, obj) {
if (!err) {
if (obj.length === 0) {
botils.shout(res, "No karma yet! Try `karmabot++`?");
botils.holla(res, "No karma yet! Try `karmabot++`?");
return;
}
var message = "";
Expand All @@ -120,7 +120,7 @@ var handleSlash = function(req, res) {
message += e;
message += (i % 2 === 0) ? ": " : "\n";
});
botils.shout(res, message);
botils.holla(res, message);
} else {
botils.whisp(res, "something went wrong trying to retrieve karma"+ err);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/roll.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --------------------------------- 80chars ---------------------------------->
const botils = require('./botils.js')
const shout = botils.shout
const holla = botils.holla
const whisp = botils.whisp
const randint = botils.randint
const bern = botils.bern
Expand All @@ -11,11 +11,11 @@ var handleSlash = function(req, res) {
if (isNaN(n)) {
whisp(res, "Pssst, this is not an integer: " + text)
} else if (n <= 0) {
shout(res, "Rolling " + n + "-sided die... "
holla(res, "Rolling " + n + "-sided die... "
+ (bern(0.1) ? ":poop:" : ":boom:")
+ " (try again with a positive number of sides?)")
} else {
shout(res, "Rolling " + n + "-sided die... it came up " + randint(n))
holla(res, "Rolling " + n + "-sided die... it came up " + randint(n))
}
// TODO: whisper help in response to /roll or /roll help
}
Expand Down
18 changes: 9 additions & 9 deletions lib/tock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// --------------------------------- 80chars ---------------------------------->
const botils = require('./botils.js')
const shout = botils.shout
const holla = botils.holla
const whisp = botils.whisp
const keyfn = botils.keyfn

Expand Down Expand Up @@ -98,7 +98,7 @@ var abortTock = (res, user, chan) => {
tock.dueby,
tock.dueby,
(err, obj) => {
shout(res, "Ended tock for " + user)
holla(res, "Ended tock for " + user)
}
)
}
Expand Down Expand Up @@ -129,10 +129,10 @@ var finishTock = (res, user, chan, team_id, user_id) => {
if (goalname) {
// user has linked goal to beeminder
updateBeeminder(team_id, user_id, goalname, tock)
shout(res, user + " completed tock: " + tock.text +
holla(res, user + " completed tock: " + tock.text +
" :tada:\nUpdating Beeminder goal now...")
} else {
shout(res, `${user} SUCCESS :tada:: ${tock.text}`)
holla(res, `${user} SUCCESS :tada:: ${tock.text}`)
}
}
)
Expand Down Expand Up @@ -176,7 +176,7 @@ var beemind = (res, user, chan, team_id, goalname=null) => {
var setChannelLength = (res, chan, length) => {
redis.set(rkey("channels", chan, "length"), length,
(err, obj) => {
shout(res, "New tocks are now "+ length +
holla(res, "New tocks are now "+ length +
" minutes long. Active tocks are unaffected.")
})
}
Expand All @@ -186,7 +186,7 @@ var echoChannelLength = (res, chan, chanName) => {
})
}

var shoutStatus = (res, chan) => {
var hollaStatus = (res, chan) => {
redis.zrangebyscore(rkey("tocks", chan), Date.now(), "inf",
(err, obj) => {
var rText = "";
Expand All @@ -201,7 +201,7 @@ var shoutStatus = (res, chan) => {
minutes + " minutes " + seconds + " seconds" + "\n";
})
if (rText === "") { rText = "No active tocks. Get to work, slackers!" }
shout(res, rText)
holla(res, rText)
}
)
}
Expand Down Expand Up @@ -239,7 +239,7 @@ var startTock = (res, chan, user, team_id, text) => {
// that being in milliseconds will make it unique to retrieve.
redis.zadd(rkey("tocks", chan), dueby, JSON.stringify(tock),
(err, obj) => {
shout(res, "Started tock for " + user + ": " + text)
holla(res, "Started tock for " + user + ": " + text)
}
)

Expand Down Expand Up @@ -283,7 +283,7 @@ var handleSlash = (req, res) => {
} else if (text === "length") {
echoChannelLength(res, chan, req.body.channel_name)
} else if (text === "status") {
shoutStatus(res, chan)
hollaStatus(res, chan)
} else {
startTock(res, chan, user, team_id, text)
}
Expand Down

0 comments on commit 99e5dd0

Please sign in to comment.