galactic-bot/structure/extensions/Message.js

207 lines
7.4 KiB
JavaScript
Raw Normal View History

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');
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.
this.arguments = null;
this.parameters = null;
this._pending = null;
}
format(index, parameters = { }, code = false) {
2020-04-19 21:55:26 +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];
for(const emoji of Object.keys(Emojis)) {
parameters[`emoji_${emoji}`] = Emojis[emoji];
}
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
}
if(code) {
try {
2020-07-25 08:02:22 +02:00
template = eval(template); //eslint-disable-line no-eval
} 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;
}
async resolve() {
2020-08-04 11:35:48 +02:00
if(this.guild && this.guild.debug) {
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
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
}
this.client.emit('commandExecute', this);
try {
const resolved = this.command.execute(this, {
params: this.parameters,
args: this.arguments
});
this.command._invokes.success++;
if(resolved instanceof Promise) await resolved;
return { error: false };
} catch (error) {
this.command._invokes.fail++;
2020-08-09 01:22:51 +02:00
return { error: true, message: error };
}
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
}
async respond(str, opts = { files: [], embed: null, dm: false }) {
if(typeof str === 'string') {
if(opts.emoji) {
2020-06-19 23:03:09 +02:00
const emoji = Emojis[opts.emoji];
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}`;
}
this._pending = await this[opts.dm ? 'author' : 'channel'].send(str, { files: opts.files, embed: opts.embed });
return this._pending;
}
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);
}
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}`;
if(opts.reply) str = `<@!${this.author.id}> ${str}`;
}
return super.edit(str);
}
async prompt(str, opts) {
if(typeof str === 'string') {
2020-06-19 23:03:09 +02:00
if(opts.emoji) str = `${Emojis[opts.emoji]} ${str}`;
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'] })
.then((collected) => {
return collected.first();
})
2020-07-25 08:02:22 +02:00
.catch((error) => { //eslint-disable-line no-unused-vars, handle-callback-err
return null;
});
}
async _showUsage(comp = null) {
const component = comp
? comp
: this.command;
2020-05-24 05:50:35 +02:00
const prefix = this.guild?.prefix;
2020-07-25 08:02:22 +02:00
const fields = [];
if(component.examples.length > 0) {
fields.push({
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')
});
}
if(component.aliases.length > 0) {
fields.push({
name: `${this.format('GENERAL_ALIASES')}`,
value: component.aliases.join(', ')
});
}
if(component.arguments.length > 0) {
fields.push({
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-07-25 08:02:22 +02:00
const embed = {
author: {
name: `${component.name}${component.module ? ` (${component.module.resolveable})` : ''}`,
icon_url: this.client.user.displayAvatarURL()
},
description: stripIndents`\`${prefix}${component.name}${component.usage ? ` ${component.usage}` : ''}\`${component.guildOnly ? ' *(guild-only)*' : ''}
${this.format(component.description)}`,
fields
};
2020-07-25 08:02:22 +02:00
return this.embed(embed);
2020-05-05 01:35:01 +02:00
}
get prefix() {
return this.guild
? this.guild.prefix
: this.author.prefix;
}
2020-04-08 18:08:46 +02:00
}
return ExtendedMessage;
});
module.exports = Message;