How can I make a kick script with reasoning, using string.sub?

So I recently have been trying to find out how to make a “!warn” command that when executed, gets sent to a weebhooki in discord. I have been able to accomplish part of this but, I am trying to get the reason after the player’s name, how would I get that in the weebhook, when I execute it? Like for example, what I mean is like, when I say “!warn NACKRR Being rude” how could I get the reasoning after the player’s name?

  • I hope someone is able to anwer my question and if you are not, thank you for reading this then! :slight_smile:

string.sub wouldn’t be what you want. You want string.split. You can use it to split between spaces, get everything after the first two strings.

local reason = table.concat({ table.unpack(string.split(message, " "), 3), " ") }

I use table.concat because it would also split the reason if there were spaces there. And I undo this with table.concat.

table.concat undoes string.split with the same delimiter
string.split undoes table.concat with the same delimiter


The better solution came to my head.

Use string.match! If you have any capture patterns, string.match will return those captures as well!

local reason = select(2, string.match(message, "!warn (%w+) (.*)"))

Since we have 2 captures (wrapped in the parentheses), we don’t want the first one since that is just the name, so we get the second capture via select(2)

I get what your saying, but I am a bit confused, what is the “(%w+)” and “(.*)” in the code mean?

If this’s an optional parameter, you could get the third parameter with the help of String:find(Match), as it returns the start and end position of the match within the string, or if it didn’t find a match it’ll return nil.

Here’s an example with find with the third parameter being optional.

local Command = "!warn player being a big nerd";
local SeparatorPosition1 = Command:sub(7):find(" ");

print(Command:sub(1, 6)); -- !warn

if (SeparatorPosition ~= nil) then -- Check if separator didn't return nil (nil = no match was found)
    SeparatorPosition1 =  SeparatorPosition1 + 6;
    print(Command:sub(7, SeparatorPosition1 - 1)); -- player
    print(Command:sub(SeparatorPosition1 + 1)); -- being a big nerd
else
    print(Command:sub(7)); -- If there was no third parameter found
end

%w is alphanumeric, so abc123 is alphanumeric, since it has an alphabetical characters and numbers, it can have one or the other as well. The + is there to show we want 1 or more occurrences. Names can be alphanumeric. . means match ANY string. The * means match 0 or more occurrences.

Problem with that is you need to know the length of the player’s name which you won’t always know.

Okay, thank you for clearing this up for me.

Thank you for this example, I understand it a lot better now with @incapaxx clearing this up for me as well.