使Discord Bot执行Ubuntu命令

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.
评论
  • [呛人心]
    [呛人心] 回复

    您可以尝试使用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.