First, you need to figure out which global string gets used. When filtering system messages, you should always use the relevant global string, so it works in all languages and automatically works if/when Blizzard changes the string. In this case, it's ROLE_CHANGED_INFORM_WITH_SOURCE, which in its raw state looks like
"%s is now %s. (Changed by %s.)" in English,
"%s ist nun %s. (Von %s geändert.)" in German, etc.
Second, you need to use some
gsub magic to turn that string into a pattern you can use with
string.find. Here's the function I use that will turn any Blizzard global string into a search pattern:
Code:
local function topattern(str)
str = gsub(str, "%%%d?$?c", ".+")
str = gsub(str, "%%%d?$?s", ".+")
str = gsub(str, "%%%d?$?d", "%%d+")
str = gsub(str, "([%(%)])", "%%%1")
return str
end
Notes:
(a) If you're only working with a few strings, you may be tempted to just
gsub them directly, but then if Blizzard changes them (for example, changes %s tokens to %1$s tokens, or adds a token) you have to update. The above function will automatically handle any such changes, unless Blizzard adds some new type of tokens.
(b) The above function doesn't add captures, so it's mainly for use with
string.find -- is this a "X defated Y in a duel" message? -- and not with
string.match -- what are the values of X and Y in this message?.
Back on topic -- to hide the messages:
Code:
local pattern = topattern(ROLE_CHANGED_INFORM_WITH_SOURCE)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", function(frame, event, message, ...)
return strfind(message, pattern)
end)
If you have a lot of messages to block (for example, if you wanted to block all the messages about how drunk other people are) I'd suggest putting them in an indexed table (faster than a hash table because you don't need a function call to iterate over it) and looping over it like so:
Code:
local patterns = {
topattern(DRUNK_MESSAGE_ITEM_OTHER1),
topattern(DRUNK_MESSAGE_ITEM_OTHER2),
topattern(DRUNK_MESSAGE_ITEM_OTHER3),
topattern(DRUNK_MESSAGE_ITEM_OTHER4),
topattern(DRUNK_MESSAGE_OTHER1),
topattern(DRUNK_MESSAGE_OTHER2),
topattern(DRUNK_MESSAGE_OTHER3),
topattern(DRUNK_MESSAGE_OTHER4),
}
ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", function(frame, event, message, ...)
for i = 1, #patterns do
if strfind(message, patterns[i]) then
return true
end
end
end)