Tags & Groups
Commands can be invoked as Tags instead of normal prefixes. By default, tags are triggered via @. You can change this behavior setting the tagPrefix option when creating your Bot.
Recreating @everyone
Section titled “Recreating @everyone”Here we’ll create a command that works exactly like Discord’s @everyone by fetching all group members, wrapping their IDs in mention syntax, and sending a broadcast.
import Bot, { CommandType, CreateCommand } from "whatsbotcord";
const everyoneTag = CreateCommand( "everyone", async (chat, api, args) => { // Fetch properties from the current group context const res = await chat.FetchGroupData();
if (res) { /** * `res.members` contains everyone's formatted IDs * abstracted for you by the library! */ const mentions = res.members.map((m) => m.asMentionFormatted); const ids = res.members.map((m) => m.rawId);
// Reply with all mentions, telling WhatsApp who to actually tag await chat.SendText(mentions.join(" "), { mentionsIds: ids }); } }, // Now it can be used like @e { aliases: "e" });
const bot = new Bot({ commandPrefix: ["!"], tagPrefix: ["@"], credentialsFolder: "./auth",});
/** IMPORTANT: Use CommandType.Tag prop to make it use the @ prefix */bot.Commands.Add(everyoneTag, CommandType.Tag);bot.Start();import type { AdditionalAPI, ChatContext, CommandArgs, ICommand } from "whatsbotcord";import Bot, { CommandType } from "whatsbotcord";
class EveryoneTag implements ICommand { name: string = "e"; aliases: string[] = ["test"];
async run(chat: ChatContext, _api: AdditionalAPI, _args: CommandArgs): Promise<void> { const res = await chat.FetchGroupData(); if (res) { // Format: @23423423 const mentions = res.members.map((m) => m.asMentionFormatted!);
// Format: 234234234@lid const ids = res.members.map((m) => m.rawId!);
await chat.SendText(mentions.join(" "), { mentionsIds: ids }); } }}
const bot = new Bot({ commandPrefix: ["!"], tagPrefix: ["@"], credentialsFolder: "./auth",});
bot.Commands.Add(new EveryoneTag(), CommandType.Tag);bot.Start();Now whenever someone sends "@everyone" or "@e", the bot will broadcast out the mention block. (Note: the bot must have permissions/presence in that group to read group data and send messages).