Skip to content

Commit dcf502f

Browse files
committed
'parse' option; quit after history scan
1 parent 828557d commit dcf502f

File tree

2 files changed

+29
-10
lines changed

2 files changed

+29
-10
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ module.exports = config;
1414

1515
config.domains = ["pastebin.com"]; // pastebin/etc domans
1616
config.dbFilename = "database.db"; // the database file to store the collection
17-
config.history = false; // archive chat history on login
17+
config.parse = false; // archive chat history on login
1818
config.port = 8080; // the port to server the http frontend on
1919
config.token = "PUT YOUR TOKEN HERE"; // discord login token
2020
```
2121

22-
Run the bot with `npm start`.
22+
Run the bot with `npm run bot`.

bot.js

100755100644
Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function prepareDatabase()
3636
// close the database if its open
3737
function closeDatabase()
3838
{
39-
if(database) database.close().catch(console.error);
39+
if(database) database.close();//.catch(console.error);
4040
}
4141

4242
// insert a row into a table
@@ -154,16 +154,20 @@ function scanHistory()
154154
// and pass them to processMessage (the function right below)
155155
console.log("Preparing to scan chat history...");
156156

157-
// for every text channel of every guild the bot is in...
158-
client.guilds.forEach(guild => guild.channels
159-
.filter(channel => channel.type === "text").forEach(scanChannel));
157+
// get every text channel of every guild the bot is in...
158+
const channels = [].concat.apply([], client.guilds.map(guild => guild.channels.array()
159+
.filter(channel => channel.type === "text")));
160+
161+
// track how many channels there are to be scanned
162+
channelsRemaining = channels.length;
163+
164+
// scan the channels!
165+
channels.forEach(scanChannel);
160166
}
161167

162168
// scan a text channel's history and process all messages
163169
function scanChannel(channel)
164170
{
165-
// ADD SCANNING FOR MESSAGE UPDATES/EDITS
166-
// i wonder how we should handle edits?
167171
// fetch messages recursively and then process them
168172
const limit = 100; // how many to fetch at one time
169173
function fetch(before)
@@ -172,7 +176,12 @@ function scanChannel(channel)
172176
messages.forEach(processMessageEdits); // process the revisions
173177
// if there's still more to go, then fetch more and recurse!
174178
if(messages.size == limit) fetch(messages.last().id);
175-
else console.log(`Finished scanning channel #${channel.name}!`);
179+
else
180+
{
181+
console.log(`Finished scanning channel #${channel.name}!`);
182+
// if that's all of them, then quit!
183+
if(--channelsRemaining == 0) quit("Scanning complete.");
184+
}
176185
}).catch(console.error);
177186
};
178187
console.log(`Scanning channel #${channel.name}...`);
@@ -205,21 +214,31 @@ function processMessage(message, edit)
205214
client.on("ready", () => {
206215
console.log(`Logged in as ${client.user.tag}!`);
207216
// if configured to archive chat history on login...
208-
if(config.history) scanHistory();
217+
if(config.parse) scanHistory();
209218
});
210219

211220
// when a discord message is received
212221
client.on("message", message => {
222+
if(config.parse) return; // ignore if in parse mode
213223
logMessage(message); // log the message to console
214224
processMessage(message); // and process it normally
215225
});
216226

217227
// when a discord message is edited
218228
client.on("messageUpdate", (oldMessage, newMessage) => {
229+
if(config.parse) return; // ignore if in parse mode
219230
logMessage(newMessage, true); // log the message to console
220231
processMessage(newMessage, true); // and process it normally
221232
});
222233

234+
// quit the process peacefully with a message
235+
function quit(message)
236+
{
237+
console.log(message);
238+
closeDatabase();
239+
process.exit(0);
240+
}
241+
223242
// exit with error message
224243
function exit(message)
225244
{

0 commit comments

Comments
 (0)