Skip to content

The Bot Instance

The Bot (or Whatsbotcord) instance is the absolute core of your application. It manages the underlying WhatsApp socket connection, orchestrates the middleware pipeline, handles incoming events, and serves as the registry for all your commands.


To get your bot up and running, you only need to instantiate it and call .Start().

import Whatsbotcord, { BaileysAdapter } from "whatsbotcord";
const bot = new Whatsbotcord({
commandPrefix: "!",
});
await bot.Start();

The Bot instance exposes several powerful modules that you will use to shape your application.

  • bot.Commands: The built-in command registry. Use bot.Commands.Add(myCommand) to register new commands.
  • bot.Use(...): The middleware pipeline manager. You can seamlessly intercept messages before they reach commands or apply official plugins.
  • bot.Events: The global event emitter. Subscribe to socket events or command lifecycle hooks.
  • bot.Settings: Access or mutate the bot’s runtime settings dynamically.

Here is an example demonstrating how these core components fit together:

import Whatsbotcord, { BaileysAdapter, CommandType, OfficialPlugin_OneCommandPerUserAtATime } from "whatsbotcord";
import { PingCommand, SendPrivately } from "./commands";
const bot = new Whatsbotcord({
commandPrefix: ["!", "/"],
});
// 1. Add Commands
bot.Commands.Add(new PingCommand(), CommandType.Normal);
bot.Commands.Add(new SendPrivately(), CommandType.Normal);
// 2. Use Middleware & Plugins
bot.Use(
OfficialPlugin_OneCommandPerUserAtATime({
msgToSend: (info, lastCommand, actualCommand) => {
return `You ${info.pushName} have already started command !${lastCommand.name}, you can't use !${actualCommand.name} yet.`;
},
timeoutSecondsToForgetThem: 60 * 5,
})
);
// 3. Mutate Runtime Settings dynamically
bot.Settings.defaultEmojiToSendReactionOnFailureCommand = "🦊";
// 4. Start the bot
await bot.Start();

The Bot constructor accepts an extensive set of configuration properties to fine-tune prefixes, connection throttling, error handling, interactive prompt defaults, and more.

Here is an example of a bot initialized with custom configuration values:

import Whatsbotcord, { BaileysAdapter } from "whatsbotcord";
const bot = new Whatsbotcord({
commandPrefix: ["$", "!", "/", "."],
tagPrefix: ["@"],
delayMilisecondsBetweenMsgs: 1,
cancelKeywords: ["cancelcustom"],
defaultEmojiToSendReactionOnFailureCommand: "🦊",
}, new BaileysAdapter({
credentialsFolder: process.env.NODE_ENV === "development" ? "auth_canary" : "./auth",
loggerMode: "recommended",
}));

The adapter is optional, if you do not provide any, it will use BaileysAdapter by default. You would only use it if you need to custom, the credentialsFodler, loggermode etc…

The main options you should always set or at least be aware of.

PropertyTypeDefaultDescription
commandPrefixstring | string[]'!'Character(s) used to prefix commands. Configure multiple prefixes with an array (e.g. ['!', '/']).
tagPrefixstring | string[]'@'Character(s) used to tag the bot in messages (especially useful in group contexts).
ignoreSelfMessagebooleantrueIf true, the bot ignores messages sent by its own account to prevent infinite loops.

Tweak these options if you experience high concurrency messaging or encounter problems with WhatsApp rate-limiting your bot.

PropertyTypeDefaultDescription
maxReconnectionRetriesnumber5Maximum number of reconnection attempts if the socket drops unexpectedly.
senderQueueMaxLimitnumber20Maximum number of messages queued globally. Buffers pending output to avoid dropping messages.
delayMilisecondsBetweenMsgsnumber100Delay between sending queued messages to prevent spamming/flooding the WhatsApp API.

Configure how the bot reacts when a command crashes or throws an unhandled exception.

PropertyTypeDefaultDescription
enableCommandSafeNetbooleantrueCaught internal errors won’t crash the entire bot.
defaultEmojiToSendReactionOnFailureCommandstring | nullundefinedEmoji to react with on the triggering message if a command fails unexpectedly (e.g. "⚠️" or "❌").
sendErrorToChatOnFailureCommand_debugbooleanfalseSends a JSON representation of the caught error to the chat. Excellent for real-time debugging!

These defaults define the behavior of ChatContext methods like WaitYesOrNoAnswer(). Setting them here saves you from configuring them manually on every command.

PropertyTypeDefaultDescription
cancelKeywordsstring[]VariesWords that correctly cancel a running command or interactive prompt.
positiveAnswerOptionsstring[]VariesKeywords interpreted as an affirmative (“yes”) response in WaitYesOrNoAnswer (case-insensitive).
negativeAnswerOptionsstring[]VariesKeywords interpreted as a negative (“no”) response in WaitYesOrNoAnswer (case-insensitive).
timeoutSecondsnumber30Default time limit for awaiting user input during interactive commands.

These options are primarily intended for maintainers or advanced users writing automated integration tests. Incorrect usage can break the bot’s standard behavior.

PropertyTypeDescription
ownWhatsSocketImplementation_InternalIWhatsSocketReplaces the built-in WhatsApp socket implementation.
ownChatContextCreationHook_Internal() => IChatContext | nullReplaces the default ChatContext that is sent to all commands.