javascript - 使用命令时如何使不和谐机器人更改脚本?
问题描述
问题是,我已经设置了一个带有命令和响应的不和谐机器人。我想这样做,例如,当我在我的不和谐频道中使用“!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/
它不像我试图做的那样工作。
解决方案
您需要更改第一个 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/
我替换了您的第二个port
varnewPort
并将第一个port
值更改newPort
为命令运行时的值。
推荐阅读
- c# - 生成控制器时,“视图模型”需要主键”错误
- javascript - 无法读取 admin.firestore.collection.doc.get.then.doc 中未定义的属性“firstName”
- java - HTTP代码返回后如何通知客户端异常
- r - 对 tobit 进行摘要时出错:$ 运算符对原子向量无效
- javascript - 从从 localStorage 导入的字符串中删除引号
- javascript - 创建元素后Vuetify滚动
- python - 仅使用标准库在 Python 中打开 SSH 会话
- sublimetext2 - Sublime Text 中的德语引号
- xml - 如何将 XML 文件可视化为图形?
- spring-roo - 无法在 macOS Mojave 10.14 中安装 Spring Roo 2