diff --git a/lib/db.ts b/lib/db.ts index bf45d925..e9108a67 100644 --- a/lib/db.ts +++ b/lib/db.ts @@ -279,6 +279,11 @@ const data = { NOT_A_GROUP: "```This command is only applicable in a group chat.```", ICON_CHANGED: "```Changing icon/group image...```", }, + smeta: { + DESCRIPTION: "Module to add or change metadata to a sticker such as emojis associated with the sticker, pack name, author name and shape of the sticker.", + EXTENDED_DESCRIPTION: + "```Use this module to add/change metadata to a sticker. Reply to a sticker with the command``` *.smeta * ```to add/change metadata to the sticker.``` \n\n``` - Emoji to be used for searching in the sticker whatsapp's menu.``` \n``` - Type of the sticker's shape: default, crop, full, circle, rounded.``` \n``` - Name of the sticker pack.``` \n``` - Name of the sticker pack author.``` \n\n```For example\n``` *.smeta 🤣 circle myPack anAuthor*\n *.smeta ❤️*\n\nTo avoid using the emoji or shape metadata in the command, use the keyword``` *null* ```in place of the metadata.``` \n\n```For example```\n*.smeta null null myPack anAuthor*", + }, song: { DESCRIPTION: "Download songs", EXTENDED_DESCRIPTION: diff --git a/modules/smeta.ts b/modules/smeta.ts new file mode 100644 index 00000000..0ddd3518 --- /dev/null +++ b/modules/smeta.ts @@ -0,0 +1,69 @@ +import Strings from "../lib/db"; +import inputSanitization from "../sidekick/input-sanitization"; +import { MessageType } from "../sidekick/message-type"; +import { downloadContentFromMessage, proto } from "@adiwajshing/baileys"; +import Client from "../sidekick/client"; +import BotsApp from "../sidekick/sidekick"; +import fs from "fs"; +import { readFileSync } from 'fs' + +import { Transform } from "stream"; +import { Sticker, extractMetadata, StickerTypes, Categories } from 'wa-sticker-formatter' + +const smeta = Strings.smeta; + +function getStickerType(arg: string): StickerTypes { + if (Object.values(StickerTypes).includes(arg as StickerTypes)) { + return arg as StickerTypes; + } + return StickerTypes.FULL; +} + +export = { + name: "smeta", + description: smeta.DESCRIPTION, + extendedDescription: smeta.EXTENDED_DESCRIPTION, + demo: { isEnabled: false, text: ".smeta" }, + async handle(client: Client, chat: proto.IWebMessageInfo, BotsApp: BotsApp, args: string[]): Promise { + + if (BotsApp.isReplySticker) { + //it allows more than 1 emoji but its a secret for now + //convert from string separate by commas to Categories + const arg_emojis: Categories[] = []; + for (let i = 0; i < args[0].split(",").length; i++) { + arg_emojis.push(args[0].split(",")[i] as Categories); + } + + var replyChatObject: any = { + message: chat.message.extendedTextMessage.contextInfo.quotedMessage.stickerMessage, + type: 'sticker' + }; + var stickerId = chat.message.extendedTextMessage.contextInfo.stanzaId; + const filePath = "./tmp/convert_to_image-" + stickerId; + const fileWebp = './tmp/sticker-' + stickerId + '.webp'; + const stream: Transform = await downloadContentFromMessage(replyChatObject.message, replyChatObject.type) + await inputSanitization.saveBuffer(filePath, stream); + + const stickerRead = readFileSync(filePath) + let metadata = await extractMetadata(stickerRead) // { emojis: [], 'sticker-pack-id': '', 'sticker-pack-name': '', 'sticker-author-name': '' } + + const sticker = new Sticker(filePath, { + categories: args[0] != "null" ? arg_emojis : [], + type: args[1] != "null" ? getStickerType(args[1]) : StickerTypes.FULL, + pack: args[2] ?? metadata['sticker-pack-name'], + author: args[3] ?? metadata['sticker-author-name'] + // id: '12345', // The sticker id + // quality: 100, // The quality of the output file + // background: '#000000' // The sticker background color (only for full stickers) + }) + await sticker.toFile(fileWebp) + await client.sendMessage( + BotsApp.chatId, + fs.readFileSync(fileWebp), + MessageType.sticker, + ).catch(err => inputSanitization.handleError(err, client, BotsApp)); + await inputSanitization.deleteFiles(filePath, fileWebp); + } + } + +}; \ No newline at end of file diff --git a/package.json b/package.json index 52763a3c..585cb79e 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "typescript": "^4.6.4", "unofficial-carbon-now": "^1.0.3", "urban-dictionary": "^3.0.2", + "wa-sticker-formatter": "^4.4.4", "yt-search": "^2.10.1", "ytdl-core": "^4.11.2" },