Skip to content
This repository was archived by the owner on Dec 10, 2022. It is now read-only.

Added == #14

Open
wants to merge 45 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
0fdc7f6
Update app.json
RowdyChildren Dec 12, 2018
70aec76
Update messages.js
RowdyChildren Dec 12, 2018
1a01a51
Update events.js
RowdyChildren Dec 12, 2018
48b8296
Update leaderboard.js
RowdyChildren Dec 12, 2018
1a61169
Update points.js
RowdyChildren Dec 12, 2018
fc95a3c
whoops
RowdyChildren Dec 12, 2018
c9b3f95
added support for emojji
RowdyChildren Dec 12, 2018
6e82a1e
Update helpers.js
RowdyChildren Dec 12, 2018
cc720b2
Update events.js
RowdyChildren Dec 12, 2018
3df78b2
Update helpers.js
RowdyChildren Dec 12, 2018
0df6fe7
Update helpers.js
RowdyChildren Dec 12, 2018
c3d3f63
added =
MattLParker Dec 13, 2018
5150f54
added ,
MattLParker Dec 13, 2018
ffbc86d
updated handler
MattLParker Dec 13, 2018
23658ea
fix casing
MattLParker Dec 13, 2018
461c898
added =
MattLParker Dec 13, 2018
407d1bd
updated messages
MattLParker Dec 13, 2018
2eb44ba
removed spaces
MattLParker Dec 13, 2018
8991055
test remove operation
MattLParker Dec 13, 2018
56bbf10
Revert "test remove operation"
MattLParker Dec 13, 2018
cfe97b0
Merge branch 'master' into master
RowdyChildren Dec 13, 2018
08fb59f
test error fix when asking points of bot
MattLParker Dec 13, 2018
685a1aa
removed donothing
MattLParker Dec 13, 2018
1533c3e
add quotes
MattLParker Dec 13, 2018
eb2ec66
Merge branch 'Testing'
MattLParker Dec 13, 2018
c9f1db8
rebase messages
MattLParker Dec 13, 2018
cf86fb7
moved text
MattLParker Dec 13, 2018
b1b52d7
undo last
MattLParker Dec 13, 2018
14cf312
added one line
MattLParker Dec 13, 2018
c08de65
revert others
MattLParker Dec 13, 2018
e7b71cc
Merge branch 'Testing'
MattLParker Dec 13, 2018
f85f847
changed to creater/master
MattLParker Dec 13, 2018
9980aaf
further rebase
MattLParker Dec 13, 2018
dd6b91f
fixing some "errors"
MattLParker Dec 13, 2018
6e00348
extra }
MattLParker Dec 13, 2018
64d28af
missing comma
MattLParker Dec 13, 2018
ce8a96b
changed to uppercase
MattLParker Dec 13, 2018
da15093
Check without uppercase
MattLParker Dec 13, 2018
15a0098
added =
MattLParker Dec 14, 2018
cc3e4b1
fixed spaces
MattLParker Dec 14, 2018
ab34559
remove operation
MattLParker Dec 14, 2018
afa61a8
one last stupid space
MattLParker Dec 14, 2018
37c8d2c
oops
MattLParker Dec 14, 2018
5994f1d
added help, refactored to lowercase g in getScore
MattLParker Dec 14, 2018
0f0d952
removed Site specific
MattLParker Dec 14, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 28 additions & 2 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,25 @@ const handlePlusMinus = async( item, operation, channel ) => {
return slack.sendMessage( message, channel );
};

/**
* Handles a = against a user, and then notifies the channel of the new score.
*
* @param {string} item The Slack user ID (if user) or name (if thing) of the item being
* operated on.
* @param {string} operation The mathematical operation performed on the item's score.
* @param {object} channel The ID of the channel (Cxxxxxxxx for public channels or Gxxxxxxxx for
* private channels - aka groups) that the message was sent from.
* @return {Promise} A Promise to send a Slack message back to the requesting channel after the
* points have been updated.
*/
const handlePlusEqual = async( item, operation, channel ) => {
const score = await points.getScore( item, operation ),
operationName = operations.getOperationName( operation ),
message = messages.getRandomMessage( operationName, item, score );

return slack.sendMessage( message, channel );
};

