简体   繁体   English

使 Discord Bot 执行 Ubuntu 命令

[英]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".我想让 Discord 机器人在看到类似“.t start”的消息时执行命令(sudo service terraria 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.我已经看过本指南https://thomlom.dev/create-a-discord-bot-under-15-minutes/我知道如何让机器人知道您何时发送某个消息,但我不知道我怎么能让它做一个命令。 I'll copy my index.js.我将复制我的 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.

You can try using child_process您可以尝试使用 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.有关详细信息,请参阅https://nodejs.org/api/child_process.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM