diff --git a/package-lock.json b/package-lock.json index 9c5eb25..6dc7139 100644 --- a/package-lock.json +++ b/package-lock.json @@ -325,6 +325,11 @@ } } }, + "bignumber.js": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-2.4.0.tgz", + "integrity": "sha1-g4qZLan51zfg9LLbC+YrsJ3Qxeg=" + }, "binary-extensions": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz", @@ -2543,6 +2548,14 @@ "is-number": "^7.0.0" } }, + "to-time-monthsfork": { + "version": "1.1.3", + "resolved": "https://registry.npmjs.org/to-time-monthsfork/-/to-time-monthsfork-1.1.3.tgz", + "integrity": "sha512-SykQ/IAyihZJVtQUjB04ThztVYdQBFx9birBLyCm6i62bZJx8GiuDVis/l9VBPWWsz5NuT5gr7HCrrAb/2OIEg==", + "requires": { + "bignumber.js": "^2.4.0" + } + }, "toposort-class": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/toposort-class/-/toposort-class-1.0.1.tgz", diff --git a/package.json b/package.json index 88591bb..180887d 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,8 @@ "n-readlines": "^1.0.1", "node-fetch": "^2.6.1", "sequelize": "^6.4.0", - "terminal-link": "^2.1.1" + "terminal-link": "^2.1.1", + "to-time-monthsfork": "^1.1.3" }, "optionalDependencies": { "sqlite3": "^5.0.0" diff --git a/src/commands/closeall.js b/src/commands/closeall.js index f6fe8b8..25f4caf 100644 --- a/src/commands/closeall.js +++ b/src/commands/closeall.js @@ -12,15 +12,18 @@ const fs = require('fs'); const { join } = require('path'); const config = require(join(__dirname, '../../user/', require('../').config)); const archive = require('../modules/archive'); +const { plural } = require('../modules/utils'); +const { Op } = require('sequelize'); +const toTime = require('to-time-monthsfork'); // A slight modification to the 'close' command to allow multiple tickets to be closed at once module.exports = { name: 'closeall', - description: 'Closes all currently open tickets.', - usage: '', + description: 'Closes all currently open tickets older than a specified time length', + usage: '[time]', aliases: ['ca'], - example: 'closeall', + example: 'closeall 1mo 1w', args: false, disabled: !config.commands.closeall.enabled, async execute(client, message, args, { @@ -41,11 +44,43 @@ module.exports = { .setFooter(guild.name, guild.iconURL()) ); - let tickets = await Ticket.findAndCountAll({ - where: { - open: true, - }, - }); + let tickets; + + if (args.length > 0) { + let time, maxDate; + let timestamp = args.join(' '); + + try { + time = toTime(timestamp).milliseconds(); + maxDate = new Date(Date.now() - time); + } catch (error) { + return message.channel.send( + new MessageEmbed() + .setColor(config.err_colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle('❌ **Invalid Timestamp**') + .setDescription(`The timestamp that you specified, \`${timestamp}\`, was invalid.`) + .addField('Usage', `\`${config.prefix}${this.name}${' ' + this.usage}\`\n`) + .addField('Help', `Type \`${config.prefix}help ${this.name}\` for more information`) + .setFooter(guild.name, guild.iconURL()) + ); + } + + tickets = await Ticket.findAndCountAll({ + where: { + open: true, + updatedAt: { + [Op.lte]: maxDate, + } + }, + }); + } else { + tickets = await Ticket.findAndCountAll({ + where: { + open: true, + }, + }); + } if (tickets.count === 0) return message.channel.send( @@ -207,7 +242,7 @@ module.exports = { let embed = new MessageEmbed() .setColor(config.colour) .setAuthor(message.author.username, message.author.displayAvatarURL()) - .setTitle(`${tickets.count} ticket${tickets.count > 1 ? 's' : ''} closed (${config.prefix}closeall)`) + .setTitle(`${tickets.count} ${plural('ticket', tickets.count)} closed (${config.prefix}closeall)`) .addField('Closed by', message.author, true) .setFooter(guild.name, guild.iconURL()) .setTimestamp(); @@ -222,4 +257,4 @@ module.exports = { } }, -}; \ No newline at end of file +}; diff --git a/src/events/message.js b/src/events/message.js index f5aea2a..58bc428 100644 --- a/src/events/message.js +++ b/src/events/message.js @@ -30,7 +30,12 @@ Type \`${config.prefix}new\` on the server to create a new ticket.`); */ let ticket = await Ticket.findOne({ where: { channel: message.channel.id } }); - if (ticket) archive.add(message); // add message to archive + if (ticket) { + archive.add(message); // add message to archive + // Update the ticket updated at so closeall can get most recent + ticket.changed('updatedAt', true); + ticket.save(); + } if (message.author.bot || message.author.id === client.user.id) return; // goodbye bots