Skip to content

Commit

Permalink
Bugfix with whitespace in /charge command
Browse files Browse the repository at this point in the history
  • Loading branch information
Glitch (bee) committed Dec 16, 2017
1 parent e337ac5 commit d489990
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 57 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
19 changes: 12 additions & 7 deletions lib/botils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// --------------------------------- 80chars ---------------------------------->
var request = require('request')

// Respond with string txt to everyone in the channel, echoing the slash command
var shout = (res, txt) => {
res.send({ "response_type": "in_channel", "text": txt })
}
// 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 (and optional text attachment att) to just the user
// who issued the slash command, and don't echo their slash command. WHISPer.
Expand All @@ -15,8 +15,13 @@ var whisp = (res, txt, att) => {
"attachments": [{"text": att}]})
}

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

// 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,
whisp,
shoutDelayed,
holla,
blurt,
bern,
randint,
isEmpty,
Expand Down
26 changes: 13 additions & 13 deletions lib/charge.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ var help = function(res) {
// whisper the documentation
botils.whisp(res, "How to use /charge\n"
+ "`/charge N [reason]` charge yourself $N (real, US dollars) via the "
+ "Beeminder API. Optionally include a `reason`\n"
+ "`/charge N in X [reason]` charge yourself $N in X minutes unless you"
+ " cancel before the timer runs out (e.g. you complete the task) Optionally "
+ "include a `reason`\n"
+ "Beeminder API. Optionally include a `reason`.\n"
+ "`/charge N in T [reason]` charge yourself $N in T minutes unless you"
+ " cancel before the timer runs out (e.g. you complete the task)\n"
+ "`/charge addcard` show the URL to add a payment method to Beeminder\n"
+ "`/charge cancel` cancel all pending charges\n"
+ "`/charge cancel reason` cancel charge matching `reason`\n"
+ "`/charge list` lists all pending charges\n"
+ "`/charge help` show this message")
+ "`/charge help` is what you're looking at")
}

// run all charges with charge dates < now, post to channel
Expand Down Expand Up @@ -73,7 +72,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 +92,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 +110,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 +132,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 Expand Up @@ -173,11 +172,12 @@ var handleSlash = function(req, res) {
};

// if they've specified a delay, update the delay and the reason.
if (text.match(/^[\d\.]+\sin\s[\d\.]/)) {
var delay = parseInt(text.match(/^[\d\.]+\sin\s([\d\.]+)/)[1]);
// ick, those regexes need DRYing up!
if (text.match( /^[\d\.]+\s+in\s+[\d\.]/)) {
var delay = parseInt(text.match(/^[\d\.]+\s+in\s+([\d\.]+)/)[1]);
charge.charge_at = Date.now() + 1000*60*delay;
if (text.match(/^[\d\.]+\sin\s([\d\.])+\s+(.*)/)) {
charge.reason = text.match(/^[\d\.]+\sin\s([\d\.])+\s+(.*)/)[2];
if (text.match( /^[\d\.]+\s+in\s+([\d\.])+\s+(.*)/)) {
charge.reason = text.match(/^[\d\.]+\s+in\s+([\d\.])+\s+(.*)/)[2];
}
scheduleCharge(res, charge);
} else {
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
17 changes: 11 additions & 6 deletions lib/roll.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
// --------------------------------- 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

var handleSlash = function(req, res) {
var handleSlash = function(req, resp) {
var text = req.body.text
if (text === '' || text === 'help') {
whisp(resp, "How to use /roll\n"
+ "`/roll N` roll an N-sided die\n"
+ "`/roll help` show this")
return
}
var n = parseInt(text)
if (isNaN(n)) {
whisp(res, "Pssst, this is not an integer: " + text)
whisp(resp, "Pssst, this is not an integer: " + text)
} else if (n <= 0) {
shout(res, "Rolling " + n + "-sided die... "
holla(resp, "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(resp, "Rolling " + n + "-sided die... it came up " + randint(n))
}
// TODO: whisper help in response to /roll or /roll help
}

module.exports = {
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"@slack/client": "^3.13.0",
"body-parser": "^1.18.2",
"dotenv": "^4.0.0",
"express": "^4.15.5",
"express": "^4.16.2",
"http": "0.0.0",
"https": "^1.0.0",
"redis": "^2.8.0",
Expand Down

0 comments on commit d489990

Please sign in to comment.