Danil_Valov Posted January 7, 2017 Share 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 Link to comment Share on other sites More sharing options...
alex_r Posted January 8, 2017 Share 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 Link to comment Share on other sites More sharing options...
Captien Posted January 8, 2017 Share Posted January 8, 2017 Thank you for your Tutorial. I liked the tutorial so much. I wish you do more in coming future 1 Link to comment Share on other sites More sharing options...
Danil_Valov Posted January 10, 2017 Author Share 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 Link to comment Share on other sites More sharing options...
M4rK Posted January 11, 2017 Share Posted January 11, 2017 Thanks for the lesson! Link to comment Share on other sites More sharing options...
Avery M. Posted August 30, 2017 Share Posted August 30, 2017 i dont have command folder Link to comment Share on other sites More sharing options...
Elliot Posted September 1, 2017 Share 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 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now