From 75073e5e03043ae3417da27f955387081915b879 Mon Sep 17 00:00:00 2001 From: Mohammed Ashab Uddin Date: Thu, 6 Aug 2020 13:03:28 +0400 Subject: [PATCH] commands can now get media as input --- whatsapp-web.js/src/models/commands.js | 59 ++++++++++++++++++++++---- whatsapp-web.js/test/test_command.js | 22 +++++----- 2 files changed, 61 insertions(+), 20 deletions(-) diff --git a/whatsapp-web.js/src/models/commands.js b/whatsapp-web.js/src/models/commands.js index 5b1a07f..59711e5 100644 --- a/whatsapp-web.js/src/models/commands.js +++ b/whatsapp-web.js/src/models/commands.js @@ -1,12 +1,15 @@ +const fs = require('fs'); + class Command{ /** * - * @param {{string, function()}} param0 + * @param {{string, function(),boolean}} param0 */ - constructor({command,callback}){ + constructor({command,callback, requireMedia = false}){ this.command = command; this.callback = callback; + this.requireMedia = requireMedia; this.requireInput = false; this.inputPos = []; this._requireInput(); @@ -30,7 +33,7 @@ class Command{ let splitCommand = this.command.split(' '); - let splitMsg = this._getInputMessage(message); + let splitMsg = this._getSplitMessage(message); //check the number of words both has @@ -70,6 +73,12 @@ class Command{ * then the command requires input */ _requireInput(){ + if(this.requireMedia) + { + this.requireInput = true; + return; + } + let msg = this.command; let matched = msg.match(/[<>]/g); @@ -79,14 +88,28 @@ class Command{ /** * returns an array of string representing the inputs of the command - * @param {string} message + * @param {import("whatsapp-web.js").Message} message */ - getInput(message){ + async getInput(message){ + + if(this.requireMedia){ + + return await this._getInputMedia(message); + }else{ + + return this._getInputMessage(message.body); + } + } + + /** + * + * @param {string} msgString + */ + _getInputMessage(msgString){ - let splitMsg = this._getInputMessage(message); - let splitCommand = this.command.split(' '); let input = {}; - let debug = ''; + let splitMsg = this._getSplitMessage(msgString); + let splitCommand = this.command.split(' '); for(let i = 0; i < splitMsg.length; i++){ if(splitCommand[i].startsWith('<')){ let key = splitCommand[i].slice(1, splitCommand[i].length-1); @@ -94,9 +117,14 @@ class Command{ } } return input; + } - _getInputMessage(msg){ + /** + * + * @param {string} msg + */ + _getSplitMessage(msg){ let split = msg.split(/['"]/g); if(split.length > 1) { @@ -109,6 +137,19 @@ class Command{ return msg.split(' '); } + /** + * + * @param {import("whatsapp-web.js").Message} message + */ + async _getInputMedia(message){ + let input = {}; + if(message.hasMedia){ + let media = await message.downloadMedia(); + input['media'] = media.data; + } + return input; + } + } module.exports.Command = Command; diff --git a/whatsapp-web.js/test/test_command.js b/whatsapp-web.js/test/test_command.js index df1577e..b582631 100644 --- a/whatsapp-web.js/test/test_command.js +++ b/whatsapp-web.js/test/test_command.js @@ -135,7 +135,7 @@ describe('Command', () => { expect(a.equals(message)).to.false; }); - it('Test : getInput() with 1 input', () => { + it('Test : _getInputMessage() with 1 input', () => { let message = 'wg create p12' @@ -146,10 +146,10 @@ describe('Command', () => { } }); - expect(a.getInput(message)['id']).to.equal('p12'); + expect(a._getInputMessage(message)['id']).to.equal('p12'); }); - it('Test : getInput() with 2 input', () => { + it('Test : _getInputMessage() with 2 input', () => { let message = 'wg create p12 pikachu' @@ -160,31 +160,31 @@ describe('Command', () => { } }); - expect(a.getInput(message)['id']).to.equal('p12'); - expect(a.getInput(message)['name']).to.equal('pikachu'); + expect(a._getInputMessage(message)['id']).to.equal('p12'); + expect(a._getInputMessage(message)['name']).to.equal('pikachu'); }); - it('Test : _getInputMessage() with a sentence', () => { + it('Test : _getSplitMessage() with a sentence', () => { let a = new Command({command : 'wg website desc '}); - let b = a._getInputMessage(`wg website desc "Hello everynyan, how are you"`); + let b = a._getSplitMessage(`wg website desc "Hello everynyan, how are you"`); expect(b[0]).to.equal('wg'); expect(b[1]).to.equal('website'); expect(b[2]).to.equal('desc'); expect(b[3]).to.equal('Hello everynyan, how are you'); }) - it('Test : _getInputMessage() with multiple sentence input', () => { + it('Test : _getSplitMessage() with multiple sentence input', () => { let a = new Command({command : 'wg website desc '}); - let b = a._getInputMessage(`wg website desc "Hello everynyan, how are you" "I am fine thank you"`); + let b = a._getSplitMessage(`wg website desc "Hello everynyan, how are you" "I am fine thank you"`); expect(b[0]).to.equal('wg'); expect(b[1]).to.equal('website'); expect(b[2]).to.equal('desc'); expect(b[3]).to.equal('Hello everynyan, how are you'); expect(b[4]).to.equal('I am fine thank you'); }) - it('Test : _getInputMessage() with single word', () => { + it('Test : _getSplitMessage() with single word', () => { let a = new Command({command : 'wg website desc '}); - let b = a._getInputMessage(`wg website desc Hello`); + let b = a._getSplitMessage(`wg website desc Hello`); expect(b[0]).to.equal('wg'); expect(b[1]).to.equal('website'); expect(b[2]).to.equal('desc');