fixed gettng input from the message
This commit is contained in:
@@ -10,7 +10,6 @@ class Command{
|
|||||||
this.requireInput = false;
|
this.requireInput = false;
|
||||||
this.inputPos = [];
|
this.inputPos = [];
|
||||||
this._requireInput();
|
this._requireInput();
|
||||||
this._getInputPosition();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -76,19 +75,6 @@ class Command{
|
|||||||
this.requireInput = matched != null && matched.length > 1;
|
this.requireInput = matched != null && matched.length > 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
_getInputPosition(){
|
|
||||||
let splitCommand = this.command.split(' ');
|
|
||||||
let inputPos = [];
|
|
||||||
for (let i = 0; i < splitCommand.length; i++)
|
|
||||||
{
|
|
||||||
if(splitCommand[i].startsWith('<')){
|
|
||||||
inputPos.push(i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
this.inputPos = inputPos;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns an array of string representing the inputs of the command
|
* returns an array of string representing the inputs of the command
|
||||||
* @param {string} message
|
* @param {string} message
|
||||||
@@ -96,9 +82,14 @@ class Command{
|
|||||||
getInput(message){
|
getInput(message){
|
||||||
|
|
||||||
let splitMsg = message.split(' ');
|
let splitMsg = message.split(' ');
|
||||||
let input = [];
|
let splitCommand = this.command.split(' ');
|
||||||
for(let pos of this.inputPos){
|
let input = {};
|
||||||
input.push(splitMsg[pos]);
|
|
||||||
|
for(let i = 0; i < splitMsg.length; i++){
|
||||||
|
if(splitCommand[i].startsWith('<')){
|
||||||
|
let key = splitCommand[i].slice(1, splitCommand[i].length-1);
|
||||||
|
input[key] = splitMsg[i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return input;
|
return input;
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ const commands = [
|
|||||||
command: 'wg',
|
command: 'wg',
|
||||||
|
|
||||||
//function to be executed
|
//function to be executed
|
||||||
callback : () => {
|
callback : (input) => {
|
||||||
|
|
||||||
logic.onWG();
|
logic.onWG();
|
||||||
//mesage to be sent to the user
|
//mesage to be sent to the user
|
||||||
@@ -36,7 +36,7 @@ module.exports= onMessage = (message, client) => {
|
|||||||
if(message.body.startsWith("wg"))
|
if(message.body.startsWith("wg"))
|
||||||
{
|
{
|
||||||
for (let c of commands){
|
for (let c of commands){
|
||||||
if(c.command == message.body)
|
if(c.equals(message.body))
|
||||||
{
|
{
|
||||||
command = c;
|
command = c;
|
||||||
break;
|
break;
|
||||||
@@ -44,7 +44,12 @@ module.exports= onMessage = (message, client) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(command != null){
|
if(command != null){
|
||||||
let messageToBeSent = command.callback();
|
let input = {};
|
||||||
|
if(command.requireInput){
|
||||||
|
input = command.getInput();
|
||||||
|
}
|
||||||
|
|
||||||
|
let messageToBeSent = command.callback(input);
|
||||||
for (let msg of messageToBeSent)
|
for (let msg of messageToBeSent)
|
||||||
{
|
{
|
||||||
client.sendMessage(message.from, msg, new Object());
|
client.sendMessage(message.from, msg, new Object());
|
||||||
|
|||||||
@@ -120,19 +120,6 @@ describe('Command', () => {
|
|||||||
expect(a.equals(message)).to.false;
|
expect(a.equals(message)).to.false;
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Test : _getInputPosition()', () => {
|
|
||||||
|
|
||||||
let a = new Command({
|
|
||||||
command: "wg create <id>",
|
|
||||||
callback: () => {
|
|
||||||
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(a.inputPos[0]).to.equal(2);
|
|
||||||
|
|
||||||
})
|
|
||||||
|
|
||||||
it('Test : getInput() with 1 input', () => {
|
it('Test : getInput() with 1 input', () => {
|
||||||
|
|
||||||
let message = 'wg create p12'
|
let message = 'wg create p12'
|
||||||
@@ -144,7 +131,7 @@ describe('Command', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(a.getInput(message)[0]).to.equal('p12');
|
expect(a.getInput(message)['id']).to.equal('p12');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('Test : getInput() with 2 input', () => {
|
it('Test : getInput() with 2 input', () => {
|
||||||
@@ -158,8 +145,8 @@ describe('Command', () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(a.getInput(message)[0]).to.equal('p12');
|
expect(a.getInput(message)['id']).to.equal('p12');
|
||||||
expect(a.getInput(message)[1]).to.equal('pikachu');
|
expect(a.getInput(message)['name']).to.equal('pikachu');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user