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
-
5
1
// load discord.js and make a new client object
6
2
const fs = require ( "fs" ) ;
7
3
const sqlite3 = require ( "sqlite3" ) ;
@@ -43,27 +39,14 @@ function closeDatabase()
43
39
}
44
40
45
41
// insert a row into a table
46
- // i hope we can rewrite this function better :p
47
42
// should we die on failure to insert row or stay alive?
48
43
function insertRow ( table , values )
49
44
{
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
+ } ) ;
67
50
}
68
51
69
52
// get the latest timestamp for a message object
@@ -168,7 +151,7 @@ function scanChannel(channel)
168
151
function fetch ( before )
169
152
{
170
153
channel . fetchMessages ( { limit : limit , before : before } ) . then ( messages => {
171
- messages . forEach ( processMessage ) ; // process each message normally
154
+ messages . forEach ( processMessageEdits ) ; // process the revisions
172
155
// if there's still more to go, then fetch more and recurse!
173
156
if ( messages . size == limit ) fetch ( messages . last ( ) . id ) ;
174
157
else console . log ( `Finished scanning channel #${ channel . name } !` ) ;
@@ -178,6 +161,15 @@ function scanChannel(channel)
178
161
fetch ( ) ;
179
162
}
180
163
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
+
181
173
// process a discord message
182
174
function processMessage ( message , edit )
183
175
{
0 commit comments