galactic-bot/structure/extensions/Message.js

170 lines
5.8 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 emojis = require('../../util/emojis.json');
2020-05-05 01:35:01 +02:00
const { Util } = require('../../util/');
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.args = 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;
parameters.prefix = this.guild?.prefix || this.client._options.bot.prefix;
2020-04-19 21:55:26 +02:00
let template = this.client.localeLoader.template(language, index); //.languages[language][index];
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)) {
template = template.replace(new RegExp(`{${escapeRegex(param.toLowerCase())}}`, 'gi'), val);
}
if(code) {
try {
template = eval(template);
} 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-05-05 15:33:14 +02:00
if (this.command.showUsage && !this.parameters.length) {
2020-05-05 01:35:01 +02:00
return this._showUsage();
}
try {
const resolved = this.command.execute(this, {
params: this.parameters,
args: this.args
});
if(resolved instanceof Promise) await resolved;
return { error: false };
} catch(error) {
return { error: true, message: error.stack || 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-04-19 21:55:26 +02:00
let send = { content: opts.reply ? `<@${this.author.id}>` : '', embed };
this._pending = await this.channel.send(send);
return this._pending;
}
async respond(str, opts = { attachments: [] }) {
if(typeof str === 'string') {
if(opts.emoji) {
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}`;
}
//console.log(str)
this._pending = await this.channel.send(str, { files: opts.attachments });
return this._pending;
}
async edit(str, opts) {
if(!this.editable) return null;
if(typeof str === 'string') {
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') {
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);
return await this.channel.awaitMessages(m => m.author.id === this.author.id, { max: 1, time: 30000, errors: ['time'] })
.then((collected) => {
return collected.first();
})
.catch((error) => {
return null;
});
}
async _showUsage(comp = null) {
const component = comp
? comp
: this.command;
const prefix = this.guild?.prefix
|| this.client._options.bot.prefix;
let fields = [];
if(component.examples.length > 0) {
fields.push({
name: `${this.format('GENERAL_EXAMPLES')}`,
value: component.examples.map(e=>`\`${prefix}${component.name} ${e}\``).join('\n')
});
}
if(component.aliases.length > 0) {
fields.push({
name: `${this.format('GENERAL_ALIASES')}`,
value: component.aliases.map(a=>`\`${a}\``).join(', ')
});
}
if(component.arguments.length > 0) {
fields.push({
name: `${this.format('GENERAL_ARGUMENTS')}`,
value: component.arguments.map(a=>`${a.name}: ${this.format(`A_${a.name.toUpperCase()}_${component.name.toUpperCase()}_DESCRIPTION`)}`)
});
}
let 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}` : ''}\`
${this.format(component.description)}${component.guildOnly ? ' *(guild-only)*' : ''}`,
fields
};
return await this.embed(embed);
2020-05-05 01:35:01 +02:00
}
2020-04-08 18:08:46 +02:00
}
return ExtendedMessage;
});
module.exports = Message;