Thread Tools Display Modes
04-08-11, 05:39 AM   #1
Lanodar
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 6
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)
  Reply With Quote
04-08-11, 05:41 AM   #2
Mischback
A Cobalt Mageweaver
 
Mischback's Avatar
AddOn Author - Click to view addons
Join Date: Aug 2009
Posts: 221
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^^.
__________________
  Reply With Quote
04-08-11, 08:44 AM   #3
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
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 ;(

Last edited by Ketho : 04-08-11 at 12:22 PM.
  Reply With Quote
04-08-11, 09:34 AM   #4
NitraMo
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 24
Originally Posted by Ketho View Post
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
__________________
  Reply With Quote
04-08-11, 12:25 PM   #5
Vlad
A Molten Giant
 
Vlad's Avatar
AddOn Author - Click to view addons
Join Date: Dec 2005
Posts: 793
Code:
if yourmsg:lower():find("druid tank") then
  -- do stuff
end
We can do this all day!

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?
  Reply With Quote
04-08-11, 01:23 PM   #6
Ketho
A Pyroguard Emberseer
 
Ketho's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2010
Posts: 1,026
Originally Posted by Vladinator View Post
[...]
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 -.-

Last edited by Ketho : 04-08-11 at 01:28 PM.
  Reply With Quote
04-08-11, 01:41 PM   #7
Akkorian
A Flamescale Wyrmkin
 
Akkorian's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 111
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.
__________________
“Be humble, for you are made of earth. Be noble, for you are made of stars.”

Last edited by Akkorian : 04-08-11 at 01:43 PM.
  Reply With Quote
04-08-11, 05:14 PM   #8
Lanodar
A Murloc Raider
AddOn Author - Click to view addons
Join Date: Jan 2011
Posts: 6
I tried all day and i've finally found a solution
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.

Last edited by Lanodar : 04-08-11 at 05:29 PM.
  Reply With Quote
04-12-11, 01:08 AM   #9
Akkorian
A Flamescale Wyrmkin
 
Akkorian's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2010
Posts: 111
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.
__________________
“Be humble, for you are made of earth. Be noble, for you are made of stars.”
  Reply With Quote
04-12-11, 12:40 PM   #10
Xinhuan
A Chromatic Dragonspawn
 
Xinhuan's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2007
Posts: 174
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.
__________________
Author of Postal, Omen3, GemHelper, BankItems, WoWEquip, GatherMate, GatherMate2, Routes and Cartographer_Routes
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Respond to Chat commands


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off