/**
* Sends a random thank you message to the requesting channel.
*
Expand Down Expand Up @@ -94,6 +113,7 @@ const sendHelp = ( event ) => {
'Sure, here\'s what I can do:\n\n' +
'• `@Someone++`: Add points to a user or a thing\n' +
'• `@Someone--`: Subtract points from a user or a thing\n' +
'• `@Someone==`: Gets current points from a user or a thing\n' +
'• `<@' + botUserID + '> leaderboard`: Display the leaderboard\n' +
'• `<@' + botUserID + '> help`: Display this message\n\n' +
'You\'ll need to invite me to a channel before I can recognise ' +
Expand Down Expand Up @@ -134,9 +154,12 @@ const handlers = {
return false;
}

if ( '=' === operation ) {
return handlePlusEqual( item, operation, event.channel );
}

// Otherwise, let's go!
return handlePlusMinus( item, operation, event.channel );

}, // Message event.

/**
Expand All @@ -158,7 +181,10 @@ const handlers = {
help: sendHelp,
thx: sayThankyou,
thanks: sayThankyou,
thankyou: sayThankyou
thankyou: sayThankyou,
'++': handlePlusMinus,
'--': handlePlusMinus,
'==': handlePlusEqual
};

const validCommands = Object.keys( appCommandHandlers ),
Expand Down
3 changes: 2 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ const extractCommand = ( message, commands ) => {
* (i.e. + or -).
*/
const extractPlusMinusEventData = ( text ) => {
const data = text.match( /@([A-Za-z0-9]+?)>?\s*(\+{2}|-{2}|—{1})/ );

const data = text.match( /@([A-Za-z0-9:-_]+?)>?\s*(\+{2}|-{2}|—{1}|={2})/ );

if ( ! data ) {
return false;
Expand Down
20 changes: 19 additions & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,20 @@ messages[ operations.MINUS ] = [
}
];

messages[ operations.EQUAL ] = [
{
probability: 100,
set: [
'How is',
'Why is'
]
},
{
probability: 1,
set: [ ':shifty:' ]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see you've also joined the cult of the shifty. One of us! One of us! One of us!

}
];

messages[ operations.SELF ] = [
{
probability: 100,
Expand Down Expand Up @@ -96,13 +110,17 @@ const getRandomMessage = ( operation, item, score = 0 ) => {
switch ( operation ) {
case operations.MINUS:
case operations.PLUS:
format = '<message> *<item>* is now on <score> point<plural>.';
format = '<message> *<item>* has <score> point<plural>.';
break;

case operations.SELF:
format = '<item> <message>';
break;

case operations.EQUAL:
format = '<message> *<item>* currently at <score> point<plural>.';
break;

default:
throw Error ( 'Invalid operation: ' + operation );
}
Expand Down
4 changes: 3 additions & 1 deletion src/operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
const operations = {
PLUS: 'plus',
MINUS: 'minus',
SELF: 'selfPlus'
SELF: 'selfPlus',
EQUAL: 'equal'
};

/**
Expand All @@ -23,6 +24,7 @@ const getOperationName = ( operation ) => {
switch ( operation ) {
case '+': operationName = operations.PLUS; break;
case '-': operationName = operations.MINUS; break;
case '=': operationName = operations.EQUAL; break;
}
/* eslint-enable max-statements-per-line */

Expand Down
39 changes: 38 additions & 1 deletion src/points.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,44 @@ const updateScore = async( item, operation ) => {

}; // UpdateScore.

/**
* Gets score
* into the database with an assumed initial score of 0.
*
* This function also sets up the database if it is not already ready, including creating the
* scores table and activating the Postgres case-insensitive extension.
*
* @param {string} item The Slack user ID (if user) or name (if thing) of the item being
* operated on.
* @param {string} operation The mathematical operation performed on the item's score.
* @return {int} The item's new score after the update has been applied.
*/
const getScore = async( item ) => {

// Connect to the DB, and create a table if it's not yet there.
// We also set up the citext extension, so that we can easily be case insensitive.
const dbClient = await postgres.connect();
await dbClient.query( '\
CREATE EXTENSION IF NOT EXISTS citext; \
CREATE TABLE IF NOT EXISTS ' + scoresTableName + ' (item CITEXT PRIMARY KEY, score INTEGER); \
' );

// Get the new value.
// TODO: Fix potential SQL injection issues here, even though we know the input should be safe.
const dbSelect = await dbClient.query( '\
SELECT score FROM ' + scoresTableName + ' WHERE item = \'' + item + '\'; \
' );

await dbClient.release();
const score = dbSelect.rows[0].score;

console.log( item + ' now on ' + score );
return score;

}; // UpdateScore.

module.exports = {
retrieveTopScores,
updateScore
updateScore,
getScore
};