使Discord Bot执行Ubuntu命令

时间:2020-05-18 02:14:11

标签: javascript bots discord

我想让Discord机器人看到“!t start”之类的消息时执行命令(sudo服务Terraria启动)。我已经看过本指南https://thomlom.dev/create-a-discord-bot-under-15-minutes/,并且我知道如何在发送特定消息时使机器人知道,但是我不知道如何使它执行命令。我将复制index.js。 谢谢!

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.

1 个答案:

答案 0 :(得分:1)

您可以尝试使用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}`);
});

有关更多详细信息,请参见https://nodejs.org/api/child_process.html