Danil_Valov

Private Messages (PM)

5 posts in this topic

Posted (edited)

Create file `pm.js` to directory `packages/keker/commands` with the following code:

findPlayerByIdOrNickname = playerName => {
  let foundPlayer = null;

  // If playerName is numberic
  if (playerName == parseInt(playerName)) {
    // search player by ID
    foundPlayer = mp.players.at(playerName);
  }

  // or search player by nickname
  if (!foundPlayer) {
    mp.players.forEach((_player) => {
      if (_player.name === playerName) {
        foundPlayer = _player;
      }
    });
  }

  return foundPlayer;
};

module.exports = {

  // Add command `/pm`
  "pm": (player, args) => {
    // 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 = args.slice(2).join(' ');
    // 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);
  }

};

 

Now if you write in the chat: `/pm [id_or_nickname] [text_message]` you send the private message.

Edited by Danil_Valov
3 people like this

Share this post


Link to post
Share on other sites

Posted (edited)

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);
  }

};

 

Edited by alex_r
1 person likes this

Share this post


Link to post
Share on other sites

Thank you for your Tutorial. I liked the tutorial so much.

I wish you do more in coming future :) 

1 person likes this

Share this post


Link to post
Share on other sites
В 08.01.2017 в 20:48, alex_r сказал:

Also I bit updated your code:

Thank you! I added a few your solution to my post!

 

But you can make your code better if you change a few small things:

  • Command `break;` for `forEach` doesn't work in your code :(  `break` doesn't work for `forEach`. Only for `for`.
  • You don't need `else` in the function `findPlayerByIdOrNickname` because `return` stops running of function.
  • Nickname can be numberic. But you send the response by user id only if the name is numberic.
Edited by Danil_Valov

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.