diff --git a/README.md b/README.md index cf80d3f..4ef5694 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ [![License](https://img.shields.io/github/license/discord-tickets/bot?style=flat-square)](https://github.com/discord-tickets/bot/blob/master/LICENSE) ![Codacy grade](https://img.shields.io/codacy/grade/14e6851c85444424b75b8bc3f93e93db?logo=codacy&style=flat-square) [![Discord](https://img.shields.io/discord/451745464480432129?label=discord&color=7289DA&style=flat-square)](https://discord.gg/pXc9vyC) +[![Crowdin](https://badges.crowdin.net/discord-tickets/localized.svg)](https://i18n.discordtickets.app/project/discord-tickets) An open-source ticket management bot for Discord - a free alternative to the premium and white-label plans of other popular ticketing bots. diff --git a/package.json b/package.json index 37c8067..b0f8a92 100644 --- a/package.json +++ b/package.json @@ -34,15 +34,15 @@ "cryptr": "^6.0.2", "discord.js": "^12.5.1", "dotenv": "^8.2.0", - "leeks.js": "^0.0.9", - "leekslazylogger": "^3.0.2", - "leekslazylogger-fastify": "^0.1.0", + "leeks.js": "^0.2.2", + "leekslazylogger-fastify": "^0.1.1", "node-emoji": "^1.10.0", "node-fetch": "^2.6.1", "semver": "^7.3.4", "sequelize": "^6.5.0", "string-argv": "^0.3.1", - "terminal-link": "^2.1.1" + "terminal-link": "^2.1.1", + "to-time-monthsfork": "^1.1.4" }, "devDependencies": { "eslint": "^7.21.0", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index f8f12a5..cc3c46d 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,9 +8,8 @@ specifiers: discord.js: ^12.5.1 dotenv: ^8.2.0 eslint: ^7.21.0 - leeks.js: ^0.0.9 - leekslazylogger: ^3.0.2 - leekslazylogger-fastify: ^0.1.0 + leeks.js: ^0.2.2 + leekslazylogger-fastify: ^0.1.1 mariadb: ^2.5.2 mysql2: ^2.2.5 node-emoji: ^1.10.0 @@ -24,6 +23,7 @@ specifiers: string-argv: ^0.3.1 tedious: ^11.0.3 terminal-link: ^2.1.1 + to-time-monthsfork: ^1.1.4 dependencies: '@eartharoid/i18n': 1.0.0 @@ -32,15 +32,15 @@ dependencies: cryptr: 6.0.2 discord.js: 12.5.1 dotenv: 8.2.0 - leeks.js: 0.0.9 - leekslazylogger: 3.0.2 - leekslazylogger-fastify: 0.1.0 + leeks.js: 0.2.2 + leekslazylogger-fastify: 0.1.1 node-emoji: 1.10.0 node-fetch: 2.6.1 semver: 7.3.4 sequelize: 6.5.0_fb66e8c649bde1be622cc06164c9e22d string-argv: 0.3.1 terminal-link: 2.1.1 + to-time-monthsfork: 1.1.4 optionalDependencies: sqlite3: 5.0.2 @@ -404,6 +404,10 @@ packages: dependencies: tweetnacl: 0.14.5 + /bignumber.js/2.4.0: + resolution: {integrity: sha1-g4qZLan51zfg9LLbC+YrsJ3Qxeg=} + dev: false + /binary-extensions/2.2.0: resolution: {integrity: sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==} engines: {node: '>=8'} @@ -1449,34 +1453,18 @@ packages: package-json: 6.5.0 dev: true - /leeks.js/0.0.9: - resolution: {integrity: sha512-e6UVJ1fj8f2clpHy+KpXVWVxjzB3XYFGyKRJHDlT8Gy/75BT+9bYUacpHSCoXp7RTtyMSr4eBjZrp0nHyyQVbg==} - dev: false - - /leeks.js/0.1.1: - resolution: {integrity: sha512-axz4CyTYNcjmQc19S0I9HSC5ONGflLVm1N0uUz/MdYvuvC94yZUP/panEm9tLAj88DesBSo6D8cuyO/ctB9kYA==} - dev: false - /leeks.js/0.2.2: resolution: {integrity: sha512-cXbDc4a0ft0pQwN2ubagjsT1xKBeW0CP6pPmbUo+KvKynFwaqtGaBjdtpwkBO5NdwSw2OcJ89BYe9mjeZEfzQQ==} dev: false - /leekslazylogger-fastify/0.1.0: - resolution: {integrity: sha512-i0sEvJbzR3gFBA7zf95cMF3CORXfC5Wv4Elv34ZAdjG5zi8S4tZY4Pw5jygG0iu0W1sMxwXR+IY+4Zi9s78zTA==} + /leekslazylogger-fastify/0.1.1: + resolution: {integrity: sha512-O/BFEYpsjCX3/6/GnhwajJ/CA/T9F4AwiRAPyN6y7ET0PgPfv+Q6uLktKiK/ZBcVH0f+VunSPdD0UyTXbzj7wQ==} dependencies: fastify-plugin: 3.0.0 - leekslazylogger: 3.0.1 + leekslazylogger: 3.0.2 on-finished: 2.3.0 dev: false - /leekslazylogger/3.0.1: - resolution: {integrity: sha512-8o0iFuChUyjYYPFz64OfXEdQYn/H4DXyDHyuEP2hFwU/R2Bru4zP4NZbo64PzNwLBDmYZx+06xrsU4oE9WQaJg==} - dependencies: - '@eartharoid/deep-merge': 0.0.1 - '@eartharoid/dtf': 1.0.8 - leeks.js: 0.1.1 - dev: false - /leekslazylogger/3.0.2: resolution: {integrity: sha512-eXgQuEgoSIbtwJRQXy/DHFp1255C4br+x6CtOnrBb5j3GwmeARPMutPur1cMB5YonKBZxFXnvZLqQhr69qKg5A==} dependencies: @@ -2552,6 +2540,13 @@ packages: is-number: 7.0.0 dev: true + /to-time-monthsfork/1.1.4: + resolution: {integrity: sha512-3bWuIwm9QeOAq/UClxFp86QMSJ4GVHmAT8X+pkM0mIMVrpJPLfSieY5qvSsfLJugLNWTVpYJ2ayKWXH3jcAdow==} + engines: {node: '>=4.6'} + dependencies: + bignumber.js: 2.4.0 + dev: false + /toposort-class/1.0.1: resolution: {integrity: sha1-f/0feMi+KMO6Rc1OGj9e4ZO9mYg=} dev: false diff --git a/src/commands/close.js b/src/commands/close.js index 2dbe466..4a936e6 100644 --- a/src/commands/close.js +++ b/src/commands/close.js @@ -1,5 +1,7 @@ const Command = require('../modules/commands/command'); -const { MessageEmbed } = require('discord.js'); +const { MessageEmbed, MessageMentions } = require('discord.js'); +const { Op } = require('sequelize'); +const toTime = require('to-time-monthsfork'); const { footer } = require('../utils/discord'); module.exports = class CloseCommand extends Command { @@ -11,24 +13,232 @@ module.exports = class CloseCommand extends Command { description: i18n('commands.close.description'), aliases: [ i18n('commands.close.aliases.delete'), + i18n('commands.close.aliases.lock'), ], - process_args: false, + process_args: true, args: [ { name: i18n('commands.close.args.ticket.name'), description: i18n('commands.close.args.ticket.description'), example: i18n('commands.close.args.ticket.example'), required: false, + // for arg parsing + alias: i18n('commands.close.args.ticket.alias'), + type: String + }, + { + name: i18n('commands.close.args.reason.name'), + description: i18n('commands.close.args.reason.description'), + example: i18n('commands.close.args.reason.example'), + required: false, + // for arg parsing + alias: i18n('commands.close.args.reason.alias'), + type: String + }, + { + name: i18n('commands.close.args.time.name'), + description: i18n('commands.close.args.time.description'), + example: i18n('commands.close.args.time.example'), + required: false, + // for arg parsing + alias: i18n('commands.close.args.time.alias'), + type: String } ] }); } async execute(message, args) { + const arg_ticket = this.args[0].name; + const arg_reason = this.args[1].name; + const arg_time = this.args[2].name; + let settings = await message.guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); - - this.client.log.info(args) - message.channel.send(args[0]) + + if (args[arg_time]) { + let period; + + try { + period = toTime(args[arg_time]).ms(); + } catch { + return await message.channel.send( + new MessageEmbed() + .setColor(settings.error_colour) + .setTitle(i18n('commands.close.response.invalid_time.title')) + .setDescription(i18n('commands.close.response.invalid_time.description')) + .setFooter(settings.footer, message.guild.iconURL()) + ); + } + + let tickets = await this.client.db.models.Ticket.findAndCountAll({ + where: { + last_message: { + [Op.lte]: new Date(Date.now() - period) + }, + guild: message.guild.id + } + }); + + if (tickets.count === 0) { + return await message.channel.send( + new MessageEmbed() + .setColor(settings.error_colour) + .setTitle(i18n('commands.close.response.no_tickets.title')) + .setDescription(i18n('commands.close.response.no_tickets.description')) + .setFooter(settings.footer, message.guild.iconURL()) + ); + } else { + let collector_message = await message.channel.send( + new MessageEmbed() + .setColor(settings.colour) + .setTitle(i18n('commands.close.response.confirm_multiple.title')) + .setDescription(i18n('commands.close.response.confirm_multiple.description', tickets.count, tickets.count)) + .setFooter(settings.footer, message.guild.iconURL()) + ); + + await collector_message.react('✅'); + + const collector_filter = (reaction, user) => { + return user.id === message.author.id && reaction.emoji.name === '✅'; + }; + + let collector = collector_message.createReactionCollector(collector_filter, { + time: 30000 + }); + + collector.on('collect', async () => { + await collector_message.reactions.removeAll(); + + await message.channel.send( + new MessageEmbed() + .setColor(settings.success_colour) + .setTitle(i18n('commands.close.response.closed_multiple.title', tickets.count, tickets.count)) + .setDescription(i18n('commands.close.response.closed_multiple.description', tickets.count, tickets.count)) + .setFooter(settings.footer, message.guild.iconURL()) + ); + + for (let ticket of tickets.rows) { + await this.client.tickets.close(ticket.id, message.author.id, message.guild.id, args[arg_reason]); + } + + }); + + collector.on('end', async (collected) => { + if (collected.size === 0) { + await collector_message.reactions.removeAll(); + await collector_message.edit( + new MessageEmbed() + .setColor(settings.error_colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle(i18n('commands.close.response.confirmation_timeout.title')) + .setDescription(i18n('commands.close.response.confirmation_timeout.description')) + .setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), message.guild.iconURL()) + ); + setTimeout(async () => { + await collector_message + .delete() + .catch(() => this.client.log.warn('Failed to delete response (collector) message')); + await message + .delete() + .catch(() => this.client.log.warn('Failed to delete original message')); + }, 15000); + } + }); + } + + } else { + let t_row; + if (args[arg_ticket]) { + args[arg_ticket] = args[arg_ticket].replace(MessageMentions.CHANNELS_PATTERN, '$1'); + t_row = await this.client.tickets.resolve(args[arg_ticket], message.guild.id); + + if (!t_row) { + return await message.channel.send( + new MessageEmbed() + .setColor(settings.error_colour) + .setTitle(i18n('commands.close.response.unresolvable.title')) + .setDescription(i18n('commands.close.response.unresolvable.description', args[arg_ticket])) + .setFooter(settings.footer, message.guild.iconURL()) + ); + } + } else { + t_row = await this.client.db.models.Ticket.findOne({ + where: { + id: message.channel.id + } + }); + + if (!t_row) { + return await message.channel.send( + new MessageEmbed() + .setColor(settings.error_colour) + .setTitle(i18n('commands.close.response.not_a_ticket.title')) + .setDescription(i18n('commands.close.response.not_a_ticket.description', settings.command_prefix)) + .setFooter(settings.footer, message.guild.iconURL()) + ); + } + } + + let collector_message = await message.channel.send( + new MessageEmbed() + .setColor(settings.colour) + .setTitle(i18n('commands.close.response.confirm.title')) + .setDescription(i18n('commands.close.response.confirm.description', t_row.number)) + .setFooter(settings.footer, message.guild.iconURL()) + ); + + await collector_message.react('✅'); + + const collector_filter = (reaction, user) => { + return user.id === message.author.id && reaction.emoji.name === '✅'; + }; + + let collector = collector_message.createReactionCollector(collector_filter, { + time: 30000 + }); + + collector.on('collect', async () => { + collector.stop(); + + if (message.channel.id === t_row.id) { + await collector_message.delete(); + } else { + await collector_message.reactions.removeAll(); + await collector_message.edit( + new MessageEmbed() + .setColor(settings.success_colour) + .setTitle(i18n('commands.close.response.closed.title')) + .setDescription(i18n('commands.close.response.closed.description', t_row.number)) + .setFooter(settings.footer, message.guild.iconURL()) + ); + } + + await this.client.tickets.close(t_row.id, message.author.id, message.guild.id, args[arg_reason]); + }); + + collector.on('end', async (collected) => { + if (collected.size === 0) { + await collector_message.reactions.removeAll(); + await collector_message.edit( + new MessageEmbed() + .setColor(settings.error_colour) + .setAuthor(message.author.username, message.author.displayAvatarURL()) + .setTitle(i18n('commands.close.response.confirmation_timeout.title')) + .setDescription(i18n('commands.close.response.confirmation_timeout.description')) + .setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), message.guild.iconURL()) + ); + setTimeout(async () => { + await collector_message + .delete() + .catch(() => this.client.log.warn('Failed to delete response (collector) message')); + await message + .delete() + .catch(() => this.client.log.warn('Failed to delete original message')); + }, 15000); + } + }); + + } } }; \ No newline at end of file diff --git a/src/commands/new.js b/src/commands/new.js index 2f761c8..edcd6e5 100644 --- a/src/commands/new.js +++ b/src/commands/new.js @@ -12,8 +12,9 @@ module.exports = class NewCommand extends Command { name: i18n('commands.new.name'), description: i18n('commands.new.description'), aliases: [ - i18n('commands.new.aliases.open'), i18n('commands.new.aliases.create'), + i18n('commands.new.aliases.open'), + i18n('commands.new.aliases.ticket'), ], process_args: false, args: [ @@ -151,8 +152,11 @@ module.exports = class NewCommand extends Command { }); collector.on('collect', async (reaction) => { + collector.stop(); let index = letters_array.findIndex(value => value === reaction.emoji.name); // find where the letter is in the alphabet - if (index === -1) return await collector_message.delete({ timeout: 15000 }); + if (index === -1) return setTimeout(async () => { + await collector_message.delete(); + }, 15000); await collector_message.reactions.removeAll(); create(categories.rows[index], collector_message); // create the ticket, passing the existing response message to be edited instead of creating a new one }); @@ -165,7 +169,7 @@ module.exports = class NewCommand extends Command { .setColor(settings.error_colour) .setAuthor(message.author.username, message.author.displayAvatarURL()) .setTitle(i18n('commands.new.response.select_category_timeout.title')) - .setDescription(i18n('commands.new.response.select_category_timeout.description', category_list.join('\n'))) + .setDescription(i18n('commands.new.response.select_category_timeout.description')) .setFooter(footer(settings.footer, i18n('message_will_be_deleted_in', 15)), message.guild.iconURL()) ); setTimeout(async () => { diff --git a/src/commands/settings.js b/src/commands/settings.js index e19b3c8..b9dfcef 100644 --- a/src/commands/settings.js +++ b/src/commands/settings.js @@ -81,7 +81,7 @@ module.exports = class SettingsCommand extends Command { let cat_channel = await message.guild.channels.create(c.name, { type: 'category', reason: `Tickets category created by ${message.member.user.tag}`, - position: 0, + position: 1, permissionOverwrites: [ ...[ { diff --git a/src/database/models/ticket.model.js b/src/database/models/ticket.model.js index 41857e6..a14b64b 100644 --- a/src/database/models/ticket.model.js +++ b/src/database/models/ticket.model.js @@ -44,6 +44,10 @@ module.exports = (client, sequelize) => { }, unique: 'number-guild' }, + last_message: { + type: DataTypes.DATE, + allowNull: true, + }, number: { type: DataTypes.INTEGER, allowNull: false, diff --git a/src/listeners/message.js b/src/listeners/message.js index d9decfe..694f76e 100644 --- a/src/listeners/message.js +++ b/src/listeners/message.js @@ -24,16 +24,13 @@ module.exports = class MessageEventListener extends EventListener { }); if (t_row) { - if (settings.log_messages && !message.system) { - this.client.tickets.archives.addMessage(message); // add the message to the archives (if it is in a ticket channel) - } + if (settings.log_messages && !message.system) this.client.tickets.archives.addMessage(message); // add the message to the archives (if it is in a ticket channel) const ignore = [this.client.user.id, t_row.creator]; - if (!t_row.first_response && !ignore.includes(message.author.id)) { - t_row.update({ - first_response: new Date() - }); - } + if (!t_row.first_response && !ignore.includes(message.author.id)) t_row.first_response = new Date(); + + t_row.last_message = new Date(); + await t_row.save(); } else { if (message.author.bot) return; diff --git a/src/listeners/messageReactionAdd.js b/src/listeners/messageReactionAdd.js index 1693728..9496935 100644 --- a/src/listeners/messageReactionAdd.js +++ b/src/listeners/messageReactionAdd.js @@ -86,8 +86,8 @@ module.exports = class MessageReactionAddEventListener extends EventListener { new MessageEmbed() .setColor(settings.colour) .setAuthor(member.user.username, member.user.displayAvatarURL()) - .setTitle(i18n('commands.new.response.claimed.title')) - .setDescription(i18n('commands.new.response.claimed.description', member.toString())) + .setTitle(i18n('ticket.claimed.title')) + .setDescription(i18n('ticket.claimed.description', member.toString())) .setFooter(settings.footer, guild.iconURL()) ); } else { diff --git a/src/listeners/messageReactionRemove.js b/src/listeners/messageReactionRemove.js index 19d4d95..f4002d6 100644 --- a/src/listeners/messageReactionRemove.js +++ b/src/listeners/messageReactionRemove.js @@ -1,7 +1,6 @@ const EventListener = require('../modules/listeners/listener'); const { MessageEmbed } = require('discord.js'); -const { footer } = require('../utils/discord'); module.exports = class MessageReactionRemoveEventListener extends EventListener { constructor(client) { @@ -76,8 +75,8 @@ module.exports = class MessageReactionRemoveEventListener extends EventListener new MessageEmbed() .setColor(settings.colour) .setAuthor(member.user.username, member.user.displayAvatarURL()) - .setTitle(i18n('commands.new.response.released.title')) - .setDescription(i18n('commands.new.response.released.description', member.toString())) + .setTitle(i18n('ticket.released.title')) + .setDescription(i18n('ticket.released.description', member.toString())) .setFooter(settings.footer, guild.iconURL()) ); } else { diff --git a/src/locales/en-GB.json b/src/locales/en-GB.json index 5940499..b21fc73 100644 --- a/src/locales/en-GB.json +++ b/src/locales/en-GB.json @@ -1,8 +1,8 @@ { "bot": { "missing_permissions": { - "title": "⚠️", - "description": "Discord Tickets requires the following permissions:\n%s" + "description": "Discord Tickets requires the following permissions:\n%s", + "title": "⚠️" }, "version": "[Discord Tickets](%s) v%s by [eartharoid](%s)" }, @@ -66,40 +66,85 @@ }, "close": { "aliases": { - "delete": "delete" + "delete": "delete", + "lock": "lock" }, "args": { + "reason": { + "alias": "r", + "description": "The reason for closing the ticket(s)", + "example": "", + "name": "reason" + }, "ticket": { - "description": "The number or a channel mention of the ticket to close", + "alias": "t", + "description": "The ticket to close, either the number or the channel mention/ID", "example": "217", "name": "ticket" + }, + "time": { + "alias": "T", + "description": "Close all tickets that have been inactive for the specified time", + "example": "1w", + "name": "time" } }, "description": "Close a ticket channel", "name": "close", "response": { "closed": { - "description": "This ticket has been closed.\nThe channel will be deleted in 5 seconds.", + "description": "Ticket #%s has been closed.", "title": "✅ Ticket closed" }, - "closed_by_member": { - "description": "This ticket has been closed by %s.\nThe channel will be deleted in 5 seconds.", - "title": "✅ Ticket closed" + "closed_multiple": { + "description": [ + "%d ticket has been closed.", + "%d tickets have been closed." + ], + "title": [ + "✅ Ticket closed", + "✅ Tickets closed" + ] }, - "closed_by_member_with_reason": { - "description": "This ticket has been closed by %s: `%s`\nThe channel will be deleted in 5 seconds.", - "title": "✅ Ticket closed" + "confirm": { + "description": "React with ✅ to close this ticket.", + "description_with_archive": "You will be able to view an archived version of it after.\nReact with ✅ to close this ticket.", + "title": "❔ Are you sure?" }, - "closed_with_reason": { - "description": "This ticket has been closed: `%s`\nThe channel will be deleted in 5 seconds.", - "title": "✅ Ticket closed" + "confirmation_timeout": { + "description": "You took too long to confirm.", + "title": "❌ Reaction time expired" + }, + "confirm_multiple": { + "description": [ + "React with ✅ to close %d ticket.", + "React with ✅ to close %d tickets." + ], + "title": "❔ Are you sure?" + }, + "invalid_time": { + "description": "The time period provided could not be parsed.", + "title": "❌ Invalid input" + }, + "not_a_ticket": { + "description": "Please use this command in a ticket channel or use the ticket flag.\nType `%shelp close` for more information.", + "title": "❌ This isn't a ticket channel" + }, + "no_tickets": { + "description": "There are no tickets which have been inactive for this time period.", + "title": "❌ No tickets to close" + }, + "unresolvable": { + "description": "`%s` could not be resolved to a ticket. Please provide the ticket ID/mention or number.", + "title": "❌ Error" } } }, "new": { "aliases": { "create": "create", - "open": "open" + "open": "open", + "ticket": "ticket" }, "args": { "topic": { @@ -117,10 +162,6 @@ }, "questions": "Please answer the following questions:\n\n%s", "response": { - "claimed": { - "description": "%s has claimed this ticket.", - "title":"✅ Ticket claimed" - }, "created": { "description": "Your ticket has been created: %s.", "title": "✅ Ticket created" @@ -140,10 +181,6 @@ "description": "A server administrator must create at least one ticket category before a new ticket can be opened.", "title": "❌ Can't create ticket" }, - "released": { - "description": "%s has released this ticket.", - "title":"✅ Ticket released" - }, "select_category": { "description": "Select the category most relevant to your ticket's topic:\n\n%s", "title": "🔤 Please select the ticket category" @@ -222,5 +259,31 @@ "staff_only": { "description": "You must be a member of staff to use this command.", "title": "❌" + }, + "ticket": { + "claimed": { + "description": "%s has claimed this ticket.", + "title": "✅ Ticket claimed" + }, + "closed": { + "description": "This ticket has been closed.\nThe channel will be deleted in 5 seconds.", + "title": "✅ Ticket closed" + }, + "closed_by_member": { + "description": "This ticket has been closed by %s.\nThe channel will be deleted in 5 seconds.", + "title": "✅ Ticket closed" + }, + "closed_by_member_with_reason": { + "description": "This ticket has been closed by %s: `%s`\nThe channel will be deleted in 5 seconds.", + "title": "✅ Ticket closed" + }, + "closed_with_reason": { + "description": "This ticket has been closed: `%s`\nThe channel will be deleted in 5 seconds.", + "title": "✅ Ticket closed" + }, + "released": { + "description": "%s has released this ticket.", + "title": "✅ Ticket released" + } } } \ No newline at end of file diff --git a/src/modules/commands/command.js b/src/modules/commands/command.js index bbc1296..764a36f 100644 --- a/src/modules/commands/command.js +++ b/src/modules/commands/command.js @@ -45,7 +45,7 @@ module.exports = class Command { * The command's aliases * @type {string[]} */ - this.aliases = data.aliases || []; + this.aliases = data.aliases ?? []; if (!this.aliases.includes(this.name)) this.aliases.unshift(this.name); @@ -79,12 +79,7 @@ module.exports = class Command { * The command options * @type {CommandArgument[]} */ - this.args = data.args || []; - - for (let arg in this.args) { - if (!this.args[arg].example) - throw new Error(`The "${this.name}" command's "${this.args[arg].name}" argument does not have an example!`); - } + this.args = data.args ?? []; /** * True if command is internal, false if it is from a plugin @@ -132,7 +127,9 @@ module.exports = class Command { const addArgs = (embed, arg) => { let required = arg.required ? '`❗` ' : ''; - embed.addField(required + arg.name, `» ${i18n('cmd_usage.args.description', arg.description)}\n» ${i18n('cmd_usage.args.example', arg.example)}`); + let description = `» ${i18n('cmd_usage.args.description', arg.description)}`; + if (arg.example) description += `\n» ${i18n('cmd_usage.args.example', arg.example)}`; + embed.addField(required + arg.name, description); }; let usage, @@ -141,14 +138,14 @@ module.exports = class Command { if (this.process_args) { usage = `${prefix + cmd_name} ${this.args.map(arg => arg.required ? `<${arg.name}>` : `[${arg.name}]`).join(' ')}`; - example = `${prefix + cmd_name} \n${this.args.map(arg => `--${arg.name} ${arg.example}`).join('\n')}`; + example = `${prefix + cmd_name} \n${this.args.map(arg => `--${arg.name} ${arg.example || ''}`).join('\n')}`; embed = new MessageEmbed() .setColor(settings.error_colour) .setTitle(i18n('cmd_usage.title', cmd_name)) .setDescription(i18n('cmd_usage.named_args') + i18n('cmd_usage.description', usage, example)); } else { usage = `${prefix + cmd_name} ${this.args.map(arg => arg.required ? `<${arg.name}>` : `[${arg.name}]`).join(' ')}`; - example = `${prefix + cmd_name} ${this.args.map(arg => `${arg.example}`).join(' ')}`; + example = `${prefix + cmd_name} ${this.args.map(arg => `${arg.example || ''}`).join(' ')}`; embed = new MessageEmbed() .setColor(settings.error_colour) .setTitle(i18n('cmd_usage.title', cmd_name)) diff --git a/src/modules/plugins/manager.js b/src/modules/plugins/manager.js index b98aa17..b9fcef3 100644 --- a/src/modules/plugins/manager.js +++ b/src/modules/plugins/manager.js @@ -49,7 +49,7 @@ module.exports = class PluginManager { } if (typeof author === 'object') { - author = author.name || 'unknown'; + author = author.name ?? 'unknown'; } let about = { diff --git a/src/modules/plugins/plugin.js b/src/modules/plugins/plugin.js index d54650c..4b077fd 100644 --- a/src/modules/plugins/plugin.js +++ b/src/modules/plugins/plugin.js @@ -38,7 +38,7 @@ module.exports = class Plugin { * The human-friendly name of the plugin * @type {string} */ - this.name = options.name || id; + this.name = options.name ?? id; /** * An array of commands from this plugin diff --git a/src/modules/tickets/manager.js b/src/modules/tickets/manager.js index e2704f5..6d9c86f 100644 --- a/src/modules/tickets/manager.js +++ b/src/modules/tickets/manager.js @@ -20,134 +20,6 @@ module.exports = class TicketManager extends EventEmitter { this.archives = new TicketArchives(this.client); } - /** - * Handle post-creation tasks - * @param {Model} t_row - * @param {Model} cat_row - */ - async postCreate(t_row, cat_row) { - - let guild = this.client.guilds.cache.get(cat_row.guild); - let settings = await guild.settings; - const i18n = this.client.i18n.getLocale(settings.locale); - let member = guild.members.cache.get(t_row.creator); - let t_channel = this.client.channels.cache.get(t_row.id); - - let topic = t_row.topic - ? this.client.cryptr.decrypt(t_row.topic) - : ''; - - if (cat_row.ping instanceof Array) { - let mentions = cat_row.ping.map(id => id === 'everyone' - ? '@everyone' - : id === 'here' - ? '@here' - : `<@&${id}>`); - - await t_channel.send(mentions.join(', ')); - } - - await t_channel.send(member.user.toString()); - - if (cat_row.image) { - await t_channel.send(cat_row.image); - } - - let description = cat_row.opening_message - .replace(/{+\s?(user)?name\s?}+/gi, member.displayName) - .replace(/{+\s?(tag|ping|mention)?\s?}+/gi, member.user.toString()); - let embed = new MessageEmbed() - .setColor(settings.colour) - .setAuthor(member.user.username, member.user.displayAvatarURL()) - .setDescription(description) - .setFooter(settings.footer, guild.iconURL()); - - if (topic) embed.addField(i18n('commands.new.opening_message.fields.topic'), topic); - - let sent = await t_channel.send(embed); - await sent.pin({ reason: 'Ticket opening message' }); - - await t_row.update({ - opening_message: sent.id - }); - - let pinned = t_channel.messages.cache.last(); - - if (pinned.system) { - pinned - .delete({ reason: 'Cleaning up system message' }) - .catch(() => this.client.log.warn('Failed to delete system pin message')); - } - - if (cat_row.claiming) { - await sent.react('🙌'); - } - - let questions; - if (cat_row.opening_questions) { - questions = cat_row.opening_questions - .map((q, index) => `**${index + 1}.** ${q}`) - .join('\n\n'); - } - - if (cat_row.require_topic && topic.length === 0) { - let collector_message = await t_channel.send( - new MessageEmbed() - .setColor(settings.colour) - .setTitle('⚠️ ' + i18n('commands.new.request_topic.title')) - .setDescription(i18n('commands.new.request_topic.description')) - .setFooter(footer(settings.footer, i18n('collector_expires_in', 120)), guild.iconURL()) - ); - - const collector_filter = (message) => message.author.id === t_row.creator; - - let collector = t_channel.createMessageCollector(collector_filter, { - time: 120000 - }); - - collector.on('collect', async (message) => { - topic = message.content; - await t_row.update({ - topic: this.client.cryptr.encrypt(topic) - }); - await t_channel.setTopic(`${member} | ${topic}`, { reason: 'User updated ticket topic' }); - await sent.edit( - new MessageEmbed() - .setColor(settings.colour) - .setAuthor(member.user.username, member.user.displayAvatarURL()) - .setDescription(description) - .addField(i18n('commands.new.opening_message.fields.topic'), topic) - .setFooter(settings.footer, guild.iconURL()) - ); - await message.react('✅'); - collector.stop(); - }); - - collector.on('end', async () => { - collector_message - .delete() - .catch(() => this.client.log.warn('Failed to delete topic collector message')); - if (cat_row.opening_questions) { - await t_channel.send( - new MessageEmbed() - .setColor(settings.colour) - .setDescription(i18n('commands.new.questions', questions)) - .setFooter(settings.footer, guild.iconURL()) - ); - } - }); - } else { - if (cat_row.opening_questions) { - await t_channel.send( - new MessageEmbed() - .setColor(settings.colour) - .setDescription(i18n('commands.new.questions', questions)) - .setFooter(settings.footer, guild.iconURL()) - ); - } - } - } - /** * Create a new ticket * @param {string} guild_id - ID of the guild to create the ticket in @@ -207,12 +79,129 @@ module.exports = class TicketManager extends EventEmitter { topic: topic.length === 0 ? null : this.client.cryptr.encrypt(topic) }); + (async () => { + let settings = await guild.settings; + const i18n = this.client.i18n.getLocale(settings.locale); + + topic = t_row.topic + ? this.client.cryptr.decrypt(t_row.topic) + : ''; + + if (cat_row.ping instanceof Array && cat_row.ping.length > 0) { + let mentions = cat_row.ping.map(id => id === 'everyone' + ? '@everyone' + : id === 'here' + ? '@here' + : `<@&${id}>`); + + await t_channel.send(mentions.join(', ')); + } + + await t_channel.send(member.user.toString()); + + if (cat_row.image) { + await t_channel.send(cat_row.image); + } + + let description = cat_row.opening_message + .replace(/{+\s?(user)?name\s?}+/gi, member.displayName) + .replace(/{+\s?(tag|ping|mention)?\s?}+/gi, member.user.toString()); + let embed = new MessageEmbed() + .setColor(settings.colour) + .setAuthor(member.user.username, member.user.displayAvatarURL()) + .setDescription(description) + .setFooter(settings.footer, guild.iconURL()); + + if (topic) embed.addField(i18n('commands.new.opening_message.fields.topic'), topic); + + let sent = await t_channel.send(embed); + await sent.pin({ reason: 'Ticket opening message' }); + + await t_row.update({ + opening_message: sent.id + }); + + let pinned = t_channel.messages.cache.last(); + + if (pinned.system) { + pinned + .delete({ reason: 'Cleaning up system message' }) + .catch(() => this.client.log.warn('Failed to delete system pin message')); + } + + if (cat_row.claiming) { + await sent.react('🙌'); + } + + let questions; + if (cat_row.opening_questions) { + questions = cat_row.opening_questions + .map((q, index) => `**${index + 1}.** ${q}`) + .join('\n\n'); + } + + if (cat_row.require_topic && topic.length === 0) { + let collector_message = await t_channel.send( + new MessageEmbed() + .setColor(settings.colour) + .setTitle('⚠️ ' + i18n('commands.new.request_topic.title')) + .setDescription(i18n('commands.new.request_topic.description')) + .setFooter(footer(settings.footer, i18n('collector_expires_in', 120)), guild.iconURL()) + ); + + const collector_filter = (message) => message.author.id === t_row.creator; + + let collector = t_channel.createMessageCollector(collector_filter, { + time: 120000 + }); + + collector.on('collect', async (message) => { + topic = message.content; + await t_row.update({ + topic: this.client.cryptr.encrypt(topic) + }); + await t_channel.setTopic(`${member} | ${topic}`, { reason: 'User updated ticket topic' }); + await sent.edit( + new MessageEmbed() + .setColor(settings.colour) + .setAuthor(member.user.username, member.user.displayAvatarURL()) + .setDescription(description) + .addField(i18n('commands.new.opening_message.fields.topic'), topic) + .setFooter(settings.footer, guild.iconURL()) + ); + await message.react('✅'); + collector.stop(); + }); + + collector.on('end', async () => { + collector_message + .delete() + .catch(() => this.client.log.warn('Failed to delete topic collector message')); + if (cat_row.opening_questions) { + await t_channel.send( + new MessageEmbed() + .setColor(settings.colour) + .setDescription(i18n('commands.new.questions', questions)) + .setFooter(settings.footer, guild.iconURL()) + ); + } + }); + } else { + if (cat_row.opening_questions) { + await t_channel.send( + new MessageEmbed() + .setColor(settings.colour) + .setDescription(i18n('commands.new.questions', questions)) + .setFooter(settings.footer, guild.iconURL()) + ); + } + } + })(); + this.client.log.info(`${member.user.tag} created a new ticket in "${guild.name}"`); this.emit('create', t_row.id, creator_id); - this.postCreate(t_row, cat_row); - return t_row; } @@ -234,7 +223,7 @@ module.exports = class TicketManager extends EventEmitter { */ async close(ticket_id, closer_id, guild_id, reason) { let t_row = await this.resolve(ticket_id, guild_id); - if (!t_row) throw new Error(`Could not find a ticket with ID ${ticket_id}`); + if (!t_row) throw new Error(`A ticket with the ID or number "${ticket_id}" could not be resolved`); ticket_id = t_row.id; this.emit('beforeClose', ticket_id); @@ -242,7 +231,7 @@ module.exports = class TicketManager extends EventEmitter { let guild = this.client.guilds.cache.get(t_row.guild); let settings = await guild.settings; const i18n = this.client.i18n.getLocale(settings.locale); - let channel = await this.client.channels.fetch(t_row.channel); + let channel = await this.client.channels.fetch(t_row.id); if (closer_id) { let member = await guild.members.fetch(closer_id); @@ -251,13 +240,13 @@ module.exports = class TicketManager extends EventEmitter { if (channel) { let description = reason - ? i18n('commands.close.response.closed_by_member_with_reason.description', member.user.toString(), reason) - : i18n('commands.close.response.closed_by_member.description', member.user.toString()); + ? i18n('ticket.closed_by_member_with_reason.description', member.user.toString(), reason) + : i18n('ticket.closed_by_member.description', member.user.toString()); await channel.send( new MessageEmbed() .setColor(settings.success_colour) .setAuthor(member.user.username, member.user.displayAvatarURL()) - .setTitle(i18n('commands.close.response.closed.title')) + .setTitle(i18n('ticket.closed.title')) .setDescription(description) .setFooter(settings.footer, guild.iconURL()) ); @@ -271,12 +260,12 @@ module.exports = class TicketManager extends EventEmitter { } else { if (channel) { let description = reason - ? i18n('commands.close.response.closed_with_reason.description') - : i18n('commands.close.response.closed.description'); + ? i18n('ticket.closed_with_reason.description') + : i18n('ticket.closed.description'); await channel.send( new MessageEmbed() .setColor(settings.success_colour) - .setTitle(i18n('commands.close.response.closed.title')) + .setTitle(i18n('ticket.closed.title')) .setDescription(description) .setFooter(settings.footer, guild.iconURL()) ); @@ -294,8 +283,8 @@ module.exports = class TicketManager extends EventEmitter { await t_row.update({ open: false, closed_by: closer_id || null, - closed_reason: reason || null, - pinned: [ ...pinned.keys() ] + closed_reason: reason ? this.client.cryptr.encrypt(reason) : null, + pinned_messages: [...pinned.keys()] }); this.emit('close', ticket_id); @@ -308,23 +297,23 @@ module.exports = class TicketManager extends EventEmitter { * @param {string} [guild_id] - The ID of the ticket's guild (used if a ticket number is provided instead of ID) */ async resolve(ticket_id, guild_id) { - if (!this.client.channels.resolve(ticket_id)) { - let t_row = await this.client.db.models.Ticket.findOne({ + let t_row; + + if (this.client.channels.resolve(ticket_id)) { + t_row = await this.client.db.models.Ticket.findOne({ where: { - number: ticket_id, - guild_id + id: ticket_id + } + }); + } else { + t_row = await this.client.db.models.Ticket.findOne({ + where: { + number: ticket_id, + guild: guild_id } }); - if (!t_row) return null; - ticket_id = t_row.id; } - let t_row = await this.client.db.models.Ticket.findOne({ - where: { - id: ticket_id - } - }); - return t_row; } diff --git a/user/example.config.js b/user/example.config.js index 6aca452..9361ac0 100644 --- a/user/example.config.js +++ b/user/example.config.js @@ -46,7 +46,7 @@ module.exports = { type: 'PLAYING' }, { - activity: 'for new tickets', + activity: 'tickets', type: 'WATCHING' }, /* { // an example