/
sweep.js
235 lines (183 loc) · 6.2 KB
/
sweep.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
const Discord = require('discord.js');
const fs = require('fs');
const config = require('./config.json');
config.num_contexts = 0;
var dictionary = {
"lines": {},
"words": {}
};
var client;
// Initializes the bot
function startUp() {
// Read dictionary file and parse lines
loadDictionary();
// Create discord client and connect
client = new Discord.Client();
// Set handlers for client events
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
});
client.on('message', handleMessage);
client.on('error', console.error);
// Log in to discord
if (!config.discordToken) {
console.log("ERROR: Please set a Discord API token in config.json!")
console.log("You can create a token on https://discordapp.com/developers")
} else {
client.login(config.discordToken);
}
setInterval(saveDictionary, config.autosave_seconds*1000)
}
// Reply to a message and/or learn its contents
function handleMessage(msg) {
let hasTrigger = 0;
let body = msg.content
// Search for trigger words
for (var t in config.triggers) {
if (body.toLowerCase().includes(config.triggers[t].toLowerCase())) hasTrigger = 1;
}
body = body.trim();
// Check if we want to reply
if (msg.author != client.user && (hasTrigger || (Math.random()*100 < config.replyRate))) {
response = buildReply(body);
if (response && response != "") {
msg.channel.startTyping();
setTimeout(() => {
msg.reply(response);
msg.channel.stopTyping();
}, Math.floor(Math.random()*(config.maxReplyDelay-config.minReplyDelay))+config.minReplyDelay)
}
}
if (config.learning && msg.author != client.user && !(hasTrigger && config.ignoreTriggerLearning)) {
learnMessage(msg.content)
}
}
// Load dictionary file from disk and learn lines
function loadDictionary() {
try {
var lines = fs.readFileSync(config.dictFile, "utf-8");
} catch (e) {
console.log("Could not read dictionary file", config.dictFile, ", creating a new one.")
var lines = "";
}
lines = lines.split("\n");
for (let l in lines) {
learnLine(lines[l]);
}
}
// Save dictionary lines to file
function saveDictionary() {
try {
var lines = [];
for (var l in dictionary.lines) {
lines.push(l);
}
fs.writeFileSync(config.dictFile, lines.join("\n"));
} catch (e) {
console.log("Could not open dictionary file", config.dictFile, ", can't save lines.")
}
}
// Filter rules from SeeBorg's FilterMessage
function filterMessage(message) {
message = message.replace("\t", " ");
message = message.replace("\r", " ");
message = message.replace("\"", "");
message = message.replace("? ", "?.");
message = message.replace("! ", "!.");
return message.toLowerCase();
}
// Splits a message by punctuation and sends the individual sentences to learnLine
function learnMessage(message) {
message = filterMessage(message);
message = message.split(/[\.|\n]/)
for (let m in message) {
learnLine(message[m]);
}
}
// Learns a new line
function learnLine(line) {
// check to see if we've learned this line already
if (dictionary.lines[line] != undefined) {
dictionary.lines[line]++;
return false;
}
// Mark line as known
dictionary.lines[line] = 1;
// For each word, remember its context
let words = line.split(" ");
for (let w in words) {
if (dictionary.words[words[w]] != undefined) {
dictionary.words[words[w]].push([line, w]);
} else {
dictionary.words[words[w]] = [[line, w]];
}
}
}
// Build a reply for a given message, according to SeeBorg's method
function buildReply(message) {
let words = message.replace("?", "").split(" ")
let known = []
if (words.length == 0) return "";
// Check for words we know
for (let w in words) {
if (dictionary.words[words[w]] != undefined) {
known.push(words[w]);
}
}
if (known.length == 0) return;
// And select one known word at random to start building a reply
var reply = [known[Math.floor(Math.random()*known.length)]]
// Start building
// build to left
let done = false;
while (!done) {
var contexts = dictionary.words[reply[0]];
var next = contexts[Math.floor(Math.random()*contexts.length)];
var position = parseInt(next[1]);
var ctxwords = next[0].split(" ");
var depth = Math.floor(Math.random()*(config.max_context_depth-config.min_context_depth))+config.min_context_depth;
for (let i = 1; i<=depth; i++) {
if ((position-i) < 0) {
done = true;
break;
} else {
reply.unshift(ctxwords[position-i])
}
if (position-i == 0) {
done = true;
break;
}
}
}
// build to right
done = false;
while (!done) {
var contexts = dictionary.words[reply[reply.length-1]];
var next = contexts[Math.floor(Math.random()*contexts.length)];
var position = parseInt(next[1]);
var ctxwords = next[0].split(" ");
let depth = Math.floor(Math.random()*(config.max_context_depth-config.min_context_depth))+config.min_context_depth;
for (let i = 1; i<=depth; i++) {
if ((position+i) >= ctxwords.length) {
done = true;
break;
} else {
reply.push(ctxwords[position+i])
}
}
}
return reply.join(" ");
}
startUp();
function convert(file, biblemode) {
var lines = fs.readFileSync(file, "utf-8");
lines = lines.replace("\n", "")
lines = lines.replace("\r", "")
lines = lines.replace("\"", "");
if (biblemode) lines = lines.replace(/[A-z][A-z][A-z] [0-9]*:[0-9]* /g, '');
lines = lines.replace(/[^a-zA-ZäöüÄÖÜß\- 0-9.]+/g,' ');
lines = lines.replace(/ +/g, ' ');
console.log(lines.indexOf("\n"));
fs.writeFileSync("test.txt", lines)
learnMessage(lines)
}