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": { "binary-extensions": {
"version": "2.1.0", "version": "2.1.0",
"resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz", "resolved": "https://registry.npmjs.org/binary-extensions/-/binary-extensions-2.1.0.tgz",
@ -2543,6 +2548,14 @@
"is-number": "^7.0.0" "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": { "toposort-class": {
"version": "1.0.1", "version": "1.0.1",
"resolved": "https://registry.npmjs.org/toposort-class/-/toposort-class-1.0.1.tgz", "resolved": "https://registry.npmjs.org/toposort-class/-/toposort-class-1.0.1.tgz",

View File

@ -13,7 +13,8 @@
"n-readlines": "^1.0.1", "n-readlines": "^1.0.1",
"node-fetch": "^2.6.1", "node-fetch": "^2.6.1",
"sequelize": "^6.4.0", "sequelize": "^6.4.0",
"terminal-link": "^2.1.1" "terminal-link": "^2.1.1",
"to-time-monthsfork": "^1.1.3"
}, },
"optionalDependencies": { "optionalDependencies": {
"sqlite3": "^5.0.0" "sqlite3": "^5.0.0"

View File

@ -12,15 +12,18 @@ const fs = require('fs');
const { join } = require('path'); 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 { 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 // 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: '[time]',
aliases: ['ca'], aliases: ['ca'],
example: 'closeall', example: 'closeall 1mo 1w',
args: false, args: false,
disabled: !config.commands.closeall.enabled, disabled: !config.commands.closeall.enabled,
async execute(client, message, args, { async execute(client, message, args, {
@ -41,11 +44,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(
@ -207,7 +242,7 @@ module.exports = {
let embed = new MessageEmbed() let embed = new MessageEmbed()
.setColor(config.colour) .setColor(config.colour)
.setAuthor(message.author.username, message.author.displayAvatarURL()) .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) .addField('Closed by', message.author, true)
.setFooter(guild.name, guild.iconURL()) .setFooter(guild.name, guild.iconURL())
.setTimestamp(); .setTimestamp();
@ -222,4 +257,4 @@ module.exports = {
} }
}, },
}; };

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 } }); 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 if (message.author.bot || message.author.id === client.user.id) return; // goodbye bots