Skip to content

Creating your first Command

Having created and started your first chabot it’s time to create your first command.

Commands are created within files which are located in the commands directory defined in the paths option when creating the chatbot.

For creating a command you will have to simply create a class and export it. The class has to extend a pre-defined class called ChatCommand.

commands/hello.ts
1
import { ChatCommand } from '@twitchfy/chatbot';
2
3
export default class HelloCommand extends ChatCommand {}

Command Name

Now that be have created a command we need to set it’s name that will be used by any chatter to execute the command.

To set the name of the command we simply set the name property inside our command class to the command’s name.

commands/hello.ts
1
import { ChatCommand, SetCommand } from '@twitchfy/chatbot';
2
3
@SetCommand({
4
name: 'hello'
5
})
6
export default class HelloCommand extends ChatCommand {};

Running Commands

Once you have set the name of the command it’s time to run the command logic. When an user runs a command, the package will try to execute the run method from the command with a TwitchContext.

Here is a detailed example of a simple hello world command:

commands/hello.ts
1
import { ChatCommand, SetCommand, type TwitchContext } from '@twitchfy/chatbot';
2
3
@SetCommand({
4
name: 'hello'
5
})
6
export default class HelloCommand extends ChatCommand {
7
async run(ctx: TwitchContext){
8
return await ctx.reply(`Hello, ${ctx.author.toString()}`);
9
}
10
};