S
S
Sanders Rocket2020-04-22 17:38:19
JavaScript
Sanders Rocket, 2020-04-22 17:38:19

Why doesn't the check work?

if (msg.message.text.match('!kick')) {
      if(await isAdmin(msg)) {
        let playerid = msg.message.text
        let findplayer = playerid.match(/\[id(\d+)/)[1];
        let causes = playerid.match(/] (.+)/)[1];
        if (findplayer && causes === ''){
          messagesend('запрос неверный')
        } else {
          vk.call('messages.removeChatUser', {
            chat_id: msg.message.peer_id - 2000000000,
            user_id: findplayer
          });
          messagesend('🔫Был исключен vk.com/id' + findplayer + ' | Причина: ' + causes)
        }
      } else {
        notmessage();
      }
    }

What is the problem? The conditions don't work. As it should be when you enter !kick or !kick id or !kick and reason, it should display a message that the request is not valid

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Ivan Klimenko, 2020-04-22
@sandersik

It should most likely be like this:

async function test() {

    // ...

    if (msg.message.text.startsWith('!kick')) {
        if (await isAdmin(msg)) {
            const str = msg.message.text;
            const idStart = str.indexOf(' ');
            const idEnd = str.indexOf(' ', idStart + 1);
            const id = str.slice(idStart + 2, idEnd);
            const reason = str.slice(idEnd + 1);
            if ((idStart < 0) || (idEnd < 0) || (id.length === 0) || (reason.length === 0)) {
                messagesend('Неверная команда.');
            } else {
                vk.call('messages.removeChatUser', {
                    chat_id: msg.message.peer_id - 2000000000,
                    user_id: id
                });
                messagesend(`&#128299; Был исключен vk.com/${id} | Причина: ${reason}`);
            }
        } else {
            notmessage();
        }
    }

    // ...

}

G
galaxy, 2020-04-23
@galaxy

If the regular expression /\[id(\d+)/ does not match, match() will return null, and null[1] will throw an exception. Similarly with the second regex. You will never get an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question