You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Minecraft OpenComputers中Lua程序执行完成后触发stdin错误

问题分析与修复

错误原因

你遇到的bad argument #1 to 'find' (string expected, got nil)错误,核心原因是手动字符串截取GitHub API返回的JSON时,处理到最后残留的JSON内容出现参数异常。你的代码通过字符串查找download_url提取链接的方式非常脆弱——JSON结构的微小变化(比如空格、转义字符)都会导致截取位置出错,最终让某个string.find调用的参数变成nil。

此外代码还有几个潜在问题:

  1. 重复require("io")并覆盖全局io变量,可能干扰OpenComputers的内部IO处理逻辑
  2. 手动创建目录时不支持递归创建,若文件路径包含多层未存在的目录,会创建失败
  3. 未处理仓库中的子目录,会漏掉深层文件
  4. 未处理API返回的分页内容(若仓库文件数量较多)

修复后的代码

使用OpenComputers内置的json模块解析API响应,递归处理目录,同时修复IO和目录创建的问题:

local internet = require("internet")
local io = io -- 无需重新require,直接使用全局io
local filesystem = require("filesystem")
local json = require("json")

local base_url = "https://api.github.com/repos/Vedvart/Minecraft/contents"
local save_root = "/home/github/"

-- 递归创建目录
local function mkdirp(path)
    path = filesystem.canonical(path)
    if not filesystem.exists(path) then
        local parent = filesystem.path(path)
        if parent ~= path then -- 避免无限递归
            mkdirp(parent)
        end
        filesystem.makeDirectory(path)
    end
end

-- 递归下载仓库内容
local function download_contents(url, local_path)
    local handle = internet.request(url)
    local response = ""
    for chunk in handle do
        response = response .. chunk
    end

    local items = json.decode(response)
    for _, item in ipairs(items) do
        if item.type == "file" then
            -- 跳过.gitattributes
            if item.name == ".gitattributes" then
                goto continue
            end

            local file_local_path = local_path .. item.name
            print("下载文件: " .. file_local_path)

            -- 创建父目录
            mkdirp(filesystem.path(file_local_path))

            -- 下载文件内容
            local content_handle = internet.request(item.download_url)
            local content = ""
            for chunk in content_handle do
                content = content .. chunk
            end

            -- 写入文件
            local file = io.open(file_local_path, "w")
            if file then
                file:write(content)
                file:close()
            else
                print("无法写入文件: " .. file_local_path)
            end
        elseif item.type == "dir" then
            -- 递归处理子目录
            local sub_local_path = local_path .. item.name .. "/"
            print("进入目录: " .. sub_local_path)
            download_contents(item.url, sub_local_path)
        end
        ::continue::
    end
end

-- 开始下载
mkdirp(save_root)
download_contents(base_url, save_root)

print("所有文件下载完成!")

修复说明

  1. 使用JSON解析:通过json.decode将API响应转为Lua表,彻底避免手动字符串截取的各种异常
  2. 递归目录处理:自动识别并处理仓库中的子目录,不会漏掉深层文件
  3. 可靠的目录创建:mkdirp函数递归创建所有父目录,确保文件路径有效
  4. 避免覆盖全局变量:不再重新赋值io,防止干扰OpenComputers内部逻辑
  5. 清晰的流程拆分:将下载、目录创建等功能拆分为独立函数,代码可读性和可维护性更强

这样修改后,不仅能解决你遇到的string.find参数错误,还能处理仓库中的子目录,同时避免其他潜在问题。

内容的提问来源于stack exchange,提问作者Vedvart1

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 17:45:47