2016-07-28 67 views
2

所以我有以下代碼,問題是,當我循環遍歷數組中的每個文件並嘗試請求文件路徑時,它會給我一個未找到模塊的錯誤。我如何使require()採取一個文件的直接路徑

local Commands = {} 

function getCommands() 
    local readdir = fs.readdir 
    local readdirRecursive = require('luvit-walk').readdirRecursive 
    readdirRecursive('./Desktop/Discord/ArtifexBot/Discordia/resources/commands/', function(k, files) 
     for i,v in pairs(files) do 
      if v:match(".lua") and not v:match("commands.lua") then 
       local cmd = v:match("([^/]-)%..-$") 
       fs.readlink(v,function(err,thing) 
        print(err,thing) 
       end) 
       Commands[cmd] = require(v) 
      end 
     end 
    end) 
end 
getCommands() 

遞歸函數起作用,文件只是路徑的字符串。但是經過研究,require()需要相對路徑,而不是直接路徑。所以我想我需要用fs來做一些事情來讓文件路徑成爲相對路徑呢?我無法在任何地方找到答案。

謝謝!

回答

5

require根本不需要路徑。標準加載器只需使用您按照其算法給出的一系列模式中的字符串。

你想要的是加載和執行磁盤上給定的Lua腳本。這不是拼寫require;這是拼寫dofile

相關問題