Skip to content

Commit d63964a

Browse files
committed
made more generic
1 parent 4e19054 commit d63964a

File tree

1 file changed

+15
-23
lines changed

1 file changed

+15
-23
lines changed

bot.js

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
// things that'd be nice to clean up:
2-
// make the history scanner also scan message revisions
3-
// make `insertRow` generic (insert varable num of values into varable table; no case analysis)
4-
51
// load discord.js and make a new client object
62
const fs = require("fs");
73
const sqlite3 = require("sqlite3");
@@ -43,27 +39,14 @@ function closeDatabase()
4339
}
4440

4541
// insert a row into a table
46-
// i hope we can rewrite this function better :p
4742
// should we die on failure to insert row or stay alive?
4843
function insertRow(table, values)
4944
{
50-
// i'm not sure how to do this SQL more generically,
51-
// to get rid of this case analysis
52-
// someone help? :P
53-
if(table === "messages")
54-
{
55-
database.run(`INSERT INTO messages(id, author) VALUES(?, ?)`,
56-
values, e => {
57-
if(e) console.error(`Error inserting row: ${e}`);
58-
});
59-
}
60-
else if(table === "revisions")
61-
{
62-
database.run(`INSERT INTO revisions(id, timestamp, content) VALUES(?, ?, ?)`,
63-
values, e => {
64-
if(e) console.error(`Error inserting row: ${e}`);
65-
});
66-
}
45+
// can this sql be done more prettily?
46+
database.run(`INSERT INTO ${table} VALUES(${ new Array(values.length).fill("?").join(", ") })`,
47+
(values, e) => {
48+
if(e) console.error(`Error inserting row: ${e}`);
49+
});
6750
}
6851

6952
// get the latest timestamp for a message object
@@ -168,7 +151,7 @@ function scanChannel(channel)
168151
function fetch(before)
169152
{
170153
channel.fetchMessages({ limit: limit, before: before }).then(messages => {
171-
messages.forEach(processMessage); // process each message normally
154+
messages.forEach(processMessageEdits); // process the revisions
172155
// if there's still more to go, then fetch more and recurse!
173156
if(messages.size == limit) fetch(messages.last().id);
174157
else console.log(`Finished scanning channel #${channel.name}!`);
@@ -178,6 +161,15 @@ function scanChannel(channel)
178161
fetch();
179162
}
180163

164+
// process all the revisions of a message
165+
function processMessageEdits(message)
166+
{
167+
const revisions = message.edits.reverse(); // reverse so original is first
168+
processMessage(revisions[0]); // process the original message
169+
revisions.slice(1).forEach(message =>
170+
processMessage(message, true)); // process edits
171+
}
172+
181173
// process a discord message
182174
function processMessage(message, edit)
183175
{

0 commit comments

Comments
 (0)