如何在Discordia(luvit)中使用require

问题描述 投票:0回答:1

所以当我只使用 require(filename) 时它不起作用,我的意思是如果我使用 require("filename"),它认为它是一个模块,所以无法打开它,只有当我使用 require(". /filename.lua"),但是它没有从我需要的文件中获取任何数据。我可以进一步提供有关文件包含和其他内容的更多详细信息,但这正是我所需要的。 我的主文件:

local dia = require("discordia")
local tools = require("discordia-slash").util.tools()
local CLIENT = dia.Client():useApplicationCommands()
local mycommands = require('./cstmcommands.lua')

我在 CLIENT:on() print(Commands) 中有一个数组,它在 cstmcommands.lua 中具有函数,但它打印 nil。我尝试从 cstmcommands.lua 打印其他内容,但它总是打印 nil。

提供一些信息、指南、教程,或直接回答问题如何用 require 解决这个问题,即使我确实需要()它也没有从 luvit 上编写的 Discordia 核心文件中获取数据。

终端输出


C:\Unnesessary\Smallbot on Lua>luvit ./main
2023-09-03 21:35:01 | [INFO]    | Discordia 2.11.2
2023-09-03 21:35:01 | [INFO]    | Connecting to Discord...
2023-09-03 21:35:02 | [INFO]    | Authenticated as (botname)
2023-09-03 21:35:02 | [INFO]    | Launching shard 0 (1 out of 1)...
2023-09-03 21:35:02 | [INFO]    | Shard 0 : Connected to wss://gateway.discord.gg
2023-09-03 21:35:02 | [INFO]    | Shard 0 : Received HELLO
2023-09-03 21:35:02 | [INFO]    | Shard 0 : Received READY
Uncaught Error: C:\Unnesessary\Smallbot on Lua\main.lua:26: attempt to index global 'Commands' (a nil value)
stack traceback:
        C:\Unnesessary\Smallbot on Lua\main.lua: in function <C:\Unnesessary\Smallbot on Lua\main.lua:7>
stack traceback:
        [C]: in function 'error'
        C:/Unnesessary/Smallbot on Lua/deps/coro-channel.lua:16: in function 'assertResume'
        C:/Unnesessary/Smallbot on Lua/deps/coro-channel.lua:69: in function 'onPlain'
        ...nesessary/Smallbot on Lua/deps/secure-socket/biowrap.lua:76: in function <...nesessary/Smallbot on Lua/deps/secure-socket/biowrap.lua:61>
        [C]: in function 'run'
        [string "bundle:/init.lua"]:55: in function <[string "bundle:/init.lua"]:47>
        [C]: in function 'xpcall'
        [string "bundle:/init.lua"]:47: in function 'fn'
        [string "bundle:deps/require.lua"]:310: in function <[string "bundle:deps/require.lua"]:266>

C:\Unnesessary\Smallbot on Lua>pause

cstm命令内容

Commands = {}
Cmd = {
    ["вітання"] = {
        name = "вітання",
        description = "Надішли вітання боту, або обраному користувачу",
        options = {
            {
                name = "користувач",
                description = "Обери користувача, якому бот надішле повідомлення",
                type = "user",
                required = false,
                choises = nil
            }
        }
    }
}

function Commands:apply()
    entered_commands = {}
    for group, optcmd in pairs(Cmd) do
        for _, v in pairs(optcmd) do
            local SlashCommand = {}
            local option = {}
            
            for i, v in pairs(optcmd.options) do
                n = i
                local optiontype = v.type
                option = {tools.optiontype(optcmd.options.v.name, optcmd.options.v.description)}
            end
            SlashCommand = tools.slashCommand(optcmd.name, optcmd.description)
            local n = 0
           
            for i = 1, n, 1 do
                SlashCommand = SlashCommand:addOption(option[i])
            end
            entered_commands[group] = SlashCommand
        end
    end
    return entered_commands
end
lua discord require luajit luvit
1个回答
0
投票

我找到了答案,这里是如何在 Discordia 中使用 require() 如果您有命令,请使用您需要的文件中的路径

local Commands = require("./name of your file.lua")
如果您有很多命令也需要它们

Commands = require("./path/to/file.lua")
something = require('./idk.lua")

然后转到你的文件,并使用类似的返回函数

Commands = {1, 5, 10}
return Commands

记得在你的文件中执行此操作,然后你可以从命令中打印内容 如果您需要返回许多值,请使用

return {Commands, Something, else}

现在您知道如何要求并可以用它制作一些项目,但请记住,如果仅在一个使用的要求中,文件无法将内容相互转换,所以如果您在第一个文件中有值并在第二个文件中要求它,但是您具有 2nd in 1st 的值,您必须识别它们或要求 2nd in 1st,如下所示: 第一个文件:

krona = require("./2ndfile.lua")
function doprint()
print(krona)
end
return doprint()

第二:

local doprint = require("./1stfile.lua")
krona = 3
return krona
© www.soinside.com 2019 - 2024. All rights reserved.