Mikk38024@legacy41558003 (talk | contribs) (→Example filter implementation: Mention that globals are still accessible in 3.0) |
Mikk38024@legacy41558003 (talk | contribs) (→filterFunc: Note changes regarding the global arg1 from 2.4 to 3.0.) |
||
Line 31: | Line 31: | ||
If your function returns true, the message is discarded. |
If your function returns true, the message is discarded. |
||
− | If your function returns the second parameter as non-false/nil, the current message text |
+ | If your function returns the second parameter as non-false/nil, the current message text is replaced with your return. In 2.4, the global arg1 was replaced with the changed message text. In 3.0, it is not. |
− | |||
== Example filter implementation == |
== Example filter implementation == |
Revision as of 01:01, 17 October 2008
- This function is implemented in
.:AddMessageEventFilter FrameXML/ChatFrame.lua
- This function is implemented in
Manipulates FrameXML's list of chat event filters.
ChatFrame_AddMessageEventFilter("event", filterFunc) ChatFrame_RemoveMessageEventFilter("event", filterFunc) filterFuncList = ChatFrame_GetMessageEventFilters("event")
Arguments
- ("event", filterFunc)
- "event"
- String - name of the event to filter for
- filterFunc
- Function - your filtering function; see below for details
Returns
- filterFuncList
- Table - a (currently integer-indexed) list of filter functions
filterFunc
filter[, "optionalNewMsg"] = myFilterFunc(msg)
The passed-in message is the same as arg1. Other parameters of the event are available through the standard globals "event", "arg2", "arg3", etc.
If your function returns true, the message is discarded.
If your function returns the second parameter as non-false/nil, the current message text is replaced with your return. In 2.4, the global arg1 was replaced with the changed message text. In 3.0, it is not.
Example filter implementation
Writing a filter is fairly straightforward:
local myChatFilter(msg) if strfind(msg, "buy gold") then return true end if arg2=="Knownspammer" then return true end if strfind(msg, "lol") then return false, gsub(msg, "lol", "") end end ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", myChatFilter) ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", myChatFilter) ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", myChatFilter) ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", myChatFilters)
You should use the passed-in "msg" parameter rather than accessing the global arg1. As for the other values, there really is no other way than to access the globals, unfortunately. (This still holds true in 3.0. The globals were not removed.)
Example Chatframe addon use
Chatframe addons / whisper managers etc should of course make use of these filters.
local myChatEventHandler(self,event,arg1,...) local filterFuncList = ChatFrame_GetMessageEventFilters(event) if filterFuncList then for _, filterFunc in pairs(filterFuncList) do local filter, newarg1 = filterFunc(arg1) if filter then return end arg1 = (newarg1 or arg1); end end -- whoop de do, not filtered, go about our business and display etc end
Details
This set of functions was added in patch 2.4
Re-adding an already-existing filter is harmless - the second addition is ignored.
Removing a filter that has not been added is harmless - there is no error.