Making a Discord Bot execute an Ubuntu command

I want to make a Discord bot execute a command (sudo service terraria start) when it sees a message like "!t start". I've seen this guide https://thomlom.dev/create-a-discord-bot-under-15-minutes/ and I know how to make the bot know when you send a certain message, but I don't know how could I make it do a command. I'll copy my index.js. Thank you!

const client = new Discord.Client()
client.on("ready", () => {
          console.log(`Logged in as ${client.user.tag}!`)
})
client.on("message", msg => {
          if (msg.content === "Ping") {  
                       msg.reply("Pong!")
                    }
})

Obviously at the end would be the token.

Upvotes: 2

Views: 1379

Answers (1)

gaaaaaa
gaaaaaa

Reputation: 366

You can try using child_process

const { exec } = require("child_process");

exec("sudo service terraria start", (error, stdout, stderr) => { 
    if(error) { console.log(`error: ${error.message}`);
                return;}
    if(stderr){ console.log(`stderr: ${stderr}`); 
                return; }
    console.log(`stdout: ${stdout}`);
});

See https://nodejs.org/api/child_process.html for more details.

Upvotes: 1

Related Questions