-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcommand-parser.js
86 lines (79 loc) · 2.1 KB
/
command-parser.js
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
/**
* Command Parser
* BotStuff - https://github.com/CheeseMuffin/BotStuff
*
* This file parses command messages.
*
* @license MIT license
*/
'use strict';
class Context {
constructor(target, room, user, command, time) {
this.target = target ? target.trim() : '';
this.room = room;
this.user = user;
this.command = command;
this.time = time || Date.now();
}
say(text) {
this.room.say(text);
}
run(command, target) {
if (command) {
command = Tools.toId(command);
if (!Commands[command]) return;
let type = typeof Commands[command];
if (type === 'string') {
command = Commands[command];
type = typeof Commands[command];
}
if (type !== 'function') return;
target = target.trim();
} else {
command = this.command;
target = this.target;
}
try {
Commands[command].call(this, target, this.room, this.user, this.command, this.time);
} catch (e) {
let stack = e.stack;
stack += 'Additional information:\n';
stack += 'Command = ' + command + '\n';
stack += 'Target = ' + target + '\n';
stack += 'Time = ' + new Date(this.time).toLocaleString() + '\n';
stack += 'User = ' + this.user.name + '\n';
stack += 'Room = ' + (this.room === this.user ? 'in PM' : this.room.id);
console.log(stack);
}
}
}
class CommandParser {
parse(message, room, user, time) {
message = message.trim();
if (message === '/me in' && room.game) {
room.game.join(user);
} else {
if (message.charAt(0) !== Config.commandCharacter) return;
message = message.substr(1);
let spaceIndex = message.indexOf(' ');
let target = '';
let command = '';
if (spaceIndex !== -1) {
command = message.substr(0, spaceIndex);
target = message.substr(spaceIndex + 1);
} else {
command = message;
Games.sayDescription(command, room);
}
if (!Commands[command]) return;
let type = typeof Commands[command];
if (type === 'string') {
command = Commands[command];
type = typeof Commands[command];
}
if (type !== 'function') return;
new Context(target, room, user, command, time).run();
}
}
}
module.exports = new CommandParser();