This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
trigger.coffee
executable file
·62 lines (42 loc) · 1.92 KB
/
trigger.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# SauceBot Trigger Utilities
db = require './saucedb'
io = require './ioutil'
PRI_TOP = 0 # Reserved for what, if anything, needs it.
PRI_HIGH = 100 # Could be used for sub commands, like '!vm clear'
PRI_MID = 200 # For simple commands, like '!time'
PRI_LOW = 300 # For greedy commands, like counter creation.
WORD_BONUS = -10 # Modifier based on word count to give sub commands priority
OP_BONUS = -2 # Modifier based on oplevel to give mod versions priority
escapeRegex = (string) ->
string.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&")
# Returns a Trigger responding to !<name>, where name is one or more words.
# A priority will be assigned that gives preference primarily to longer
# commands, and then to higher op status requirements.
exports.buildTrigger = (module, name, oplevel, callback) ->
words = name.split /\s+/
words = (escapeRegex(word) for word in words)
regex = new RegExp "^!" + words.join("\\s+") + "(?:\\s+(.+))?$", 'i'
priority = PRI_MID + WORD_BONUS*(words.length-1) + OP_BONUS*oplevel
new Trigger module, priority, oplevel, regex, callback
# A trigger is an object used to associate bot commands with RegExp patterns.
#
# The pattern must be a RegExp object. A single capturing group should be used
# to capture all arguments to the command.
class Trigger
constructor: (@module, @priority, @oplevel, @pattern, @execute) ->
# ...
test: (msg) ->
@pattern.test msg
# Returns an array of arguments (individual words in the first capturing
# group), or null if there are none
getArgs: (msg) ->
match = @pattern.exec(msg) ? [""]
capture = match[1] ? ""
# Return no args if there are no non-space characters
return [] unless /\S/.test capture
capture.split /\s+/
exports.PRI_TOP = PRI_TOP
exports.PRI_HIGH = PRI_HIGH
exports.PRI_MID = PRI_MID
exports.PRI_LOW = PRI_LOW
exports.Trigger = Trigger