Jump to content

alex_r

Members
  • Posts

    6
  • Joined

  • Last visited

Posts posted by alex_r

  1. Good job! Also I bit updated your code:

    function findPlayerByIdOrNickname(playerName) {
      if (playerName == parseInt(playerName))
        return mp.players.at(playerName);
      else {
        let foundPlayer = null;
        
        mp.players.forEach((_player) => {
          if (_player.name === playerName) {
            foundPlayer = _player;
            break;
          }
        });
        
        return foundPlayer;
      }
    }
    
    
    module.exports = {
    
      // Add command `/pm`
      "pm": (player, args, fullcmd) => {
        // Check args
        if (args.length < 3 || !args[1].length || !args[2].length) {
          player.outputChatBox('Valid syntax: <b>/pm [recipient_id_or_nickname] [text_message]</b>');
    
          return false;
        }
    
        // Search recipient by second argument
        const recipient = findPlayerByIdOrNickname(args[1]);
    
        // If recipient not found show special message
        if (!recipient) {
          player.outputChatBox('<b>User not found</b>');
    
          return false;
        }
    
        // Source message
        const message = fullcmd.substr(fullcmd.indexOf(args[1]) + args[1].length + 1);
        // Generate chat string in the following format:
        // [PM] Sender_Nickname[Sender_Id] -> Recipient_Nickname[Recipient_Id]: Text message
        const str = `<b>[PM] ${player.name}[${player.id}] -> ${recipient.name}[${recipient.id}]</b>: ${message}`;
    
        // Send message to recipient
        recipient.outputChatBox(str);
        // Send message to sender
        player.outputChatBox(str);
      }
    
    };

     

    • Like 1
×
×
  • Create New...