Thread Tools Display Modes
01-23-12, 01:08 AM   #1
kitwinz
A Defias Bandit
Join Date: Jan 2012
Posts: 3
Question How to trigger events by chat?

I'm trying to learn a bit of lua in my free time and I got a little stuck. I want to make a code that can read what other players say in chatbox in the SAY channel, and if it matches a command, then execute the script for that command.

For example:

player1 says "hello world"

player2 has lua script and when player1 says "hello world", executes a script that makes player2 say "hi"

Sort of like auto responding to certain things.
Can someone please give me a small example of this to go by?
  Reply With Quote
01-23-12, 01:24 AM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,934
You would have to watch events triggering like CHAT_MSG_SAY, CHAT_MSG_WHISPER etc which are listed on http://wowprogramming.com/docs/events and http://www.wowpedia.org/Events/Communication

The latter gives a brief explanation as to how you would go about using them with an xml based addon.
__________________


Characters:
Gwynedda - 70 - Demon Warlock
Galaviel - 65 - Resto Druid
Gamaliel - 61 - Disc Priest
Gwynytha - 60 - Survival Hunter
Lienae - 60 - Resto Shaman
Plus several others below level 60

Info Panel IDs : http://www.wowinterface.com/forums/s...818#post136818
  Reply With Quote
01-23-12, 11:33 PM   #3
kitwinz
A Defias Bandit
Join Date: Jan 2012
Posts: 3
idk..

Can you give me a small example of how to use them? o_O I'm still not getting it. I just need to figure out how to like.. make it read certain things.. like..

if msg = x then execute y
if msg does NOT = x then do nothing

how would i do that?
  Reply With Quote
01-24-12, 05:13 AM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
lua Code:
  1. local frame=CreateFrame("Frame");-- Need a frame to capture events
  2. frame:RegisterEvent("CHAT_MSG_SAY");-- Register our event
  3. frame:SetScript("OnEvent",function(self,event,msg)-- OnEvent handler receives event triggers
  4.     if event=="CHAT_MSG_SAY" and msg=="hello world" then-- Someone sends "hello World" in /say
  5.         SendChatMessage("hi","SAY");-- Send "hi" back through /say
  6.     end
  7. end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
01-24-12, 07:26 PM   #5
kitwinz
A Defias Bandit
Join Date: Jan 2012
Posts: 3
Smile

omg thanks! works like a charm! There's only one small issue.

How would I modify that code so that when I say "hello world" it does nothing, but when other people say "hello world" it executes the script? Also, when it executes, it makes me say "hi" twice. That's the only issue I'm having now. I really appreciate all the help.

I think it would be something like this, but i can't seem to find the variable to define yourself:

Code:
local frame=CreateFrame("frame");
frame:RegisterEvent("CHAT_MSG_SAY");
frame:SetScript("OnEvent",function(self,event,msg,author)
    if event=="CHAT_MSG_SAY" and msg=="hello world" and author~= then
        SendChatMessage("hi","SAY");
    end
end);

Last edited by kitwinz : 01-24-12 at 09:02 PM.
  Reply With Quote
01-25-12, 12:47 AM   #6
Phanx
Cat.
 
Phanx's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2006
Posts: 5,617
UnitName(unitToken) is what you're looking for:

Code:
local myName = UnitName("player")

local frame = CreateFrame("Frame")
frame:RegisterEvent("CHAT_MSG_SAY")
frame:SetScript("OnEvent", function(self, event, message, sender)
	if event == "CHAT_MSG_SAY" and sender ~= myName and message:lower():match("hello world") then
		SendChatMessage("hi", "SAY")
	end
end)
Also, I improved the message check a bit by first converting the message to all lowercase (so it will catch "Hello world" and "hello world") and then only checking to see if it contains "hello world" instead of being an exact match (so it will catch "hello world" and "hello world!"). Feel free to revert this if you want to only catch exact matches.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » How to trigger events by chat?


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