commands can now get media as input
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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 <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 <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 <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');
|
||||
|
||||
Reference in New Issue
Block a user