@@ -36,7 +36,7 @@ function prepareDatabase()
36
36
// close the database if its open
37
37
function closeDatabase ( )
38
38
{
39
- if ( database ) database . close ( ) . catch ( console . error ) ;
39
+ if ( database ) database . close ( ) ; // .catch(console.error);
40
40
}
41
41
42
42
// insert a row into a table
@@ -154,16 +154,20 @@ function scanHistory()
154
154
// and pass them to processMessage (the function right below)
155
155
console . log ( "Preparing to scan chat history..." ) ;
156
156
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 ) ;
160
166
}
161
167
162
168
// scan a text channel's history and process all messages
163
169
function scanChannel ( channel )
164
170
{
165
- // ADD SCANNING FOR MESSAGE UPDATES/EDITS
166
- // i wonder how we should handle edits?
167
171
// fetch messages recursively and then process them
168
172
const limit = 100 ; // how many to fetch at one time
169
173
function fetch ( before )
@@ -172,7 +176,12 @@ function scanChannel(channel)
172
176
messages . forEach ( processMessageEdits ) ; // process the revisions
173
177
// if there's still more to go, then fetch more and recurse!
174
178
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
+ }
176
185
} ) . catch ( console . error ) ;
177
186
} ;
178
187
console . log ( `Scanning channel #${ channel . name } ...` ) ;
@@ -205,21 +214,31 @@ function processMessage(message, edit)
205
214
client . on ( "ready" , ( ) => {
206
215
console . log ( `Logged in as ${ client . user . tag } !` ) ;
207
216
// if configured to archive chat history on login...
208
- if ( config . history ) scanHistory ( ) ;
217
+ if ( config . parse ) scanHistory ( ) ;
209
218
} ) ;
210
219
211
220
// when a discord message is received
212
221
client . on ( "message" , message => {
222
+ if ( config . parse ) return ; // ignore if in parse mode
213
223
logMessage ( message ) ; // log the message to console
214
224
processMessage ( message ) ; // and process it normally
215
225
} ) ;
216
226
217
227
// when a discord message is edited
218
228
client . on ( "messageUpdate" , ( oldMessage , newMessage ) => {
229
+ if ( config . parse ) return ; // ignore if in parse mode
219
230
logMessage ( newMessage , true ) ; // log the message to console
220
231
processMessage ( newMessage , true ) ; // and process it normally
221
232
} ) ;
222
233
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
+
223
242
// exit with error message
224
243
function exit ( message )
225
244
{
0 commit comments