galactic-bot/structure/interfaces/Command.js

99 lines
3.1 KiB
JavaScript
Raw Normal View History

2020-04-08 18:08:46 +02:00
const Component = require('./Component.js');
2020-07-26 22:10:55 +02:00
const { stripIndents } = require('common-tags');
2020-04-08 18:08:46 +02:00
class Command extends Component {
constructor(manager, opts = {}) {
2020-04-08 18:08:46 +02:00
if(!opts) return null;
super(manager, {
2020-04-08 18:08:46 +02:00
id: opts.name,
type: 'command',
disabled: opts.disabled || false,
guarded: opts.guarded || false
});
this.manager = manager;
2020-04-08 18:08:46 +02:00
this.name = opts.name;
this.module = opts.module;
this.aliases = opts.aliases || [];
this.description = `C_${opts.name.toUpperCase()}_DESCRIPTION`;
this.usage = opts.usage || '';
this.examples = opts.examples || [];
this.rawExamples = Boolean(opts.rawExamples);
2020-04-08 18:08:46 +02:00
this.tags = opts.tags || [];
2020-04-08 18:08:46 +02:00
this.restricted = Boolean(opts.restricted);
this.showUsage = Boolean(opts.showUsage);
2020-04-08 18:08:46 +02:00
this.guildOnly = Boolean(opts.guildOnly);
this.archivable = opts.archivable === undefined ? true : Boolean(opts.archivable);
this.arguments = opts.arguments || [];
this.keepQuotes = Boolean(opts.keepQuotes);
2020-04-08 18:08:46 +02:00
this.clientPermissions = opts.clientPermissions || [];
this.memberPermissions = opts.memberPermissions || [];
this.throttling = opts.throttling || {
usages: 5,
duration: 10
};
this._throttles = new Map();
2020-08-14 21:46:08 +02:00
this._invokes = {
success: 0,
fail: 0
};
2020-04-08 18:08:46 +02:00
}
2020-07-28 22:40:15 +02:00
execute() {
throw new Error(`${this.resolveable} is missing an execute function.`);
}
2020-04-08 18:08:46 +02:00
get moduleResolveable() {
return `${this.module.id}:${this.id}`;
}
2020-04-08 16:27:34 +02:00
2020-07-26 22:10:55 +02:00
usageEmbed(message, verbose = false) {
const { guild } = message;
const prefix = guild?.prefix || this.client.prefix;
const fields = [];
if (this.examples.length) {
fields.push({
name: `${message.format('GENERAL_EXAMPLES')}`,
value: this.examples.map((e) => this.rawExamples ? `\`${prefix}${e}\`` : `\`${prefix}${this.name} ${e}\``).join('\n')
});
}
if (this.aliases.length && verbose) {
fields.push({
name: `${message.format('GENERAL_ALIASES')}`,
value: this.aliases.join(', ')
});
}
if (this.arguments.length && verbose) {
fields.push({
name: `${message.format('GENERAL_ARGUMENTS')}`,
value: this.arguments.map((a) => `\`${a.types.length === 1 && a.types.includes('FLAG') ? '--' : ''}${a.name}${a.usage ? ` ${a.usage}` : ''}\`: ${message.format(`A_${a.name.toUpperCase()}_${this.name.toUpperCase()}_DESCRIPTION`)}`).join('\n')
2020-07-26 22:10:55 +02:00
});
}
return {
author: {
name: `${this.name}${this.module ? ` (${this.module.resolveable})` : ''}`
},
description: stripIndents`\`${prefix}${this.name}${this.usage ? ` ${this.usage}` : ''}\`${this.guildOnly ? ' *(guild-only)*' : ''}
${message.format(this.description)}`,
fields
};
}
2020-04-08 16:27:34 +02:00
}
module.exports = Command;