-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.js
More file actions
151 lines (133 loc) · 4.39 KB
/
bot.js
File metadata and controls
151 lines (133 loc) · 4.39 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
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
144
145
var irc = require("irc");
var fs = require('fs');
var crypto = require('crypto');
var config = {
channels: ["#channel1"],
server: "irc.example.com",
botName: "nodebot",
webserver: "<webserver address>"
};
// Join default channels in config
var bot = new irc.Client(config.server, config.botName, {
channels: config.channels
});
// static properties / Channel class constructor
Channel._channelMap = {};
Channel.getChannels = function() {
var channelIdList = [];
for(var channel in Channel._channelMap) {
if(Channel._channelMap.hasOwnProperty(channel)
&& Channel._channelMap[channel].constructor === "Channel") {
channelIdList.push(channel);
}
}
return channelIdList;
}
Channel.prototype.addMessage = function(user, text) {
this.chatlog.push({'user': user, 'message': text});
}
Channel.prototype.getMessages = function() {
return this.chatlog;
}
Channel.getChannel = function(name) {
if ( this._channelMap[name] ) {
return this._channelMap[name];
}
else {
var newchannel = new Channel(name);
return newchannel;
}
}
function Channel(name) {
this.record = false;
this.chatlog = [];
this.name = name;
Channel._channelMap[name] = this;
}
// Help function
bot.addListener("message", function(from, to, text, message) {
var regex = /^\.help/
if ( text.match(regex) ) {
var arrtext = text.split(" ");
var command = arrtext[1];
if ( !command ) {
bot.say(to, "Available commands: .invite .leave .startrecord .stoprecord .postrecord .wb");
}
else if ( command.match("invite") ) { bot.say(to, "Get bot to join a channel, usage: .invite #channel" ); }
else if ( command.match("leave" ) ) { bot.say(to, "Kick bot out of your channel, usage: .leave" ); }
else if ( command.match("startrecord" ) ) { bot.say(to, "Start recording in the channel, usage: .startrecord" ); }
else if ( command.match("stoprecord" ) ) { bot.say(to, "Stop recording in the channel, usage: .stoprecord" ); }
else if ( command.match("postrecord" ) ) { bot.say(to, "Post last recording to webserver, usage: .postrecord" ); }
else if ( command.match("wb" ) ) { bot.say(to, "Welcome people back, usage: .wb name" ); }
}
});
// Controlling Bot
bot.addListener("message", function(from, to, text, message) {
var regexinvite = /^\.invite/
var regexleave = /^\.leave/
if ( text.match(regexinvite) ) {
var arrtext = text.split(" ");
var channel = Channel.getChannel(arrtext[1]);
bot.say(to, "Joining "+channel.name);
bot.join(channel.name);
}
else if ( text.match(regexleave) ) {
bot.part(to);
//maybe remove channel from channelmap here
}
var regexstartrecord = /^\.startrecord/
var regexstoprecord = /^\.stoprecord/
var regexpostrecord = /^\.postrecord/
if ( text.match(regexstartrecord) ) {
var channel = Channel.getChannel(to);
channel.chatlog.length = 0;
channel.record = true;
bot.say(to, "Started recording in "+channel.name);
}
else if ( text.match(regexstoprecord) ) {
var channel = Channel.getChannel(to);
channel.record = false;
bot.say(to, "Stopped recording in "+channel.name);
}
else if ( text.match(regexpostrecord) ) {
var channel = Channel.getChannel(to);
var stamp = crypto.randomBytes(3).toString('hex');
var stream = fs.createWriteStream("webfiles/chat-"+ stamp +".log");
stream.once('open', function(fd) {
channel.chatlog.forEach(function(msg) {
stream.write(msg.user+": "+msg.message+"\n");
});
stream.end();
});
// Put a symbolic link from webfiles to your webserver directory
bot.say(channel.name, "Posting conversation to: http://"+config.webserver+"/chatlogs/chat-"+ stamp +".log");
}
});
// Recording section
bot.addListener("message", function(from, to, text, message) {
var channel = Channel.getChannel(to);
if ( channel.record ) {
channel.addMessage(from, text)
}
});
bot.addListener("join", function(inchannel, who) {
var channel = Channel.getChannel(inchannel);
if ( channel.record ) {
channel.addMessage("JOINED", who)
}
});
// Fun Section
bot.addListener("message", function(from, to, text, message) {
regexwb = /^\.wb/
if ( text.match(regexwb) ) {
arrtext = text.split(" ");
var person = arrtext[1];
if ( person ) {
bot.say(to, "Welcome back "+person+"!");
}
}
});
// Catch IRC errors
bot.addListener('error', function(message) {
console.log('error: ', message);
});