【问题标题】:Making a Discord Bot execute an Ubuntu command让 Discord Bot 执行 Ubuntu 命令
【发布时间】:2020-09-03 18:24:47
【问题描述】:

我想让 Discord 机器人在看到类似“!t start”的消息时执行命令(sudo service terraria start)。我看过这个指南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.

【问题讨论】:

    标签: javascript bots discord


    【解决方案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

    【讨论】:

      猜你喜欢
      • 2020-10-30
      • 2017-06-04
      • 2021-01-06
      • 2021-04-07
      • 2018-03-04
      • 1970-01-01
      • 1970-01-01
      • 2018-09-12
      • 2020-10-25
      相关资源
      最近更新 更多