closes all tickets before a specified date

This commit is contained in:
Oliver Cordingley 2021-01-23 17:45:35 +00:00
parent 33b300f2b3
commit a0a15c4551
No known key found for this signature in database
GPG Key ID: BDD85B6392EE03EA
3 changed files with 48 additions and 13 deletions

8
package-lock.json generated
View File

@ -2526,10 +2526,10 @@
"is-number": "^7.0.0" "is-number": "^7.0.0"
} }
}, },
"to-time": { "to-time-monthsfork": {
"version": "1.0.2", "version": "1.1.3",
"resolved": "https://registry.npmjs.org/to-time/-/to-time-1.0.2.tgz", "resolved": "https://registry.npmjs.org/to-time-monthsfork/-/to-time-monthsfork-1.1.3.tgz",
"integrity": "sha1-T4FFoH2F9jVqYuHOoKep5mYXduM=", "integrity": "sha512-SykQ/IAyihZJVtQUjB04ThztVYdQBFx9birBLyCm6i62bZJx8GiuDVis/l9VBPWWsz5NuT5gr7HCrrAb/2OIEg==",
"requires": { "requires": {
"bignumber.js": "^2.4.0" "bignumber.js": "^2.4.0"
} }

View File

@ -14,7 +14,7 @@
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"sequelize": "^6.3.5", "sequelize": "^6.3.5",
"terminal-link": "^2.1.1", "terminal-link": "^2.1.1",
"to-time": "^1.0.2" "to-time-monthsfork": "^1.1.3"
}, },
"optionalDependencies": { "optionalDependencies": {
"sqlite3": "^5.0.0" "sqlite3": "^5.0.0"

View File

@ -13,15 +13,18 @@ const { join } = require('path');
const config = require(join(__dirname, '../../user/', require('../').config)); const config = require(join(__dirname, '../../user/', require('../').config));
const archive = require('../modules/archive'); const archive = require('../modules/archive');
const { plural } = require('../modules/utils'); 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 // A slight modification to the 'close' command to allow multiple tickets to be closed at once
module.exports = { module.exports = {
name: 'closeall', name: 'closeall',
description: 'Closes all currently open tickets.', description: 'Closes all currently open tickets older than a specified time length',
usage: '', usage: '<timestamp>',
aliases: ['ca'], aliases: ['ca'],
example: 'closeall', example: 'closeall 1y 2m',
args: false, args: false,
disabled: !config.commands.closeall.enabled, disabled: !config.commands.closeall.enabled,
async execute(client, message, args, { async execute(client, message, args, {
@ -42,11 +45,43 @@ module.exports = {
.setFooter(guild.name, guild.iconURL()) .setFooter(guild.name, guild.iconURL())
); );
let tickets = await Ticket.findAndCountAll({ let tickets;
where: {
open: true, 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) if (tickets.count === 0)
return message.channel.send( return message.channel.send(