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 Whatsbotcord, { BaileysAdapter, 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 Whatsbotcord({ commandPrefix: ["!"], tagPrefix: ["@"],}, new BaileysAdapter({ credentialsFolder: "./auth" }));
/** IMPORTANT: Use CommandType.Tag prop to make it use the @ prefix */bot.Commands.Add(everyoneTag, CommandType.Tag);bot.Start();import type { AdditionalAPI, IChatContext, CommandArgs, ICommand } from "whatsbotcord/types";import Whatsbotcord, { BaileysAdapter, CommandType, SenderType } from "whatsbotcord";
class EveryoneTag implements ICommand { name: string = "everyone"; aliases: string[] = ["e"];
async run(ctx: IChatContext, _api: AdditionalAPI, args: CommandArgs): Promise<void> { if (args.senderType === SenderType.Individual) { await ctx.SendText("This command can only be used in groups!"); return; }
const groupData = await ctx.FetchGroupData(); if (!groupData) { await ctx.SendText("There was an error trying to get the group data!..."); return; }
// Format: 234234234@lid const allIds = groupData.members.map((m) => m.rawId!);
// Format: @23423423 const allMentionsIds: string[] = groupData.members.map((m) => m.asMentionFormatted!);
await ctx.SendText(allMentionsIds.join(" "), { mentionsIds: allIds }); }}
const bot = new Whatsbotcord({ commandPrefix: ["!"], tagPrefix: ["@"],}, new BaileysAdapter({ 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).