Thread Tools Display Modes
02-24-10, 10:39 AM   #1
bowedyapper
Guest
Posts: n/a
local function Filter, help? - SOLVED

I seem to be having trouble creating a chat filter addon, i have tried various things before i managed to see what the problem is, when using

Code:
local function Filter(self, event, msg)
  if msg:find("arg1") then
    return true
end
end

ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", Filter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", Filter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", Filter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", Filter)
this will only filter out "arg1" when typed into any channel, say, tell and yell, what i am trying to do is link this to various strings in another part of the LUA script, so it will filter out anything in that list of strings, but i have no idea how to make the "if msg:find("arg1") then" part of the script refer to the arg1 in the previous part of the script.

Any ideas?

Thanks

~Bowedyapper

Last edited by bowedyapper : 02-24-10 at 04:29 PM.
  Reply With Quote
02-24-10, 10:46 AM   #2
Hati-EK
A Fallenroot Satyr
AddOn Author - Click to view addons
Join Date: Jul 2008
Posts: 20
Originally Posted by bowedyapper View Post
I seem to be having trouble creating a chat filter addon, i have tried various things before i managed to see what the problem is, when using

Code:
local function Filter(self, event, msg)
  if msg:find("arg1") then
    return true
end
end

ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", Filter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", Filter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", Filter)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", Filter)
this will only filter out "arg1" when typed into any channel, say, tell and yell, what i am trying to do is link this to various strings in another part of the LUA script, so it will filter out anything in that list of strings, but i have no idea how to make the "if msg:find("arg1") then" part of the script refer to the arg1 in the previous part of the script.

Any ideas?

Thanks

~Bowedyapper
dunno if i got you right -
but "arg1" is a string not a variable
to filter 'something' else you would need a variable ie.

Code:
local toFilter = 'something'
local function Filter(self, event, msg)
  if msg:find(toFilter) then
    return true
end
end
...
  Reply With Quote
02-24-10, 11:06 AM   #3
bowedyapper
Guest
Posts: n/a
even if i add the variable "local toFilter = 'something'" and i change the something to something else this is still a string i think e.g "local toFilter = 'buy gold'" this will still block whatever is put there, maybe i did not explain what it was im trying to do very clearly, all i need is a way to link the filter to filter out all the words etc... in another part of the code. In the other section of code the arg1 string does link to all of the words in that list for example

Code:
function myFilterAddon_OnLoad(this)
   this:RegisterEvent("CHAT_MSG_CHANNEL");
   this:RegisterEvent("CHAT_MSG_SAY");
   this:RegisterEvent("CHAT_MSG_WHISPER");
   this:RegisterEvent("CHAT_MSG_YELL");
end;

function myFilterAddon_OnEvent(this, event)
   if event == "CHAT_MSG_CHANNEL"  or event == "CHAT_MSG_SAY" or event == "CHAT_MSG_YELL" or event == "CHAT_MSG_WHISPER" then
   s = arg1; if(string.find( s, "A Filtered Word") or string.find( s, "Another Filtered Word") ) then arg1 = DEFAULT_CHAT_FRAME:AddMessage("Word Blocked",255,0,0);
so as you can see the first section of my code watches and will display a message when a word or sentence in that list is detected, all i need is to link a filter to that so the detected words/sentences do not show

Hopefully i made more sense that time

P.S thanks for the speedy reply

Last edited by bowedyapper : 02-24-10 at 11:30 AM.
  Reply With Quote
02-24-10, 11:47 AM   #4
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
I use something like this, not sure how much performance impact is has though.

Wrote censored instead of the domain names, hope you don't mind :>

This way also accepts regex.

Code:
local WordBlacklist = {
     "www.censored.c.m", -- use @ in .com sometimes
     "www.censored.com",
     "censored.com",
     "www..censored.com", -- ecensored, kcensored
     "c e n s o r e d , c o m",
     "wwwcensored.com",
}
    
local ChatIgnore = function(self, event, msg, ...)
    for _,v in pairs(WordBlacklist) do
        if string.find(string.lower(msg), v) then
            return true -- Block
--[[-- Pretty sure you really need this if you want to replace text but I only block so it still returns original text without.
        else
            return false, msg, ... -- Don't block, return original without any change
]]--
        end
    end
end

ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", ChatIgnore)
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.
  Reply With Quote
02-24-10, 12:19 PM   #5
bowedyapper
Guest
Posts: n/a
so there is no way i can link those two functions together? or should i just use something similar to your's v6o?
  Reply With Quote
02-24-10, 12:39 PM   #6
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
"ChatFrame_****MessageEventFilter" is really useful if you want to filter your chat. Reason being more than one addon add single or multiple filters or even remove them and no dirty hooking needs to be done.

If you're not going to filter your chat then by all means use the regular events.
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.
  Reply With Quote
02-24-10, 01:44 PM   #7
bowedyapper
Guest
Posts: n/a
ahh wait, breakthrough, just discovered a way that it works, although its not fully operational.

i typed Hi under say, and then typed one of the words to be filtered and it blocked it without it showing, but if i try to type again it does not show anything, here's the code

Code:
function filter_OnLoad(this)
this:RegisterEvent("CHAT_MSG_CHANNEL");
this:RegisterEvent("CHAT_MSG_SAY");
this:RegisterEvent("CHAT_MSG_WHISPER");
this:RegisterEvent("CHAT_MSG_YELL");
end; 

