使用命令时如何使Discord bot更改脚本?

问题描述 投票:0回答:1

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

您需要更改第一个变量的值:

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/




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

© www.soinside.com 2019 - 2024. All rights reserved.