-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
143 lines (140 loc) · 3.44 KB
/
main.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
var HackChat = require("./hackchat.js");
var chat = new HackChat();
var channelName = "programming";
var botName = "Stammy";
var channel = chat.join(channelName, botName);
var fs = require("fs");
var path = require("path");
var bot = {};
bot.commands = {};
bot.data = require("./data.json");
bot.messages = [];
bot.dataupdate = function()
{
fs.writeFileSync("data.json", JSON.stringify(bot.data, undefined, 4));
}
bot.choose = function(arr)
{
channel.sendMessage(arr[Math.round(Math.random() * (arr.length - 1))]);
}
bot.ardel = function(ar, del)
{
var a = ar.indexOf(del);
ar = ar.splice(a, 1);
}
bot.substr = function(arr, radix)
{
var result = [];
for(var i = radix; i < arr.length; i++)
{
result.push(arr[i]);
}
return result;
}
bot.requirePerm = function(trip)
{
if(bot.data.mods.indexOf(trip) != -1)
{
return true;
}
return false;
}
fs.readdir("./commands", function(err, files)
{
if(err)
{
throw err;
}
for(var i = 0; i < files.length; i++)
{
if(path.extname(files[i]) == ".js")
{
var cmds = require("./commands/" + files[i]);
if(typeof cmds != "object")
{
throw "Invalid command " + files[i];
}
for(var k in cmds)
{
if(typeof cmds[k] != "function")
{
throw "Invalid command " + files[i];
}
bot.commands[k] = cmds[k];
}
}
}
function parseCmd(session, nick, text, time, isAdmin, trip)
{
var msg = nick + ": " + text;
console.log(msg);
bot.messages.push(msg);
for(var i in bot.data.banned)
{
if(bot.data.banned[i] === nick)
{
return;
}
}
if(nick != botName && nick.toLowerCase().indexOf("bot") === -1)
{
if(bot.data.afks.hasOwnProperty(nick) && text.indexOf("#") !== 0)
{
if(!bot.data.afks[nick].hasOwnProperty("trip") || bot.data.afks[nick].trip == trip)
{
if(bot.data.afks[nick].who.length > 0)
{
channel.sendMessage("@" + nick + " is back! @" + nick + ", you got these messages while AFK:\n" + bot.data.afks[nick].who.join("\n"));
}
else
{
channel.sendMessage("@" + nick + " is back!");
}
delete bot.data.afks[nick];
bot.dataupdate();
}
}
if(text.indexOf("@") != -1)
{
for(var i in bot.data.afks)
{
if((text.indexOf(i) == (text.indexOf("@") + 1)) && nick != i)
{
channel.sendMessage("@" + nick + " @" + i + " is AFK!");
bot.data.afks[i].who.push(msg);
bot.dataupdate();
}
}
}
if(text[0] == "#")
{
var args = text.substr(1).split(" ");
var cmd = args[0].toLowerCase();
var args = args.slice(1).join(" ").trim().split(" ");
if(typeof bot.commands[cmd] == "function" && bot.commands.hasOwnProperty(cmd))
{
bot.commands[cmd](session, bot, channel, cmd, args, nick, text, time, isAdmin, trip);
}
}
}
}
chat.on("chat", function(session, nick, text, time, isAdmin, trip)
{
parseCmd(session, nick, text, time, isAdmin, trip);
});
chat.on("info", function(session, text, time)
{
var not = "*** " + text + " ***";
console.log(not);
});
chat.on("onlineAdd", function(session, nick, time)
{
var not = "*** " + nick + " joined ***";
console.log(not);
});
chat.on('onlineRemove', function(session, nick, time)
{
var not = "*** " + nick + " left ***";
console.log(not);
});
});