From 9bdcbc96e19a61dcbac5e63a83d4b5d1309936a2 Mon Sep 17 00:00:00 2001 From: AlePaff Date: Sat, 13 May 2023 22:26:51 -0300 Subject: [PATCH] add random module given a list of values selects a random one --- lib/db.ts | 6 ++++++ modules/random.ts | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 modules/random.ts diff --git a/lib/db.ts b/lib/db.ts index bf45d92..8ddfaa4 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -240,6 +240,12 @@ const data = { PROCESSING: "```Generating QR code. Please wait...```", IMAGE_CAPTION: "```Here's your QR image.```", }, + random: { + DESCRIPTION: "Given a list of items, pick a random one.", + EXTENDED_DESCRIPTION: + "```Choose a random value by using command``` *.random WatchTV WashDishes ...* .", + NO_ARG: "```Please enter at least one value to be picked. For example .random Value1 Value2```", + }, rbl: { DESCRIPTION: "Module to enable a blacklist person or group to use the bot.", EXTENDED_DESCRIPTION: diff --git a/modules/random.ts b/modules/random.ts new file mode 100644 index 0000000..bbf5d4a --- /dev/null +++ b/modules/random.ts @@ -0,0 +1,36 @@ +import Strings from "../lib/db"; +import inputSanitization from "../sidekick/input-sanitization"; +import { MessageType } from "../sidekick/message-type"; +import Client from "../sidekick/client"; +import { proto } from "@adiwajshing/baileys"; +import BotsApp from "../sidekick/sidekick"; +const random = Strings.random; + +export = { + name: "random", + description: random.DESCRIPTION, + extendedDescription: random.EXTENDED_DESCRIPTION, + demo: { isEnabled: true, text: ".random" }, + async handle(client: Client, chat: proto.IWebMessageInfo, BotsApp: BotsApp, args: string[]): Promise { + if(args.length === 0) { + client.sendMessage( + BotsApp.chatId, + random.NO_ARG, + MessageType.text + ); + return; + } else if(args.length >= 1) { + let outcomes = args; + let outcomesIndex = Math.floor(Math.random() * outcomes.length); + try { + client.sendMessage( + BotsApp.chatId, + outcomes[outcomesIndex], + MessageType.text + ).catch(err => inputSanitization.handleError(err, client, BotsApp)); + } catch (err) { + await inputSanitization.handleError(err, client, BotsApp); + } + } + }, +}; \ No newline at end of file