Skip to main content

Method isExecutable

Method isExecutable is a method in the Command class that checks whether the given command can be executed based on the incoming message. This method takes one argument - a user message object.

Parameters

  • message - an object of the UserMessageContext class, which contains information about the incoming user message such as the text of the message, chat ID, etc.

Default Logic

Initially, the isExecutable method returns a boolean value that is equal to true if the incoming message has the same command name as the Command instance and false otherwise. If the isExecutable method returns true, then the execute method of the Command instance will be called to execute the command.

public isExecutable(message: UserMessageContext): boolean {
return message.hasCommand === this.params.name;
}

Overriding

In the Command class, the isExecutable method can be overridden to change the logic for determining whether a command can be executed. For example, depending on the specific requirements of the command, additional message parameters can be verified rather than just comparing the command name.

The isExecutable method can be extended as follows:

import { Command, UserMessageContext, CommandHandler } from "evogram";

@CommandHandler("start")
class GreetingCommand extends Command {
public isExecutable(message: MessageContext): boolean {
return message.hasCommand === this.params.name && message.chat.type === "private";
}

public execute(message: MessageContext) {
// Command execution logic
}
}

In the example above, the isExecutable method was overridden in the GreetingCommand class to add a check for the chat type for which this command will be available. Now the command will only be executed in private chats with users.

note

This approach allows for flexible customization of command behavior, which is particularly useful when creating bots with various functional capabilities. As a result, more logical, readable, and intuitive commands can be created for users.