forked from Galactic/galactic-bot
90 lines
2.6 KiB
JavaScript
90 lines
2.6 KiB
JavaScript
const { Structures } = require('discord.js');
|
|
|
|
const escapeRegex = require('escape-string-regexp');
|
|
const emojis = require('../../util/emojis.json');
|
|
|
|
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 = {}) {
|
|
|
|
let language = this.author._settings.locale || 'en_us';
|
|
if(this.guild && this.guild._settings.locale) language = this.guild._settings.locale;
|
|
|
|
let template = this.client.localeLoader.template(language, index); //.languages[language][index];
|
|
for (const [param, val] of Object.entries(parameters)) {
|
|
template = template.replace(new RegExp(`{${escapeRegex(param.toLowerCase())}}`, 'gi'), val);
|
|
}
|
|
|
|
return template;
|
|
|
|
}
|
|
|
|
async resolve() {
|
|
|
|
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 };
|
|
}
|
|
|
|
}
|
|
|
|
async embed(embed, opts = {}) {
|
|
|
|
let send = { content: opts.reply ? `<@${this.author.id}>` : '', embed };
|
|
this._pending = await this.channel.send(send);
|
|
return this._pending;
|
|
|
|
}
|
|
|
|
async respond(str, opts = {}) {
|
|
|
|
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}`;
|
|
}
|
|
|
|
this._pending = await this.channel.send(str);
|
|
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);
|
|
}
|
|
|
|
}
|
|
|
|
return ExtendedMessage;
|
|
|
|
});
|
|
|
|
module.exports = Message; |