diff --git a/package-lock.json b/package-lock.json index 569dd66..c69a0c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2526,10 +2526,10 @@ "is-number": "^7.0.0" } }, - "to-time": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/to-time/-/to-time-1.0.2.tgz", - "integrity": "sha1-T4FFoH2F9jVqYuHOoKep5mYXduM=", + "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" } diff --git a/package.json b/package.json index 647b2aa..8565a97 100644 --- a/package.json +++ b/package.json @@ -14,7 +14,7 @@ "node-fetch": "^2.6.1", "sequelize": "^6.3.5", "terminal-link": "^2.1.1", - "to-time": "^1.0.2" + "to-time-monthsfork": "^1.1.3" }, "optionalDependencies": { "sqlite3": "^5.0.0" diff --git a/src/commands/closeall.js b/src/commands/closeall.js index cf27633..8eee123 100644 --- a/src/commands/closeall.js +++ b/src/commands/closeall.js @@ -13,15 +13,18 @@ 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'); +const { time } = require('@eartharoid/dtf'); // 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: '', aliases: ['ca'], - example: 'closeall', + example: 'closeall 1y 2m', args: false, disabled: !config.commands.closeall.enabled, async execute(client, message, args, { @@ -42,11 +45,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(