首页 > 解决方案 > 使用命令时如何使不和谐机器人更改脚本?

问题描述

问题是,我已经设置了一个带有命令和响应的不和谐机器人。我想这样做,例如,当我在我的不和谐频道中使用“!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/





它不像我试图做的那样工作。

标签: javascriptarraysnode.jsdiscorddiscord.js

解决方案


您需要更改第一个 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/




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


推荐阅读