diff --git a/structure/Client.js b/structure/Client.js index 896cbed..1da81a9 100644 --- a/structure/Client.js +++ b/structure/Client.js @@ -57,7 +57,11 @@ class ModmailClient extends Client { this.modmail.init(); process.on('exit', this.saveCache.bind(this)); - process.on('SIGINT', this.saveCache.bind(this)); + process.on('SIGINT', () => { + this.saveCache.bind(this); + // eslint-disable-next-line no-process-exit + process.exit(); + }); this._ready = true; diff --git a/structure/Modmail.js b/structure/Modmail.js index 47a690f..f965a9d 100644 --- a/structure/Modmail.js +++ b/structure/Modmail.js @@ -510,7 +510,10 @@ class Modmail { if (this.mmcache[userId]) return resolve(this.mmcache[userId]); const path = `./modmail_cache/${userId}.json`; - if (!fs.existsSync(path)) return resolve([]); + if (!fs.existsSync(path)) { + this.mmcache[userId] = []; + return resolve(this.mmcache[userId]); + } fs.readFile(path, { encoding: 'utf-8' }, (err, data) => { if (err) reject(err); @@ -532,7 +535,11 @@ class Modmail { for (const id of toSave) { const path = `./modmail_cache/${id}.json`; - fs.writeFileSync(path, JSON.stringify(this.mmcache[id])); + try { + fs.writeFileSync(path, JSON.stringify(this.mmcache[id])); + } catch (err) { + this.client.logger.error(`Error during saving of history\n${id}\n${JSON.stringify(this.mmcache)}\n${err.stack}`); + } } }