Sign in to follow this  
Gross

tutorial Add Command Line to server console

Recommended Posts

Create file 'commandline.js' to dir 'packages/keker' with the following code:

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

function ParseCmd(s){
	var args = s.split(" ");
	var cmd = args[0].toLowerCase();
	var res = '';
	switch(cmd){
		case "ban":
			mp.players.forEach(player => { 
				if(player.name.toLowerCase() == args[1].toLowerCase()){
					player.ban("Console");
					res = " Player "+player.name+" was banned!!!";
				} 
				});
			break;
		case "kick":
			mp.players.forEach(player => { 
				if(player.name.toLowerCase() == args[1].toLowerCase()){
					player.kick("Console");
					res = " Player "+player.name+" was kicked!!!";
				} 
				});
			break;
		case "tppos":
			if(args.length>4){
				mp.players.forEach(player => { 
				if(player.name.toLowerCase() == args[1].toLowerCase()){
					player.position = new mp.Vector3(parseFloat(args[2]), parseFloat(args[3]), parseFloat(args[4]));
					res = " Player "+player.name+" was teleported to X:"+parseFloat(args[2])+" Y:"+parseFloat(args[3])+" Z:"+parseFloat(args[4])+"!!!";
				} 
				});
			} else {
				res = " Invalid arguments!!!"
			}
			break;
		case "give.weapons":
			if(args.length>3){
				mp.players.forEach(player => { 
				if(player.name.toLowerCase() == args[1].toLowerCase()){
					player.giveWeapon(mp.joaat(args[2]), parseInt(args[3]));
					res = " Player "+player.name+" received weapons!!!";
				} 
				});
			} else {
				res = " Invalid arguments!!!"
			}
			break;
		case "status":
			res = "\n Players: "+mp.players.length+"/"+mp.players.size+"\n Vehicles: "+mp.vehicles.length+"\n Objects: "+mp.objects.length+"\n Wheather: "+mp.environment.weather+"\n Game Time: "+mp.environment.time.hour+"h\n Uptime: "+process.uptime()+"\n";
			break;
		case "online":
			res = "\n Online: "+mp.players.length+"/"+mp.players.size+"\n ";
			mp.players.forEach(player => {
				res += player.name+" | "+player.ip+" | "+player.ping+"\n ";
			});
			break;
		default:
			res = " Unknown command!!!";
			break;
	}
	return res;
}

rl.on('line', (s) => {
	var res = ParseCmd(s);
	console.log(res);
});

After that,  append to file 'packages/keker/index.js' the following line:

require("./commandline");

Then you can execute commands on the server console.

The list of available commands as seen from the code:

ban <PlayerName> -> Kiked player
kick <PlayerName> -> Baned player
tppos <PlayerName> <X> <Y> <Z> -> Teleport player to coordinates
give.weapons <PlayerName> <WeaponName> <Amount> -> Give weapon to player
status -> Show server info
online -> Show players list with info(Name,IP,Ping)

 

  • Like 3
  • Mask 1

Share this post


Link to post
Share on other sites
20 minutes ago, alex_r said:

Isn't it better to make separate package?

Why, for quick installation through NPM?

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.