Danil_Valov 22 Posted January 7, 2017 (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 January 10, 2017 by Danil_Valov 3 Share this post Link to post Share on other sites
alex_r 2 Posted January 8, 2017 (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 January 8, 2017 by alex_r 1 Share this post Link to post Share on other sites
Captien 134 Posted January 8, 2017 Thank you for your Tutorial. I liked the tutorial so much. I wish you do more in coming future 1 Share this post Link to post Share on other sites
Danil_Valov 22 Posted January 10, 2017 (edited) В 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 January 10, 2017 by Danil_Valov Share this post Link to post Share on other sites
M4rK 3 Posted January 11, 2017 Thanks for the lesson! Share this post Link to post Share on other sites
Avery M. 0 Posted August 30, 2017 i dont have command folder Share this post Link to post Share on other sites
Elliot 47 Posted September 1, 2017 On 30/08/2017 at 10:54 AM, Avery M. said: i dont have command folder It doesn't have to go in a commands folder, where ever your put it just remember to require it in your serverside index.js Share this post Link to post Share on other sites