【问题标题】:How to make discord bot to change script when command is used?使用命令时如何使不和谐机器人更改脚本?
【发布时间】:2020-03-05 19:24:17
【问题描述】:

问题是,我已经设置了一个带有命令和响应的不和谐机器人。我想这样做,例如,当我在我的不和谐频道中使用“!changeport 2222”时,它会更改里面的实际代码,应该将其更改为 var port = 2222;。如果我使用“!changeport 80”,则应将其更改为 var port = 80;内部脚本。

我尝试了几种方法,但没有运气。我也不是专业的程序员。

var port = 80;
var portscanner = require('portscanner')
var used = false;

//command that is used in discord channel to change the port
client.on("message", (message) => {
var port = message.content.split(" ")[1]
var cooldowntext = ("You need to wait before using that command!")  
  if (message.content.startsWith("!port")) { 
     if(used) message.channel.send(cooldowntext);
     else{
    portscanner.checkPortStatus(port, '127.0.0.1', function(error, status) {
      // Status is 'open' if currently in use or 'closed' if available
      message.channel.send("Port changed to ");
      console.log(status)
      update()
      used = true;
      setTimeout(() => {
          used = false;
      }, 1000 * 10);

    })
     }


  }

});
//command that is used in discord channel to change the port /end/





它没有按照我尝试的方式工作。

【问题讨论】:

    标签: javascript arrays node.js discord discord.js


    【解决方案1】:

    您需要更改第一个 var 的值:

    var port = 80;
    var portscanner = require('portscanner')
    var used = false;
    
    //command that is used in discord channel to change the port
    client.on("message", (message) => {
    var newPort = message.content.split(" ")[1]
    var cooldowntext = ("You need to wait before using that command!")  
      if (message.content.startsWith("!port")) { 
         if(used) message.channel.send(cooldowntext);
         else{
        port = newPort;
        portscanner.checkPortStatus(port, '127.0.0.1', function(error, status) {
          // Status is 'open' if currently in use or 'closed' if available
          message.channel.send("Port changed to ");
          console.log(status)
          update()
          used = true;
          setTimeout(() => {
              used = false;
          }, 1000 * 10);
    
        })
         }
    
    
      }
    
    });
    //command that is used in discord channel to change the port /end/
    
    
    
    
    

    我用newPort 替换了您的第二个port var,并在运行命令时将第一个port 值更改为newPort 值。

    【讨论】:

      猜你喜欢
      • 2021-10-30
      • 2019-05-11
      • 2018-11-10
      • 1970-01-01
      • 2018-07-21
      • 2021-03-28
      • 2021-12-31
      • 1970-01-01
      • 2018-07-10
      相关资源
      最近更新 更多