-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnbot.js
162 lines (142 loc) · 4.88 KB
/
nbot.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// Read config options
var config = require('./config.json');
// Checks if input matches command expression and executes modules
function check_for_command(server, from, channel, text, cb) {
var fs = require('fs');
if (fs.existsSync('./modules.json')) {
var dyna = JSON.parse(fs.readFileSync('./modules.json', 'utf8'));
dyna.modules.forEach(function (value, key) {
var re = new RegExp(value.expression, 'i');
if (re.test(text)) {
console.log('Server: ' + server + ' Channel: ' + channel + ' User: ' + from + ' Command: ' + text);
// delete module from cache to allow dynamic changes
delete require.cache[require.resolve('./modules/' + value.module + '.js')];
// load module from disk
var module = require('./modules/' + value.module + '.js');
module[value.module](from, text, function (result) {
if (result !== false) {
cb(channel, result);
}
});
return;
}
});
}
}
// connects to server, joins channels and listens for messages
function start_bot(config) {
var IRC = require('ircsock');
var options = {
name: 'IRC',
port: 6667,
ssl: false
}
options.host = config.Server;
options.nickname = config.Nick;
options.username = config.Nick;
options.realname = config.Nick;
options.password = config.Password;
var irc = new IRC(options);
irc.on('motd', function () {
var util = require('util');
console.log("Connecting to " + config.Server + " channel(s) " + util.inspect(config.Channels) + " as " + config.Nick);
if (config.Channels.constructor.prototype.hasOwnProperty('push')) {
config.Channels.forEach(function (value) {
irc.join(value);
});
} else {
irc.join(config.Channels);
}
});
irc.on('join', function (event) {
if (event.nick == config.Nick) {
console.log(event.nick + ' joined ' + event.target);
}
});
// Listen for PRIVMSG
irc.on('privmsg', function (event) {
check_for_command(config.Server, event.nick, event.target, event.message, function (channel, message) {
irc.privmsg(channel, message);
});
// leave channel when requested
if (event.message === '!leavechannel') {
irc.part(event.target, 'leaving from ' + event.target + ' as requested by ' + event.nick, function () {
console.log('Leave requested from ' + event.target + ' by ' + event.nick);
});
}
});
// join channel upon invite
irc.on('invite', function (event) {
var channel_regex = /.*(#.*)/g;
if (match = channel_regex.exec(event.raw)) {
irc.join(match[1]);
console.log('Joined: ' + match[1]);
}
});
// Reconnect when kicked
irc.on('kick', function (event) {
console.log(event);
if (event.client == config.Nick) {
irc.join(event.target);
}
});
// Connect to server
irc.connect();
// Init OpenAI API if key is set
if (config.OpenAIKey) {
const { OpenAI } = require("openai");
const openai = new OpenAI({
apiKey: config.OpenAIKey
});
irc.on('privmsg', function (event) {
// Only respond to highlights in channels
if (event.message.startsWith(config.Nick + ':')) {
(async () => {
// remove bot name from message
var user_input = event.message.replace(config.Nick + ':', '') + '. Make it short.';
const messages = [];
messages.push({ role: "user", content: user_input });
try {
const chatCompletion = await openai.chat.completions.create({
model: "gpt-4-1106-preview",
messages: messages,
});
if (chatCompletion.choices[0] !== undefined) {
const completion_text = chatCompletion.choices[0].message.content;
console.log(completion_text);
var text = completion_text.replace(/^\s*$(?:\r\n?|\n)/gm, '');
// splice text into lines
var text_array = text.split('\n');
// if array is empty return
if (text_array.length === 0) {
console.log(text);
irc.privmsg(event.target, text);
} else {
// loop through array and send each line
for (var i = 0; i < text_array.length; i++) {
// check if line is empty
if (text_array[i] !== '') {
console.log(text_array[i]);
irc.privmsg(event.target, text_array[i]);
}
}
}
}
} catch (error) {
if (error.response) {
console.log(error.response.status);
console.log(error.response.data);
} else {
console.log(error.message);
}
}
})();
}
});
}
}
// Main Program
console.log('Starting IRC bot\n');
config.forEach(function (value) {
start_bot(value);
});