导出变量始终 returns 为 'false'
Exported variable always returns as 'false'
如果 "exported" 这里的术语不太正确,请原谅。我正在使用 javascript 和 discord.js 为我的机器人制作一个命令处理程序,它将命令作为文件夹中的单个文件读取,直到现在它一直运行良好,我正在尝试制作一个允许你向 "lock" 机器人命令到一个特定的频道,我已经弄清楚背后的技术。但是,由于某种原因,包含频道 ID 的变量 ticketChannels
中的数据在命令启动时以某种方式变为 false
。我已经确保它没有在任何地方被重新定义,并且已经使用 several console.logs
来确保变量在被设置为所需的通道时被正确定义,但不知何故命令处理程序或其他东西正在把它变成 false
。我已经尝试了几种不同的格式化锁定频道 ID 的方法,包括单个对象数组(因为我有一个用数组格式化的点系统,我可以导出并在命令之间对其进行更改就好了,所以我想我会尝试另一个阵列),尝试将其设置为仅通道本身而不是 ID,已四重检查任何地方的任何重新定义,但没有。我很难过。有任何想法吗?
命令处理程序代码:
function readCMD() {
fs.readdir("./commands/", (err, files) => {
if (err) return console.error(err);
client.commands.deleteAll()
//deleteAll is here so that the commands list is refreshed properly everytime readCMD is called, as i have an update command that re-reads the folder using this function
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./commands/${file}`);
let commandName = file.split(".")[0];
console.log(`Attempting to load command ${commandName}`);
client.commands.set(commandName, props);
});
});
}
readCMD()
client.on('message', msg => {
if (msg.author.bot) return;
const prefix = '$'
const args = msg.content.slice(prefix.length).trim().split(/ +/g)
let command = args.shift().toLowerCase()
const cmd = client.commands.get(command)
我在核心文件中和核心文件外尝试 运行 的命令:
if (msg.content.toLowerCase().startsWith('$setticketchannel')) {
const indArgs = msg.content.split(" ")
if (indArgs[1].toLowerCase() == 'on') {
var chosenChannel = msg.mentions.channels.first()
console.log(chosenChannel.id)
ticketChannels = chosenChannel.id
console.log(ticketChannels)
} else if (indArgs[1].toLowerCase() == 'off') {
ticketChannels = 0
}
}
执行:
try {
cmd.run(client, msg, args, bank, currentPoints, fs, duelon, ticketChannels)
}catch(e) {
console.log(e)
}
以及尝试检查 ID 的命令代码:
exports.run = (client, msg, args, bank, currentPoints, fs, ticketChannels) => {
console.log("$GAMBLE:")
console.log(ticketChannels)
if (ticketChannels !== 0) {
if (msg.channel.id !== ticketChannels) {
msg.reply(`Commands such as these can only be done in ${ticketChannels}`)
return
}
}
需要说明的是,我使用命令处理程序导出的所有其他变量都工作得很好,我可以在各个命令内外编辑和重新定义它们。另外:并不是 if
语句返回 false,而是 变量本身 正在变成 false
,尽管没有定义它。
我今天才弄明白这个问题。显然在我的 cmd.run(client, msg, args, bank, currentPoints, fs, duelon, ticketChannels)
行中,因为 exports.run = (client, msg, args, bank, currentPoints, fs, ticketChannels)
行不包含变量 duelon
,其中 是 布尔值,这令人困惑 ticketChannels
与 duelon
不知何故。在 exports.run
行中包含 duelon
使其工作得很好。
如果 "exported" 这里的术语不太正确,请原谅。我正在使用 javascript 和 discord.js 为我的机器人制作一个命令处理程序,它将命令作为文件夹中的单个文件读取,直到现在它一直运行良好,我正在尝试制作一个允许你向 "lock" 机器人命令到一个特定的频道,我已经弄清楚背后的技术。但是,由于某种原因,包含频道 ID 的变量 ticketChannels
中的数据在命令启动时以某种方式变为 false
。我已经确保它没有在任何地方被重新定义,并且已经使用 several console.logs
来确保变量在被设置为所需的通道时被正确定义,但不知何故命令处理程序或其他东西正在把它变成 false
。我已经尝试了几种不同的格式化锁定频道 ID 的方法,包括单个对象数组(因为我有一个用数组格式化的点系统,我可以导出并在命令之间对其进行更改就好了,所以我想我会尝试另一个阵列),尝试将其设置为仅通道本身而不是 ID,已四重检查任何地方的任何重新定义,但没有。我很难过。有任何想法吗?
命令处理程序代码:
function readCMD() {
fs.readdir("./commands/", (err, files) => {
if (err) return console.error(err);
client.commands.deleteAll()
//deleteAll is here so that the commands list is refreshed properly everytime readCMD is called, as i have an update command that re-reads the folder using this function
files.forEach(file => {
if (!file.endsWith(".js")) return;
let props = require(`./commands/${file}`);
let commandName = file.split(".")[0];
console.log(`Attempting to load command ${commandName}`);
client.commands.set(commandName, props);
});
});
}
readCMD()
client.on('message', msg => {
if (msg.author.bot) return;
const prefix = '$'
const args = msg.content.slice(prefix.length).trim().split(/ +/g)
let command = args.shift().toLowerCase()
const cmd = client.commands.get(command)
我在核心文件中和核心文件外尝试 运行 的命令:
if (msg.content.toLowerCase().startsWith('$setticketchannel')) {
const indArgs = msg.content.split(" ")
if (indArgs[1].toLowerCase() == 'on') {
var chosenChannel = msg.mentions.channels.first()
console.log(chosenChannel.id)
ticketChannels = chosenChannel.id
console.log(ticketChannels)
} else if (indArgs[1].toLowerCase() == 'off') {
ticketChannels = 0
}
}
执行:
try {
cmd.run(client, msg, args, bank, currentPoints, fs, duelon, ticketChannels)
}catch(e) {
console.log(e)
}
以及尝试检查 ID 的命令代码:
exports.run = (client, msg, args, bank, currentPoints, fs, ticketChannels) => {
console.log("$GAMBLE:")
console.log(ticketChannels)
if (ticketChannels !== 0) {
if (msg.channel.id !== ticketChannels) {
msg.reply(`Commands such as these can only be done in ${ticketChannels}`)
return
}
}
需要说明的是,我使用命令处理程序导出的所有其他变量都工作得很好,我可以在各个命令内外编辑和重新定义它们。另外:并不是 if
语句返回 false,而是 变量本身 正在变成 false
,尽管没有定义它。
我今天才弄明白这个问题。显然在我的 cmd.run(client, msg, args, bank, currentPoints, fs, duelon, ticketChannels)
行中,因为 exports.run = (client, msg, args, bank, currentPoints, fs, ticketChannels)
行不包含变量 duelon
,其中 是 布尔值,这令人困惑 ticketChannels
与 duelon
不知何故。在 exports.run
行中包含 duelon
使其工作得很好。