Unlike almost every other command framework, discord-ahagon does not rely on the verbose
class syntax or this
to implement commands. Allowing you to compose and reuse
similar traits of commands and use concise arrow functions as well as giving you the ability
to hook into different parts of the command execution process.
Adding a command handler
// src/index.js
const { createHandler } = require("ahagon");
const handler = createHandler({
commandsDirectory: `${__dirname}/commands`,
prefix: "!"
});
Adding commands
// src/commands/util.js
const ping = {
name: ["ping", "p"],
run: ctx => ctx.message.reply("Pong!")
}
const avatar = {
name: "avatar",
run: ({ message }) => message.channel.send(message.author.avatarURL)
}
module.exports = { ping, avatar };
An extensive ban command
const ban = {
name: ["ban", "🔨", "bean"],
description: "bans a naughty boye with a reason",
category: "moderation",
userPermissions: ["BAN_MEMBERS"],
clientPermissions: ["BAN_MEMBERS"],
args: [{
name: "target",
description: "Ban target",
type: "member",
checks: [{
check: (ctx, member) => member.bannable,
onFail: (ctx, member) => ctx.message.channel.send(`I cannot ban ${member.user.username}!`)
}, {
check: (ctx) => ctx.database.lookup(ctx.message.author.id).then(member => member.isModerator),
onFail: (ctx) => ctx.message.channel.send(`You are not authorized to do this`)
}],
}, {
name: "reason",
description: "Optional ban reason",
type: "text",
optional: true
}],
effect: (ctx, args) => {
const start = new Date();
console.log(`${ctx.message.member} used ban`);
return (bannedMember) => {
const time = new Date() - start;
console.log(`Banned ${bannedMember.user.username} in ${time}ms.`);
analyticsLibrary.send(time, { command: ctx.command.name });
}
},
run: (ctx, args) => args.target.ban(args.reason)
}
module.exports = { ban };
You can export the commands however you like, in an array, an object or as default