@guildedts/framework
Version:
A framework for creating a Guilded bot.
93 lines • 3.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const guilded_ts_1 = require("guilded.ts");
const Event_1 = require("../structures/Event");
/** The handler for commands. */
class CommandHandler extends Event_1.Event {
name = 'messageCreate';
/**
* The handler for the messageCreate event.
* @param message The message that was created.
* @example commandHandler.execute(message);
*/
async execute(message) {
const prefix = this.getPrefix(message.serverId);
if (message.createdBy === this.client.user?.id || !message.content?.startsWith(prefix))
return;
const [commandName, ...args] = this.parseContent(prefix, message.content);
const command = this.getCommand(commandName);
if (!command)
return;
let mappedArgs;
let err;
try {
mappedArgs = await command.validate(message, args);
}
catch (error) {
err = error.message;
}
if (err)
return this.sendError(message, err);
try {
await command.execute(message, mappedArgs);
}
catch (error) {
console.error(error);
err = `An error occurred while executing the command: ${(0, guilded_ts_1.inlineCode)(error.message)}`;
}
if (err)
return this.sendError(message, err);
command.setCooldown(message.createdBy);
}
/**
* Get the command with the given name.
* @param name The name of the command.
* @returns The command.
* @example commandHandler.getCommand('ping');
*/
getCommand(name) {
return this.client.commands.find((command) => command.name === name || command.aliases.includes(name));
}
/**
* Get the prefix of the message.
* @param serverId The ID of the server the message belongs to.
* @returns The prefix.
* @example commandHandler.getPrefix(serverId);
*/
getPrefix(serverId) {
return serverId
? this.client.prefixes.get(serverId) || this.client.config.prefix
: this.client.config.prefix;
}
/**
* Parse the content of the message.
* @param prefix The prefix of the message content.
* @param content The content of the message.
* @returns The command name and arguments.
* @example commandHandler.parseContent('!echo hello'); // ['echo', 'hello']
*/
parseContent(prefix, content) {
return content.slice(prefix.length).split(/\s+/);
}
/**
* Send a error message to the user.
* @param message The message that triggered the command.
* @param error The error message.
* @example commandHandler.sendError(message, 'Invalid arguments');
*/
sendError(message, error) {
message.delete();
message.channel.send({
embeds: [
new guilded_ts_1.Embed()
.setColor('Red')
.setTitle((0, guilded_ts_1.userMention)(message.createdBy))
.setDescription(error)
.setFooter(message.content),
],
isPrivate: true,
});
}
}
exports.default = CommandHandler;
//# sourceMappingURL=commands.js.map