diff --git a/structure/ChannelHandler.js b/structure/ChannelHandler.js index a15c423..d260784 100644 --- a/structure/ChannelHandler.js +++ b/structure/ChannelHandler.js @@ -75,9 +75,13 @@ class ChannelHandler { error: true, msg: `Internal error, this has been logged.` }; - history.push({ author: staff.id, timestamp: Date.now(), markread: true }); // To keep track of read state + if (!history.length) return { + error: true, + msg: `User has no modmail history.` + }; + if(!history[history.length-1].markread) history.push({ author: staff.id, timestamp: Date.now(), markread: true }); // To keep track of read state - await channel.edit({ parentID: this.readMail.id, lockPermissions: true }); + if (channel) await channel.edit({ parentID: this.readMail.id, lockPermissions: true }); if (!this.cache.updatedThreads.includes(target)) this.cache.updatedThreads.push(target); if (this.cache.queue.includes(target)) this.cache.queue.splice(this.cache.queue.indexOf(target), 1); return {}; diff --git a/structure/Client.js b/structure/Client.js index b5d331e..ff31be3 100644 --- a/structure/Client.js +++ b/structure/Client.js @@ -40,8 +40,9 @@ class ModmailClient extends Client { this.cache.load(); this.logger.info(`Logging in`); + const promise = this.ready(); await this.login(this._options.discordToken); - await this.ready(); + await promise; this.mainServer = this.guilds.cache.get(this._options.mainGuild); this.bansServer = this.guilds.cache.get(this._options.bansGuild) || null; @@ -55,8 +56,8 @@ class ModmailClient extends Client { }); process.on('SIGINT', () => { this.logger.warn('received sigint'); - this.cache.save(); - this.cache.saveModmailHistory(this.modmail); + //this.cache.save(); + //this.cache.saveModmailHistory(this.modmail); // eslint-disable-next-line no-process-exit process.exit(); }); diff --git a/structure/Modmail.js b/structure/Modmail.js index e433998..3ab1847 100644 --- a/structure/Modmail.js +++ b/structure/Modmail.js @@ -298,14 +298,44 @@ class Modmail { msg: `This command only works in modmail channels without arguments.` }; - // Eventually support marking several threads read at the same time - const [id] = args; - let channel = null; - const _user = await this.client.resolveUser(id, true); - if (!args.length) ({ channel } = message); - else if (this.cache.channels[_user.id]) channel = this.client.channels.resolve(this.cache.channels[_user.id]); - else channel = await this.client.resolveChannel(id); + if (args.length) { + // Eventually support marking several threads read at the same time + const [id] = args; + let user = await this.client.resolveUser(id, true); + let channel = await this.client.resolveChannel(id); + + if (channel) { + + const chCache = this.cache.channels; + const result = Object.entries(chCache).find(([, val]) => { + return val === channel.id; + }); + + if (!result) return { + error: true, + msg: `That doesn't seem to be a valid modmail channel. Cache might be out of sync. **[MISSING TARGET]**` + }; + + user = await this.client.resolveUser(result[0]); + const response = await this.channels.markread(user.id, channel, author); + if (response.error) return response; + return 'Done'; + + } else if (user) { + + const _ch = this.cache.channels[user.id]; + if (_ch) channel = await this.client.resolveChannel(_ch); + + const response = await this.channels.markread(user.id, channel, author); + if (response.error) return response; + return 'Done'; + + } + + } + + const { channel } = message; const chCache = this.cache.channels; const result = Object.entries(chCache).find(([, val]) => { return val === channel.id; diff --git a/structure/Resolver.js b/structure/Resolver.js index c9a2b2b..694461a 100644 --- a/structure/Resolver.js +++ b/structure/Resolver.js @@ -86,7 +86,7 @@ class Resolver { if (typeof resolveables === 'string') resolveables = [resolveables]; if (resolveables.length === 0) return false; - if (!guild) return false; + if (!guild) guild = this.client.mainServer; const CM = guild.channels; const resolved = []; diff --git a/structure/commands/Logs.js b/structure/commands/Logs.js index e7d926b..a022343 100644 --- a/structure/commands/Logs.js +++ b/structure/commands/Logs.js @@ -25,7 +25,7 @@ class Logs extends Command { } const { member, channel } = message; - const history = await this.client.modmail.loadHistory(user.id); + const history = await this.client.cache.loadModmailHistory(user.id); if (!history.length) return 'Not found in modmail DB'; const page = this.paginate([...history].filter((e) => !e.markread).reverse(), pageNr, 10);