-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.js
More file actions
90 lines (72 loc) · 2.67 KB
/
command.js
File metadata and controls
90 lines (72 loc) · 2.67 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
var util = require('util');
var commands = 'channels chat endchat join leave part say quit'.split(' ');
var chatTarget = null;
module.exports.commands = commands;
module.exports.process = function (client, commandLine) {
var keyword = getFirstWord(commandLine);
var target = getSecondWord(commandLine);
if (chatTarget) {
if (keyword == 'endchat') {
var oldTarget = chatTarget;
chatTarget = null;
return util.format('ending conversation with %s.', oldTarget);
}
client.say(chatTarget, commandLine);
return '';
} else if (commandLine.length === 0) {
return '';
} else if (commands.indexOf(keyword) < 0) {
return util.format('Unknown command: %s', keyword);
}
switch (keyword) {
case "channels":
return util.format('Currently on: %s', client.channels);
// cases with a channel parameter
case "join":
if (!target)
return 'no channel specified';
else if (client.channels.indexOf(target) >= 0)
return util.format('already in %s', target);
client.join(target);
client.channels.push(target);
return util.format('joined %s', target);
case "leave":
case "part":
var chanIdx = client.channels.indexOf(target);
if (!target)
return 'no channel specified';
else if (chanIdx < 0)
return util.format('not in %s', target);
client.part(target);
client.channels.splice(chanIdx, 1);
return util.format('left %s', target);
case "say":
if (!target)
return "missing target";
var whatToSay = getRemainingArgs(keyword, target, commandLine);
if (!whatToSay)
return "nothing to say";
client.say(target, whatToSay);
return util.format("spoke to %s", target);
case "chat":
if (!chatTarget) {
chatTarget = target;
}
return util.format('beginning conversation with %s. "endchat" to end.', chatTarget);
}
}
var getFirstWord = function (str) {
var idx = str.indexOf(' ');
return (idx < 0) ? str : str.slice(0, idx);
}
var getSecondWord = function (str) {
var first = getFirstWord(str);
if (first.length == str.length)
return null; // there is no second word
return getFirstWord(str.replace(first + ' ', ''));
}
var getRemainingArgs = function (one, two, str) {
var prefix = util.format('%s %s', one, two);
var out = str.replace(prefix, '').trim();
return out === '' ? null : out;
}