Shell Command
in , , ,

Shell Command For Your Discord Bot

Shell Command For Your Discord Bot: It must be a pain not to be able to use the terminal of the bot host, like on Heroku and many other places where you can see your logs, but not do anything. For that reason, I will show you how to create a shell command today.

You probably want to keep this command only for the owner of it, since it can be dangerous if anyone else uses it

Steps to Create Shell Command

Start by creating an empty file named shell.js in your bot’s owner folder, then open up the terminal by typing 

npm i discord.js

Next, we require the discord.js and child_process modules to be imported into the file.

const Discord = require('discord.js');
const process = require('child_process');

Let’s export our command information based on our handler configuration, if you want to use the same handler as me, then use Starter Template by CTK WARRIOR

const Discord = require('discord.js');
const process = require('child_process');

module.exports = {
name: "shell",
usage: "shell <npm i discord.js>",
ownerOnly: true, 
cooldown: 5000,
botPermission: [],
authorPermission: [],
aliases: [],
description: "Run any shell command from discord!",
run: async (client, message, args) => {
  	
/* <The code goes here> */
  
  
      
	}
}

Firstly, we will make the bot send a message that will show the status of the command as a simple loading message

const running = await message.channel.send({embed: {
	title: 'Running Shell Command.',
	description: "Please Wait Running Command, 5 Seconds!",
	color: 'RANDOM'
}})

In the next step, we will use exec which is a child process function that basically generates a shell to execute the command.

        process.exec(args.join(" "), (error, stdout) => {
        let result = (stdout || error)
        running.delete(); //Delete the loading message
		message.channel.send(result)
	})
}

Here’s how it should look when it’s all put together

const Discord = require('discord.js');
const process = require('child_process');

module.exports = {
name: "shell",
usage: "shell <npm i discord.js>",
ownerOnly: true, 
cooldown: 5000,
botPermission: [],
authorPermission: [],
aliases: [],
description: "Run any shell command from discord!",
run: async (client, message, args) => {
  	
      const running = await message.channel.send({embed: {
	      title: 'Running Shell Command.',
	      description: "Please Wait Running Command, 5 Seconds!",
	      color: 'RANDOM'
	  }})
      
	  process.exec(args.join(" "), (error, stdout) => {
      let result = (stdout || error)
      running.delete();
	  message.channel.send(result, code: "asciidoc", split: '\n')})
	  })
	}
}

You’re done! Feel free to comment on this post if you encounter any errors or Join the CTK Discord Server!. I hope you learned something today 🙂 Thanks for stopping by! Have a wonderful day!

Related Posts

Written by Simon Wk

Hi :)

Leave a Reply