Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

discord - How do i make a warn command for my bot using DISCORDIA(LUA)

How can i make a warn command in discordia? i have tried this but the .json returns as null(no error) in cmd, ive been trying for ages i just cant figure out the problem:

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

guessing your :sub() command is wrong.

it begins at position three, but then ends at length of ".warn" which is 5

so :sub(3, 5) will never return anything that will never == ".warn"

at best, it might give you ".wa"

you need to add three to the second arg in your sub()

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...