This repository has been archived by the owner on Sep 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
bot.js
executable file
·120 lines (106 loc) · 4.05 KB
/
bot.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var fs = require("fs");
var path = require("path");
var events = require("events");
var util = require('util');
function Bot(chatServerBase) {
this.chatServerBase = chatServerBase;
this.channelRestrictedCommands = {};
events.EventEmitter.call(this);
var that = this;
fs.readdir("./commands", function(err, files) {
if (err)
throw err;
that.perm = require('./data/requiredPerm.json');
that.commands = {};
var init = [];
for (var i = 0; i < files.length; i++) {
if (path.extname(files[i]) == ".js") {
try {
var cmds = require("./commands/" + files[i]);
if (typeof cmds != 'object')
throw "Invalid command " + files[i];
if (typeof cmds.init == 'function') {
cmds.init(that);
init.push(cmds.init);
delete cmds.init;
}
for (var key in cmds) {
if (typeof cmds[key] != 'function')
throw "Invalid command " + files[i];
that.commands[key] = cmds[key];
}
} catch (e) {
console.log("Error loading command " + files[i]);
console.log(e);
}
}
}
});
this.restrictCommandToChannels = function(cmd, channelList) {
this.channelRestrictedCommands[cmd] = channelList;
};
this.parseCmd = function(data, client) {
var msg = data.text;
var cmd = msg.substr(1).split(" ")[0];
var args = msg.substr(2 + cmd.length).split(" ");
var channelRestrictedCommand = this.channelRestrictedCommands[cmd];
if (channelRestrictedCommand && channelRestrictedCommand.indexOf(client.channel) == -1) {
that.sendClient("This command can not be executed in this channel", client);
return;
}
if (typeof that.commands[cmd] == 'function' && that.commands.hasOwnProperty(cmd))
if (that.verify(that, cmd, data, client))
return that.commands[cmd](that, data.nick, args, data, client);
else
that.sendClient("You do not have the right permissions for this command", client);
else
that.sendClient("Unknown command, type !help or !h for a list of available commands", client);
return;
};
this.verify = function(that, cmd, data, client) {
if (that.perm[cmd] > 0) {
if (data.mod || data.admin)
return true;
else
return false;
}
if (that.perm[cmd] > 1) {
if (data.admin)
return true;
else
return false;
}
return true;
};
this.sendAll = function(text, client) {
if (client) {
var botData = { cmd: 'chat', text: text, nick: 'Bot' };
this.chatServerBase.broadcast(null, botData, client.channel);
return true;
}
console.log('missing parameter [client] in bot.sendAll()');
return false;
};
this.sendClient = function(text, client) {
if (client) {
var botData = { cmd: 'chat', text: text, nick: 'Bot' };
client.send(null, botData);
return true;
}
console.log('missing parameter [client] in bot.sendClient()');
return false;
}
this.sendWhisper = function(text, client, friend) {
if (client) {
var botData = { cmd: 'chat', text: text, nick: client.nick, target: friend.nick, whisper: true, whisperTo: true };
client.send(null, botData);
botData = { cmd: 'chat', text: text, nick: client.nick, whisper: true, whisperAt: true };
friend.send(null, botData);
return true;
}
console.log('missing parameter [client] in bot.sendWhisper()');
return false;
}
}
util.inherits(Bot, events.EventEmitter);
module.exports = Bot;