modmail/structure/commands/CannedReply.js

84 lines
2.8 KiB
JavaScript
Raw Normal View History

2021-06-19 15:06:20 +02:00
const Command = require('../Command');
class CannedReply extends Command {
2021-10-22 09:35:04 +02:00
constructor (client) {
2021-06-19 15:06:20 +02:00
super(client, {
name: 'cannedreply',
2021-10-22 09:35:04 +02:00
aliases: [ 'cr', 'canned' ],
2021-06-19 15:06:20 +02:00
showUsage: true,
usage: `<canned response name>`
});
}
2021-10-22 09:35:04 +02:00
async execute (message, { args }) {
2021-06-19 22:41:51 +02:00
2021-10-22 09:35:04 +02:00
const [ first ] = args.map((a) => a);
2021-06-19 15:06:20 +02:00
// eslint-disable-next-line prefer-const
2021-06-19 20:05:32 +02:00
let { channel, content, _caller } = message,
2021-06-19 15:06:20 +02:00
anon = false;
content = content.replace(`${this.client.prefix}${_caller}`, '');
2021-06-19 20:05:32 +02:00
const op = args.shift().toLowerCase();
if (op === 'anon') {
2021-06-19 15:06:20 +02:00
anon = true;
content = content.replace(first, '');
2021-10-22 09:35:04 +02:00
} else if ([ 'create', 'delete' ].includes(op)) {
2021-06-19 20:05:32 +02:00
return this.createCanned(op, args, message);
2021-10-22 09:35:04 +02:00
} else if ([ 'list' ].includes(first.toLowerCase(op))) {
2021-06-19 20:05:32 +02:00
const list = Object.entries(this.client.modmail.replies);
let str = '';
2021-10-22 09:35:04 +02:00
// eslint-disable-next-line no-shadow
for (const [ name, content ] of list) {
2021-06-19 20:05:32 +02:00
if (str.length + content.length > 2000) {
2021-11-29 11:25:38 +01:00
await channel.send(str).catch(err => this.client.logger.error(`CannedReply.execute errored at channel.send:\n${err.stack}`));
2021-06-19 20:05:32 +02:00
str = '';
}
str += `**${name}:** ${content}\n`;
}
2021-11-29 11:25:38 +01:00
if (str.length) return channel.send(str).catch(err => this.client.logger.error(`CannedReply.execute errored at channel.send:\n${err.stack}`));
2021-06-19 21:09:36 +02:00
return '**__None__**';
2021-06-19 15:06:20 +02:00
}
return this.client.modmail.sendCannedResponse({ message, responseName: content.trim(), anon });
}
2021-10-22 09:35:04 +02:00
async createCanned (op, args, { channel, author }) {
2021-06-19 20:05:32 +02:00
if (args.length < 1) return {
error: true,
msg: 'Missing reply name'
};
2021-10-22 09:35:04 +02:00
const [ _name, ...rest ] = args;
2021-06-19 20:05:32 +02:00
const name = _name.toLowerCase();
const canned = this.client.modmail.replies;
let confirmation = null;
if (op === 'create') {
if (!rest.length) return {
error: true,
msg: 'Missing content'
};
if (canned[name]) {
confirmation = await this.client.prompt(`A canned reply by the name ${name} already exists, would you like to overwrite it?`, { channel, author });
if (!confirmation) return 'Timed out.';
2021-10-22 09:35:04 +02:00
confirmation = [ 'y', 'yes', 'ok' ].includes(confirmation.content.toLowerCase());
2021-06-19 20:05:32 +02:00
if (!confirmation) return 'Cancelled';
}
canned[name] = rest.join(' ');
} else {
delete canned[name];
}
this.client.modmail.saveReplies();
return `Updated ${_name}`;
}
2021-06-19 15:06:20 +02:00
}
module.exports = CannedReply;