@kakushin/command-context
Wrapping all sapphire commands (interaction, message) in one context
Installation
npm install @sapphire/framework@next discord.js @sapphire/utilities @kakushin/command-context
Example ContextCommand
import { ApplicationCommandRegistry, Command, RegisterBehavior } from "@sapphire/framework";
import { CommandContext } from "@kakushin/command-context";
import { ApplyOptions } from "@sapphire/decorators";
@ApplyOptions<Command.Options>({
name: "ping",
description: "A ping command.",
chatInputCommand: {
register: true
}
})
export class PingCommand extends ContextCommand {
public contextRun(ctx: CommandContext): unknown {
return ctx.send({
content: "PONG!"
});
}
public override registerApplicationCommands(registry: ApplicationCommandRegistry): void {
registry.registerChatInputCommand({
name: this.name,
description: this.description
}, {
behaviorWhenNotIdentical: RegisterBehavior.Overwrite,
registerCommandIfMissing: true
});
}
}
Example ContextPrecondition
import { CommandContext, ContextPrecondition } from "@kakushin/command-context";
import type { Precondition } from "@sapphire/framework";
import { ApplyOptions } from "@sapphire/decorators";
@ApplyOptions<Precondition.Options>({
name: "isAdministrator"
})
export class isAdministrator extends ContextPrecondition {
public contextRun(context: CommandContext) {
return context.member!.id === context.guild ? .ownerId ? this.ok() : this.error({
message: "You are not the administrator."
});
}
}
declare module "@sapphire/framework" {
interface Preconditions {
isAdministrator: never;
}
}