Skip to content

ChatMock Examples

Here are several generic, real-world examples demonstrating how ChatMock seamlessly validates standard bot behaviors across different scenarios.

Example 1: Handling Missing Arguments in a Group

Section titled “Example 1: Handling Missing Arguments in a Group”

When a user executes a command that strictly requires a @user mention, you should test that the command correctly aborts and warns the user if no relevant arguments are provided.

import type { ICommand, IChatContext, AdditionalAPI, CommandArgs } from "whatsbotcord";
export default class KickCommand implements ICommand {
name = "kick";
public async run(ctx: IChatContext, _api: AdditionalAPI, args: CommandArgs): Promise<void> {
if (!args.args.length) {
await ctx.SendReactEmojiToInitialMsg("");
await ctx.SendText("You must mention a user to kick.");
return;
}
// ... Kick logic processing here
}
}

Example 2: Parsing Predefined Confirmation Flows

Section titled “Example 2: Parsing Predefined Confirmation Flows”

If a command relies on ctx.WaitYesOrNoAnswer to prevent destructive actions, you should validate that supplying positive string literals correctly pushes the execution flow toward the success branch.

import type { ICommand, IChatContext } from "whatsbotcord";
export default class ClearDatabaseCommand implements ICommand {
name = "cleardb";
public async run(ctx: IChatContext): Promise<void> {
await ctx.SendText("Are you absolutely sure you want to delete everything? (yes/no)");
const isConfirmed = await ctx.WaitYesOrNoAnswer({
timeOutMiliseconds: 15000,
expectedTextYes: "yes",
expectedTextNo: "no"
});
if (isConfirmed) {
await ctx.SendReactEmojiToInitialMsg("");
await ctx.SendText("Database has been wiped successfully.");
} else {
await ctx.SendReactEmojiToInitialMsg("");
await ctx.SendText("Action cancelled.");
}
}
}

Example 3: Simulating Faux Multimedia Uploads

Section titled “Example 3: Simulating Faux Multimedia Uploads”

Sometimes commands await multimedia (like WaitMultimedia) to process documents, photos, or audio files. ChatMock accepts mock buffers simulating standard Buffer streams, allowing you to validate processing pipelines without actual downloads.

import type { ICommand, IChatContext } from "whatsbotcord";
export default class ProcessInvoiceCommand implements ICommand {
name = "invoice";
public async run(ctx: IChatContext): Promise<void> {
const payload = await ctx.WaitMultimedia({ timeOutMiliseconds: 20000 });
if (payload.mimetype === "application/pdf") {
// Process payload.buffer here
await ctx.SendText("Valid PDF received. Starting evaluation process...");
} else {
await ctx.SendText("Invalid format. Please upload a PDF.");
}
}
}