2020-04-08 18:08:46 +02:00
|
|
|
const { Structures } = require('discord.js');
|
|
|
|
|
2020-04-19 21:55:26 +02:00
|
|
|
const escapeRegex = require('escape-string-regexp');
|
2020-04-16 14:37:04 +02:00
|
|
|
|
2020-06-19 20:28:49 +02:00
|
|
|
const { Util, Emojis } = require('../../util/');
|
2020-07-25 08:02:22 +02:00
|
|
|
const { stripIndents } = require('common-tags');
|
2020-05-05 01:35:01 +02:00
|
|
|
|
2020-04-08 18:08:46 +02:00
|
|
|
const Message = Structures.extend('Message', (Message) => {
|
|
|
|
|
|
|
|
class ExtendedMessage extends Message {
|
|
|
|
|
|
|
|
constructor(...args) {
|
|
|
|
|
|
|
|
super(...args);
|
|
|
|
|
|
|
|
this.command = null; //Will set to command if the message induces a command.
|
2020-05-25 13:13:34 +02:00
|
|
|
this.arguments = null;
|
2020-04-16 14:37:04 +02:00
|
|
|
this.parameters = null;
|
|
|
|
|
|
|
|
this._pending = null;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-05-05 01:06:27 +02:00
|
|
|
format(index, parameters = { }, code = false) {
|
2020-04-19 21:55:26 +02:00
|
|
|
|
2020-05-08 08:50:54 +02:00
|
|
|
let language = 'en_us';
|
2020-04-19 21:55:26 +02:00
|
|
|
if(this.guild && this.guild._settings.locale) language = this.guild._settings.locale;
|
|
|
|
|
2020-05-24 05:50:35 +02:00
|
|
|
parameters.prefix = this.guild?.prefix;
|
2020-04-19 21:55:26 +02:00
|
|
|
let template = this.client.localeLoader.template(language, index); //.languages[language][index];
|
2020-05-05 01:06:27 +02:00
|
|
|
|
2020-06-19 20:28:49 +02:00
|
|
|
for(const emoji of Object.keys(Emojis)) {
|
|
|
|
parameters[`emoji_${emoji}`] = Emojis[emoji];
|
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
if(!template) {
|
|
|
|
return `**Missing language index \`${language} [${index}]\` in languages. Contact a bot developer about this.**`;
|
|
|
|
}
|
|
|
|
|
2020-04-19 21:55:26 +02:00
|
|
|
for (const [param, val] of Object.entries(parameters)) {
|
2020-07-25 08:02:22 +02:00
|
|
|
template = template.replace(new RegExp(`{${escapeRegex(param.toLowerCase())}}`, 'gi'), val); //eslint-disable-line require-unicode-regexp
|
2020-04-19 21:55:26 +02:00
|
|
|
}
|
|
|
|
|
2020-05-05 01:06:27 +02:00
|
|
|
if(code) {
|
|
|
|
try {
|
2020-07-25 08:02:22 +02:00
|
|
|
template = eval(template); //eslint-disable-line no-eval
|
2020-05-05 01:06:27 +02:00
|
|
|
} catch(error) {
|
|
|
|
this.command.client.logger.error(`Error in locale ${language}:${index} while executing code.\n${error.stack || error}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 21:55:26 +02:00
|
|
|
return template;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:37:04 +02:00
|
|
|
async resolve() {
|
|
|
|
|
2020-08-04 11:35:48 +02:00
|
|
|
if(this.guild && this.guild.debug) {
|
2020-07-04 12:23:10 +02:00
|
|
|
this.guild._debugLog(`Command [${this.command.moduleResolveable}] execution by ${this.author.tag}. Parameters: ${this.parameters.join(', ')} || Arguments: ${Object.values(this.arguments).map((arg) => `${arg.name}:${arg.value}`)}`);
|
|
|
|
}
|
2020-06-16 00:15:13 +02:00
|
|
|
|
2020-05-25 13:13:34 +02:00
|
|
|
if (this.command.showUsage && !this.parameters.length && !Object.values(this.arguments).length) {
|
2020-07-26 22:11:12 +02:00
|
|
|
return this.embed(this.command.usageEmbed(this));
|
2020-05-05 01:35:01 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 03:25:27 +02:00
|
|
|
this.client.emit('commandExecute', this);
|
|
|
|
|
2020-04-16 14:37:04 +02:00
|
|
|
try {
|
|
|
|
const resolved = this.command.execute(this, {
|
|
|
|
params: this.parameters,
|
2020-05-25 13:13:34 +02:00
|
|
|
args: this.arguments
|
2020-04-16 14:37:04 +02:00
|
|
|
});
|
2020-08-14 21:45:32 +02:00
|
|
|
this.command._invokes.success++;
|
2020-04-16 14:37:04 +02:00
|
|
|
if(resolved instanceof Promise) await resolved;
|
|
|
|
return { error: false };
|
2020-08-14 21:45:32 +02:00
|
|
|
} catch (error) {
|
|
|
|
this.command._invokes.fail++;
|
2020-08-09 01:22:51 +02:00
|
|
|
return { error: true, message: error };
|
2020-04-16 14:37:04 +02:00
|
|
|
}
|
2020-04-08 18:08:46 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-19 21:55:26 +02:00
|
|
|
async embed(embed, opts = {}) {
|
|
|
|
|
2020-05-01 16:10:55 +02:00
|
|
|
if (!embed.color) embed.color = 619452;
|
2020-07-25 08:02:22 +02:00
|
|
|
const send = { content: opts.reply ? `<@${this.author.id}>` : '', embed };
|
2020-04-19 21:55:26 +02:00
|
|
|
this._pending = await this.channel.send(send);
|
|
|
|
return this._pending;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:22 +02:00
|
|
|
async formattedRespond(index, opts = { }) {
|
2020-07-11 22:32:52 +02:00
|
|
|
|
|
|
|
if (!opts.params) opts.params = {};
|
2020-07-25 08:02:22 +02:00
|
|
|
this.respond(this.format(index, opts.params), opts);
|
2020-07-11 22:32:52 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-08-19 10:26:29 +02:00
|
|
|
async respond(str, opts = { files: [], embed: null, dm: false, disableMentions: 'none' }) {
|
2020-04-16 14:37:04 +02:00
|
|
|
|
|
|
|
if(typeof str === 'string') {
|
|
|
|
if(opts.emoji) {
|
2020-06-19 23:03:09 +02:00
|
|
|
const emoji = Emojis[opts.emoji];
|
2020-04-16 14:37:04 +02:00
|
|
|
if(!emoji) this.command.client.logger.warn(`Invalid emoji provided to command ${this.command.resolveable}: "${opts.emoji}".`);
|
|
|
|
str = `${emoji} ${str}`;
|
|
|
|
}
|
|
|
|
if(opts.reply) str = `<@!${this.author.id}> ${str}`;
|
|
|
|
}
|
|
|
|
|
2020-08-19 10:26:29 +02:00
|
|
|
this._pending = await this[opts.dm ? 'author' : 'channel'].send(str, { files: opts.files, embed: opts.embed, disableMentions: opts.disableMentions });
|
2020-04-16 14:37:04 +02:00
|
|
|
return this._pending;
|
|
|
|
|
|
|
|
}
|
2020-04-09 23:08:28 +02:00
|
|
|
|
2020-06-19 23:03:09 +02:00
|
|
|
async limitedRespond(str, opts = { attachments: [] }) {
|
|
|
|
|
|
|
|
if(typeof str === 'string') {
|
|
|
|
if(opts.emoji) {
|
|
|
|
const emoji = Emojis[opts.emoji];
|
|
|
|
if(!emoji) this.client.logger.warn(`Invalid emoji provided to command ${this.command.resolveable}: "${opts.emoji}".`);
|
|
|
|
str = `${emoji} ${str}`;
|
|
|
|
}
|
|
|
|
if(opts.reply) str = `<@!${this.author.id}> ${str}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.client.rateLimiter.limitSend(this.channel, str, opts.limit, opts.utility);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-16 14:37:04 +02:00
|
|
|
async edit(str, opts) {
|
|
|
|
if(!this.editable) return null;
|
|
|
|
if(typeof str === 'string') {
|
2020-06-19 23:03:09 +02:00
|
|
|
if(opts.emoji) str = `${Emojis[opts.emoji]} ${str}`;
|
2020-04-16 14:37:04 +02:00
|
|
|
if(opts.reply) str = `<@!${this.author.id}> ${str}`;
|
|
|
|
}
|
|
|
|
return super.edit(str);
|
2020-04-09 23:08:28 +02:00
|
|
|
}
|
|
|
|
|
2020-05-06 01:40:46 +02:00
|
|
|
async prompt(str, opts) {
|
|
|
|
if(typeof str === 'string') {
|
2020-06-19 23:03:09 +02:00
|
|
|
if(opts.emoji) str = `${Emojis[opts.emoji]} ${str}`;
|
2020-05-06 01:40:46 +02:00
|
|
|
if(opts.reply) str = `<@!${this.author.id}> ${str}`;
|
|
|
|
}
|
2020-05-06 18:28:40 +02:00
|
|
|
await this.channel.send(str);
|
2020-07-25 08:02:22 +02:00
|
|
|
return this.channel.awaitMessages((m) => m.author.id === this.author.id, { max: 1, time: 30000, errors: ['time'] })
|
2020-05-06 01:40:46 +02:00
|
|
|
.then((collected) => {
|
|
|
|
return collected.first();
|
|
|
|
})
|
2020-07-25 08:02:22 +02:00
|
|
|
.catch((error) => { //eslint-disable-line no-unused-vars, handle-callback-err
|
2020-05-06 01:40:46 +02:00
|
|
|
return null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-07 01:26:16 +02:00
|
|
|
async _showUsage(comp = null) {
|
|
|
|
|
|
|
|
const component = comp
|
|
|
|
? comp
|
|
|
|
: this.command;
|
|
|
|
|
2020-05-24 05:50:35 +02:00
|
|
|
const prefix = this.guild?.prefix;
|
2020-05-07 01:26:16 +02:00
|
|
|
|
2020-07-25 08:02:22 +02:00
|
|
|
const fields = [];
|
2020-05-07 01:26:16 +02:00
|
|
|
if(component.examples.length > 0) {
|
|
|
|
fields.push({
|
2020-08-16 09:27:49 +02:00
|
|
|
name: `》 ${this.format('GENERAL_EXAMPLES')}`,
|
2020-07-25 08:02:22 +02:00
|
|
|
value: component.examples.map((e) => component.rawExamples ? `\`${prefix}${e}\`` : `\`${prefix}${component.name} ${e}\``).join('\n')
|
2020-05-07 01:26:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if(component.aliases.length > 0) {
|
|
|
|
fields.push({
|
2020-08-16 09:27:49 +02:00
|
|
|
name: `》 ${this.format('GENERAL_ALIASES')}`,
|
2020-07-04 12:23:10 +02:00
|
|
|
value: component.aliases.join(', ')
|
2020-05-07 01:26:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if(component.arguments.length > 0) {
|
|
|
|
fields.push({
|
2020-08-16 09:27:49 +02:00
|
|
|
name: `》 ${this.format('GENERAL_ARGUMENTS')}`,
|
2020-07-25 08:02:22 +02:00
|
|
|
value: component.arguments.map((a) => `\`${a.types.length === 1 && a.types.includes('FLAG') ? '--' : ''}${a.name}${a.usage ? ` ${a.usage}` : ''}\`: ${this.format(`A_${a.name.toUpperCase()}_${component.name.toUpperCase()}_DESCRIPTION`)}`)
|
2020-05-07 01:26:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-07-25 08:02:22 +02:00
|
|
|
const embed = {
|
2020-05-07 01:26:16 +02:00
|
|
|
author: {
|
|
|
|
name: `${component.name}${component.module ? ` (${component.module.resolveable})` : ''}`,
|
2020-05-18 01:13:24 +02:00
|
|
|
icon_url: this.client.user.displayAvatarURL()
|
2020-05-07 01:26:16 +02:00
|
|
|
},
|
2020-07-04 12:23:10 +02:00
|
|
|
description: stripIndents`\`${prefix}${component.name}${component.usage ? ` ${component.usage}` : ''}\`${component.guildOnly ? ' *(guild-only)*' : ''}
|
|
|
|
${this.format(component.description)}`,
|
2020-05-07 01:26:16 +02:00
|
|
|
fields
|
|
|
|
};
|
|
|
|
|
2020-07-25 08:02:22 +02:00
|
|
|
return this.embed(embed);
|
2020-05-05 01:35:01 +02:00
|
|
|
}
|
|
|
|
|
2020-05-25 13:13:34 +02:00
|
|
|
get prefix() {
|
|
|
|
return this.guild
|
|
|
|
? this.guild.prefix
|
|
|
|
: this.author.prefix;
|
|
|
|
}
|
|
|
|
|
2020-09-17 19:41:44 +02:00
|
|
|
get _clean() {
|
|
|
|
if (!this.command) return this.cleanContent;
|
|
|
|
let str = this.content.replace(new RegExp(`^(${this.prefix}|<@!?${this.client.id}>)`, 'iu'), '').replace(this._caller, '');
|
|
|
|
if (this._settingCaller) str = str.replace(this._settingCaller, '');
|
|
|
|
return str.trim();
|
|
|
|
}
|
|
|
|
|
2020-04-08 18:08:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ExtendedMessage;
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = Message;
|