WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   Lua/XML Help (https://www.wowinterface.com/forums/forumdisplay.php?f=16)
-   -   How to trigger events by chat? (https://www.wowinterface.com/forums/showthread.php?t=42430)

kitwinz 01-23-12 01:08 AM

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?

Xrystal 01-23-12 01:24 AM

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.

kitwinz 01-23-12 11:33 PM

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?

SDPhantom 01-24-12 05:13 AM

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);

kitwinz 01-24-12 07:26 PM

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. :D

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);


Phanx 01-25-12 12:47 AM

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.


All times are GMT -6. The time now is 12:58 AM.

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