Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

➕ Support multiple karma increments/decrements #407

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
35 changes: 20 additions & 15 deletions scripts/karma.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -84,21 +84,26 @@ module.exports = (robot) ->
karma = new Karma robot
allow_self = process.env.KARMA_ALLOW_SELF or "true"

robot.hear /(?:[\(]([\S\s]+)[\)]|([\S]+[^() ]))\+\+(\s|$)/, (msg) ->
subject = (msg.match[1] || msg.match[2]).toLowerCase()
if allow_self is true or msg.message.user.name.toLowerCase() != subject
karma.increment subject
msg.send "#{subject} #{karma.incrementResponse()} (Karma: #{karma.get(subject)})"
else
msg.send msg.random karma.selfDeniedResponses(msg.message.user.name)

robot.hear /(?:[\(]([\S\s]+)[\)]|([\S]+[^()][^ ]))--(\s|$)/, (msg) ->
subject = (msg.match[1] || msg.match[2]).toLowerCase()
if allow_self is true or msg.message.user.name.toLowerCase() != subject
karma.decrement subject
msg.send "#{subject} #{karma.decrementResponse()} (Karma: #{karma.get(subject)})"
else
msg.send msg.random karma.selfDeniedResponses(msg.message.user.name)
increment_regex = /(?:[\(]([\S\s]+)[\)]|([\S]+[^() ]))\+\+(\s|$)/g
decrement_regex = /(?:[\(]([\S\s]+)[\)]|([\S]+[^()][^ ]))--(\s|$)/g

robot.hear increment_regex, (msg) ->
while match = increment_regex.exec msg.message.text
subject = (match[1] || match[2]).toLowerCase()
if allow_self is true or msg.message.user.name.toLowerCase() != subject
karma.increment subject
msg.send "#{subject} #{karma.incrementResponse()} (Karma: #{karma.get(subject)})"
else
msg.send msg.random karma.selfDeniedResponses(msg.message.user.name)

robot.hear decrement_regex, (msg) ->
while match = decrement_regex.exec msg.message.text
subject = (match[1] || match[2]).toLowerCase()
if allow_self is true or msg.message.user.name.toLowerCase() != subject
karma.decrement subject
msg.send "#{subject} #{karma.decrementResponse()} (Karma: #{karma.get(subject)})"
else
msg.send msg.random karma.selfDeniedResponses(msg.message.user.name)

robot.respond /karma empty ?(\S+[^-\s])$/i, (msg) ->
subject = msg.match[1].toLowerCase()
Expand Down