WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   Respond to Chat commands (https://www.wowinterface.com/forums/showthread.php?t=39562)

Lanodar 04-08-11 05:39 AM

Respond to Chat commands
 
Hey

I'm writing an Addon to organize Baradin Hold Classruns.

Players have to send !class role per whisper and will be invited if their slot is available.

If someone writes !druid tank , !druid healer or !druid DPS, they'll receive a whisper and will be invited.

But if someone writes !DRUID TANK or !warlock healer. nothing will happen because those commands aren't defined.

How can i realize that if someone posts !warlock healer, there will be an answer like "Undefined command. DPS Classes just need to post ther Class (!warlock,!rogue,!mage)"

Or if Someone posts "!DROOD HEALER! "Undefined command, please use !druid healer" in lower case letters)

Mischback 04-08-11 05:41 AM

You'll have to use RegEx to parse the received whispers.

Should easily be possible, but be aware of the great opportunity of typos people could make^^.

Ketho 04-08-11 08:44 AM

for case-insensitive search, using character sets with the pattern/regex might work
Code:

if strfind(yourmsg, "[Dd][Rr][Uu][Ii][Dd] [Tt][Aa][Nn][Kk]") then
        -- do stuff
end

but yes, if people make typos it wont work ..

-- Edit:
NitraMo is right;
Myself, I'm way to accustomed with using character sets for in string.gsubs ;(

NitraMo 04-08-11 09:34 AM

Quote:

Originally Posted by Ketho (Post 234082)
for case-insensitive search, using character sets with the pattern/regex might work
Code:

if strfind(yourmsg, "[Dd][Rr][Uu][Ii][Dd] [Tt][Aa][Nn][Kk]") then
        -- do stuff
end

but yes, if people make typos it wont work ..

A prettier handling would be using the lower function.
Code:

if strfind(yourmsg:lower(), "druid tank") then
        -- do stuff
end


Vlad 04-08-11 12:25 PM

Code:

if yourmsg:lower():find("druid tank") then
  -- do stuff
end

We can do this all day! :D

By the way in regex you can do "hello (tank|healer)" and it would accept "hello tank" or "hello healer" -I guess it's not possible in lua or did I miss something?

Ketho 04-08-11 01:23 PM

Quote:

Originally Posted by Vladinator (Post 234096)
[...]
By the way in regex you can do "hello (tank|healer)" and it would accept "hello tank" or "hello healer" -I guess it's not possible in lua or did I miss something?

it's not possible. at least, not without an external library

afaik, Lua "Pattern Matching" does not support alternation
I would link to Stack Overflow for more info why regular expressions aren't implemented in Lua

Disclaimer: I'm a regex and lua patterns noob -.-

Akkorian 04-08-11 01:41 PM

The simplest method would be to capture everything between the exclamation point and the last word, and then capture the last word. That will end you up with things like "DEATHKNIGHT" or "DRUID" for the first capture, and "TANK" or "DPS" for the second one:

Code:

        local class, role = message:upper():match( "!(.+) (%S+)$" )
        -- Turn eg. "DEATH KNIGHT" into "DEATHKNIGHT"
        class = class:replace( " ", "" )

From there you can check if class == "DRUID" and role == "TANK" then or any other combination.

Lanodar 04-08-11 05:14 PM

I tried all day and i've finally found a solution :D
I combined, string.sub, string.find and string.gsub to split "!druid tank" into two parts with


Code:


arg1 = string.lower(arg1) -- converts arg1 into lower case
part1 =string.gsub(arg1,string.sub(arg1,string.find(arg1," ",1),string.len(arg1)),"")-- finds " " in arg1 and cuts off everything from " " to the end off arg1.

part2  =  string.gsub(arg1,string.sub(arg1,1,string.find(arg1," ")),"")-- same like part1 but cuts off everything before the " "

If there's only one word in the whisper, you'll receive an LUA error because there's noch space (" ") in arg1.
To avoid this I had to do the following:


Code:

    arg1 = strlower(arg1)
space = string.find(wmsg," ")
if space ~= nil then
part1 =string.gsub(arg1,string.sub(arg1,string.find(arg1," ",1),string.len(arg1)),"")
part2  =  string.gsub(arg1,string.sub(arg1,1,string.find(arg1," ")),"")
else
part1=arg1
part2=nil
end



And if part1 == "!druid" and part2=="dps" or part2==nil (or other class/specc combinations) the addon will do stuff.

If something like "!omgwtf1337" is whispered. it sends a message with a command list.

Akkorian 04-12-11 01:08 AM

Hmm, that’s probably the least efficient solution you could possibly make. :(

Was there a problem with the solution I posted right before your post? If you want lowercase instead of uppercase it’s easy enough to change “:upper()” into “:lower()”. I just used uppercase so you could easily get the “official” class name for class coloring and things like that.

Xinhuan 04-12-11 12:40 PM

I'm not sure why your code is so complicated.

Code:

arg1 = string.gsub(arg1, "%s+", " ")    -- Replaces consecutive spaces with one space
part1, part2 = string.split(" ", arg1)  -- splits arg1 by the delimiter " " character
part1 = string.lower(part1)
part2 = string.lower(part2)

Note that string.split() returns as many results as there are strings delimited by the delimiter.


All times are GMT -6. The time now is 11:18 AM.

vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI