如何使用 DISCORDIA(LUA) 为我的机器人发出警告命令

时间:2021-01-05 18:00:02

标签: lua discord

如何在 discordia 中发出警告命令? 我已经尝试过这个,但是 .json 在 cmd 中返回为 null(没有错误),我已经尝试了很长时间,但我无法弄清楚问题所在:

elseif args[1]:lower():sub(3, #".warn") == ".warn" then
  local wopen = io.open("warns.json", "r")
    local wparse = json.parse(wopen:read("*a"))
    wopen:close()
    if args[2] then
        local mentioned_user = message.mentionedUsers.first
        local mentioned_member = message.guild:getMember(mentioned_user)
        local mentioned = message.guild:getMember(mentioned_member)

        if mentioned ~= nil then
            if args[3] then
                table.remove(args, 1) --// removes command and mention arguments
                local reason = table.concat(args) --// turns remaining contents of the table into one long string value
                if wparse[mentioned.id] then --// checks if the mentioned user exists in the database
                  wparse[mentioned.id] = wparse[mentioned.id] + 1 --// big brain math (if the user already exists, add 1 to their warnings)
                    message:reply(mentioned.username.." has been warned because: "..reason..". They now have "..wparse[mentioned.id].." warnings.")
                else --// if they mentioned user doesn't exist, we should add them to it
                    wparse[mentioned.id] = 1 --// if they don't exist, add them to the database and set their warnings to 1
                    message:reply(mentioned.username.." has been warned because: "..reason..". They now have 1 warning.")
                end
            end
        end 
    else
        message:reply("Provide a member to warn.")
    end

    wopen = io.open("warns.json", "w")
    wopen:write(json.stringify(wparse))
    wopen:close()

1 个答案:

答案 0 :(得分:0)

猜测您的 :sub() 命令是错误的。

它从位置 3 开始,但在长度为“.warn”的 5 处结束

所以 :sub(3, 5) 永远不会返回任何永远不会 == ".warn"

充其量,它可能会给你“.wa”

您需要在 sub() 中的第二个参数中添加三个

:sub(3, #".warn" +3) == ".warn"