Search code examples
luadiscordbots

How to make my Lua Discord bot sends a Japanese phrase, and how to check if the expected part is null/blank?


I'm currently working on a custom Discord bot, written in Lua as a challenge out of pure spite. I started learning Lua recently, so this is a fun challenge for me! But still, I found an obstacle that pretty much confuses me. ^^"

For clearance, I'll just put the part that troubles me...

Client:on("messageCreate", function(Message)
 --Command checker
    local Content = Message.content:lower()
    local CommandYes = Message.content:sub(1,2) == Settings.Prefix --[m/ ; This is already defined, no problem here]
    if not CommandYes then
        return
    end

    Content = Content:sub(3) --The command part after prefix
    if Content == "hello" then --m/hello iterations
        if Content:sub(7,2):lower() == "jp" then
            Message:reply(string.format("こんにちは, %s-様!", Message.author.mentionString)) --Somehow the expected message doesn't send, what
        elseif Content:sub(7,2):lower() == "en" or Content:sub(7,2):lower() == --[[checkblankhow]] then
            Message:reply(string.format("Hello my master, %s-sama!", Message.author.mentionString))
        else
            Message:reply(string.format("I'm sorry, I don't know that language...\nBut hello my master, %s-sama!", Message.author.mentionString))
        end
    end
end)

Here, I tried to do a simple check on whether or not the command specifies "jp" or not. I plan for it to be only EN, JP, or none(if the checked part is not EN/JP, and if it's blank), as you can see in the code above, but it doesn't work. :')

I tried to put the code like this:

...
    Content = Content:sub(3) --The command part after prefix
    if Content == "hello" then --m/hello iterations
        Message:reply(string.format("Konnichiwa, %s-sama!", Message.author.mentionString))

        if Content:sub(7,2):lower() == "jp" then
            Message:reply(string.format("こんにちは, %s-様!", Message.author.mentionString))
        end
    end
...

But still, only the regular m/hello command works, and the m/hello jp one doesn't, like it's ignored by the bot. ._. I would be thankful if someone can help me, so お願いしますols...

Thank you in advance! ^^


Solution

  • Content:sub(7,2) is wrong: string.sub takes a start & end index (both inclusive), not a start index and a length. To get a substring of length 2, you thus have to use 8 as second parameter: Content:sub(7,8). You can test your basic "command parsing" in the REPL: ("m/hello jp"):sub(3):sub(7, 8) which yields jp as expected.

    Note that manually determining indices is quite error-prone and pretty unreadable. I'd suggest using string matching or calculating the indices by getting string lengths instead. For example:

    local command = Message.content:match"m/(.+)"
    if not command then return end
    if command == "hello" then ... end
    local command_name, param = command:match"(.-) (.+)"
    if command_name == "hello" then
        if param == "en" then ...
        elseif param == "jp" then ...
        else ... end
    end