Merge pull request #93 from OliverCordingl1/closeall-args

Closeall args
This commit is contained in:
Isaac 2021-01-25 19:59:17 +00:00 committed by GitHub
commit c2d48f904c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 66 additions and 12 deletions

13
package-lock.json generated
View File

@ -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",

View File

@ -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"

View File

@ -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();

View File

@ -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