-
Notifications
You must be signed in to change notification settings - Fork 1
/
summon.coffee
40 lines (34 loc) · 1.32 KB
/
summon.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# Description:
# Keep track of hubot's usage statistics. Who is requesting hubot to do their
# bidding?
#
# Commands:
# hubot summon count - Show hubot usage statistics.
module.exports = (robot) ->
getSummonCount = (user) ->
return robot.brain.get("summon_" + user + "_count") or 0
setSummonCount = (user, value) ->
robot.brain.set("summon_" + user + "_count", value)
robot.listenerMiddleware (context, next, done) ->
# don't increment summon count on summon commands
if !context.listener.options?.summon
user = context.response.message.user.name
summonCount = getSummonCount(user)
setSummonCount(user, summonCount + 1)
next()
robot.respond /summon count/i, summon: true, (res) ->
users = []
for _, v of robot.brain.users() or {}
users.push({name: v.name, count: getSummonCount(v.name)})
users = users.filter (a) ->
return a.count != 0
users.sort (a, b) ->
return b.count - a.count
res.send "Summon counts"
response = ""
if users.length > 0
response += "```#{users[0].name} - #{users[0].count}"
for user in users[1..]
response += "\n#{user.name} - #{user.count}"
response += "```"
res.send response