function filter_OnEvent(this, event)
if event == "CHAT_MSG_CHANNEL"  or event == "CHAT_MSG_SAY" or event == "CHAT_MSG_YELL" or event == "CHAT_MSG_WHISPER" then 
s = arg1; if(string.find( s, "word to be filtered") or string.find( s, "another word to be filtered") ) then arg1= DEFAULT_CHAT_FRAME:AddMessage("Message to notify user that spam has been blocked",255,0,0);

local function myChatFilter(self, event, msg)
  if msg:find(arg1) then
    return true
	else
    return false
  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", myChatFilter)


end;
so all i did was move
Code:
local function myChatFilter(self, event, msg)
  if msg:find(arg1) then
    return true
	else
    return false
  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", myChatFilter)
in with the other code before the "end" and now it kind of works, all i need to do is figure out what is happening now lol.

so basically by moving that to where it is now, the function now knows that arg1 means the list above "string.find"

I did also try your method v6o, but it is not suitable for displaying messages, even though your method uses less resources than mine lol

Last edited by bowedyapper : 02-24-10 at 01:50 PM.
  Reply With Quote
02-24-10, 03:12 PM   #8
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
Before you go any further, could you explain in detail in what you're trying to do.

I've reread your post and you're trying to do is basically a spam filter right?
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.
  Reply With Quote
02-24-10, 03:14 PM   #9
bowedyapper
Guest
Posts: n/a
no, not a curse filter since this is available in WoW itself, i am actually trying to construct a spam filter so as to block the cursed spam from gold sellers

EDIT -- just noticed that you said spam, not curse :S, anyways, yes i am trying to create a spam filter, and the code that i currently have, works, just the filter loops now, so any text sent after the filter works its magic, no text will be shown

Last edited by bowedyapper : 02-24-10 at 03:37 PM.
  Reply With Quote
02-24-10, 03:39 PM   #10
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
I'm gonna post two examples yet again to show you how my above code works.

Basically, WordBlacklist contains a collection of strings you want to block. If any are found in the message, it's blocked.


1. This one just replaces the message in the chat with [Blocked Message]
Code:
local WordBlacklist = {
     "blockthiscrap",
     "i dont want to see this!!",
}
    
local ChatIgnore = function(self, event, msg, author, ...)
    for _,v in pairs(WordBlacklist) do
        if string.find(string.lower(msg), v) then
            return false, "[Blocked Message]", author, ...
        end
    end
end

ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", ChatIgnore)

2. This hides the message completely and adds what looks like a system message in the chat window it appeared in
Code:
local WordBlacklist = {
     "blockthiscrap",
     "i dont want to see this!!",
}
    
local ChatIgnore = function(self, event, msg, author, ...)
    for _,v in pairs(WordBlacklist) do
        if string.find(string.lower(msg), v) then
            self:AddMessage("Blocked Message from "..author,  1,1,0)
            return true
        end
    end
end

ChatFrame_AddMessageEventFilter("CHAT_MSG_CHANNEL", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SAY", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_YELL", ChatIgnore)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", ChatIgnore)
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.
  Reply With Quote
02-24-10, 03:45 PM   #11
bowedyapper
Guest
Posts: n/a
Ah ok, i see now, the method i was using is long and probably will end up using way to much rescources, your method, clean and simple, probably does not help that i am new to the whole LUA scene, was just becoming really fed up with the spam from gold sellers and people faking to be Blizzard Employees

Anyways will test your examples and see, thanks

EDIT -- Very nice, it's actually better than i imagined mine would be, i particually like the second example as it displays who the message is from, only change i made is one of the strings to test and colour to red (255,0,0), also will it greatly impact preformance if i add lots of strings.

Again thanks for giving me a boot in the backside and pointing me in the right direction :P

Last edited by bowedyapper : 02-24-10 at 03:57 PM.
  Reply With Quote
02-24-10, 03:52 PM   #12
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
I just didn't see the point in printing out a message and just hid it.

Remember it uses regex so . is not the same as a dot.
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.

Last edited by v6o : 02-24-10 at 04:21 PM. Reason: grammar, what's left of it
  Reply With Quote
02-24-10, 04:02 PM   #13
bowedyapper
Guest
Posts: n/a
Yeah, the way i was doing it, it had one to many functions for just a simple addon lol, another thing i was going to ask, is there a way in your second example to change the colour of the Author name, so i can have the text red and the Author name in White?
  Reply With Quote
02-24-10, 04:12 PM   #14
v6o
An Onyxian Warder
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 399
Originally Posted by bowedyapper View Post
Yeah, the way i was doing it, it had one to many functions for just a simple addon lol, another thing i was going to ask, is there a way in your second example to change the colour of the Author name, so i can have the text red and the Author name in White?
Easiest way to color a message is using the "inline coloring" (not sure what it's called) using HEX color codes (look 'em up)

self:AddMessage("Blocked Message from |cffFFFFFF"..author.."|r", 1,0,0)

|r at the end removes colors
__________________
I stopped playing back World of Warcraft in 2010 and I have no plans on returning.
This is a dead account and if you want to continue any of my addons or make a fork then feel free to do so.
This is your permission slip.

If you need to contact me, do so on Twitter @v6ooo

Best regards, v6.
  Reply With Quote
02-24-10, 04:25 PM   #15
bowedyapper
Guest
Posts: n/a
Looks good, ty for the help, will need to study Lua more if i am to create addons for WoW, just going to add a few more things to this one and hopefully i will be able to say Voila :P

Will make sure to add you to the Toc file if i am to upload or distribute

Thanks again
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » local function Filter, help?


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