Command Management and Middleware
The command engine provided by Disapp eliminates the need for classic if/else loops and repetitive permission checks. Disapp offers an entirely Class-based (OOP) and Middleware-supported Slash Command infrastructure.
Base Slash Command
To create a new command, simply extend the Command class originating from the core package. Its internal constructor configuration is automatically uploaded to the Discord API.
import { Command } from "@disapp/core";
import { SlashCommandBuilder } from "discord.js";
export default class AvatarCommand extends Command {
constructor() {
super({
name: "avatar",
description: "Displays your or someone else's avatar",
data: new SlashCommandBuilder()
.setName("avatar")
.setDescription("Displays your or someone else's avatar")
.addUserOption((opt) => opt.setName("target").setDescription("User")),
execute: async () => {},
});
}
async execute(interaction: any) {
const target = interaction.options.getUser("target") || interaction.user;
await interaction.reply({
content: `${target.username}'s avatar: ${target.displayAvatarURL()}`,
});
}
}
Middleware System
The true revolution in Disapp's command management lies in its Middleware pipeline constructed for permission checks and guards. From catching error messages to Rate Limiting, everything can be resolved in the outer layer.
Middleware Integration
By providing a middlewares array into the super constructor, you directly declare what constraints your function is subjected to.
import {
Command,
OnlyAdmin,
Cooldown,
RateLimit,
OwnerOnly,
} from "@disapp/core";
export default class BanCommand extends Command {
constructor() {
super({
name: "ban",
description: "Permanently bans the user.",
middlewares: [OnlyAdmin(), Cooldown(15000), RateLimit(3, 60000)],
data: new SlashCommandBuilder()
.setName("ban")
.setDescription("Bans the user from the server."),
execute: async () => {},
});
}
async execute(interaction: any) {
await interaction.reply("User has been banned.");
}
}
Available Middleware
| Middleware | Description | Example |
|---|---|---|
OnlyAdmin() | Requires Administrator permission | OnlyAdmin() |
RequirePermission(permission) | Requires specific permission | RequirePermission(PermissionFlagsBits.BanMembers) |
Cooldown(ms) | Adds cooldown in milliseconds | Cooldown(5000) |
RequireDatabase() | Requires database connection | RequireDatabase() |
RequireGuild() | Requires command in server | RequireGuild() |
RequireRole(roleId) | Requires specific role | RequireRole('123456789') |
OwnerOnly(ownerId) | Only bot owner can use | OwnerOnly('123456789') |
RateLimit(maxUses, windowMs) | Rate limiting | RateLimit(3, 60000) |
Multiple Middleware
Chain multiple middleware:
export default class ModCommand extends Command {
constructor() {
super({
name: "mod",
description: "Moderation command",
data: new SlashCommandBuilder()
.setName("mod")
.setDescription("Moderation command"),
execute: async () => {},
middlewares: [
RequireGuild(),
RequirePermission(PermissionFlagsBits.ModerateMembers),
Cooldown(3000),
RequireDatabase(),
],
});
}
}
Custom Middleware
Create your own middleware:
import { MiddlewareFunction } from "@disapp/core";
export function RequireLevel(minLevel: number): MiddlewareFunction {
return async (ctx) => {
const userLevel = await getUserLevel(ctx.interaction.user.id);
if (userLevel < minLevel) {
await ctx.interaction.reply({
content: `❌ You need level ${minLevel} to use this command!`,
flags: 64,
});
return false;
}
return true;
};
}
export default class SpecialCommand extends Command {
constructor() {
super({
name: "special",
description: "Special command",
data: new SlashCommandBuilder()
.setName("special")
.setDescription("Special command"),
execute: async () => {},
middlewares: [RequireLevel(10)],
});
}
}
Best Practices
- Use middleware for common checks - Don't repeat yourself
- Order matters - Place fast checks first
- Return false to stop - Always return false when blocking execution
- Provide feedback - Reply to the user explaining why the command was blocked
Learn More
- Middleware Documentation - Complete middleware guide
- Advanced Features - Advanced middleware patterns
- API Reference - Full API documentation