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.
1import { ChatCommand } from '@twitchfy/chatbot';2
3export default class HelloCommand extends ChatCommand {}
1const { ChatCommand } = require('@twitchfy/chatbot'); //using cjs2
3module.exports = 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.
1import { ChatCommand, SetCommand } from '@twitchfy/chatbot';2
3@SetCommand({4 name: 'hello'5})6export default class HelloCommand extends ChatCommand {};
1const { ChatCommand } = require('@twitchfy/chatbot');2
3module.exports = class HelloCommand extends ChatCommand {4 name: 'hello'5};
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:
1import { ChatCommand, SetCommand, type TwitchContext } from '@twitchfy/chatbot';2
3@SetCommand({4 name: 'hello'5})6export default class HelloCommand extends ChatCommand {7 async run(ctx: TwitchContext){8 return await ctx.reply(`Hello, ${ctx.author.toString()}`);9 }10};
1const { ChatCommand } = require('@twitchfy/chatbot')2
3module.exports = class HelloCommand extends ChatCommand {4 name: 'hello',5 async run(ctx){6 return await ctx.reply(`Hello, ${ctx.author.toString()}`);7 }8};
Now when any user sends the command as !hello
the chatbot will reply to the user’s message, mentioning the user.