首页 > 解决方案 > 变量未传递给模块

问题描述

我无法让我的变量之一传递给命令模块。它在声明const { token, ownerid, botid } = require('./config.json');并通过command.execute(message, args, prefix, ownerid);并通读,execute(message, args, prefix, ownerid) {但是当我尝试在模块中记录 ownerid 时,它显示为未定义。我想它在某处被抹去。我尝试在 command.execute 之前记录它,它在那里读得很好,我认为它以某种方式迷路了。

哦,这就是 ownerid 的样子:([ 'xxxxxxxxxxxxxx' ]但显然有数字)有人知道我的变量去了哪里吗?

我的临时解决方案是使用 fs 来读取模块中的文件,而不是导入变量。

代码:按使用顺序:

const { token, ownerid, botid } = require('./config.json'); //declared

//command defined
client.commands = new Discord.Collection();
const commandFolders = fs.readdirSync('./commands');

for (const folder of commandFolders) {
    const commandFiles = fs.readdirSync(`./commands/${folder}`).filter(file => file.endsWith('.js'));
    for (const file of commandFiles) {
        const commandfile = require(`./commands/${folder}/${file}`);
        client.commands.set(commandfile.name, commandfile);
    }
}

client.on('message', message => {
   //filters and stuff
   try {
        command.execute(message, args, prefix, ownerid);
    } catch (error) {
        console.error(error);
        message.reply('There was an error trying to execute that command!');
    }
});

//in module
execute(message, args, prefix, ownerid) {
        for (let i = 0; i < ownerid.length; i++) {
            const dmme = message.client.users.cache.get(ownerid[i])
            const bugreportembed = new Discord.MessageEmbed()
            .setTitle('New bug report!')
            .addField('Info', `From ${message.author.tag}`)
            .setColor('RANDOM')
            .addField('Description', message.content.replace(`${prefix}bugreport `, ""))
            dmme.send(bugreportembed)
            message.channel.send('Sent!')
        }
}

标签: discorddiscord.js

解决方案


推荐阅读