-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.js
85 lines (78 loc) · 2.02 KB
/
action.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
var logger = require("./logger");
var actionFactory = {
createAction: args => {
return new Promise((resolve, reject) => {
switch (args.type) {
case "say":
resolve(new SayAction(args.message));
break;
case "whisper":
resolve(new WhisperAction(args.message));
break;
case "func":
resolve(new FuncAction(args.funcName));
break;
default:
throw new Error("Unknow action type");
}
});
}
};
module.exports.actionFactory = actionFactory;
class Action {
constructor(type, exe) {
this.type = type;
this.execute = exe;
}
}
class SayAction extends Action {
constructor(message) {
super("say", (bot, from, to, args) => {
bot.say(message, to);
});
}
}
class WhisperAction extends Action {
constructor(message) {
super("whisper", (bot, from, to, args) => {
bot.whisper(message, from);
});
}
}
class FuncAction extends Action {
constructor(funcName) {
super("func", funcWrapper[funcName]);
}
}
/* Move this to a different file */
var funcWrapper = {
commands: (bot, from, to, args) => {
var commands = bot.CommandManager.commands;
var res = Object.keys(commands).join(", ");
bot.say(res, to);
},
users: (bot, from, to, args) => {
var { channels, users } = bot;
bot.say(channels.join(", ") + " users:" + users.join(", "), to);
},
"add-command": (bot, from, to, args) => {
bot.CommandManager.addCommand(args)
.then(cmd => bot.whisper("Command {0} added ".format(cmd.name), from))
.catch(({ message, cmd }) =>
bot.whisper(message, from).catch(e => console.error(e))
);
},
"remove-command": (bot, from, to, args) => {
bot.CommandManager.removeCommand(args[0]).then(res =>
bot.whisper("Command {0} removed".format(args[0]), from)
);
},
reboot: (bot, from, to, args) => {
bot.say("Rebooting...", to);
bot.restart();
},
quit: (bot, from, to, args) => {
bot.say("Exiting...", to);
bot.stop();
}
};