PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Finished Scripts => Topic started by: Tw?sT?d-d?v on 21 October, 2007, 22:06:48

Title: Word_Replacer_[New_API]
Post by: Tw?sT?d-d?v on 21 October, 2007, 22:06:48
Plops Word Replacer      :)
Title: Re: Word_Replacer_[New_API]
Post by: Jusper on 29 January, 2008, 10:38:03
it doesn't work with cyrillic characters
Title: Re: Word_Replacer_[New_API]
Post by: Crillle on 21 November, 2008, 21:19:29
Hi!

I get this problem:  Syntax Word Replacer [New API].lua:292: attempt to index local 'fFile' (a nil value)


Regards

Crille
Title: Re: Word_Replacer_[New_API]
Post by: ATAG on 21 November, 2008, 22:51:41
Quote from: Crillle on 21 November, 2008, 21:19:29
Hi!

I get this problem:  Syntax Word Replacer [New API].lua:292: attempt to index local 'fFile' (a nil value)


Regards

Crille

Create the "scripts\error" dir manually and it should work :)
Title: Re: Word_Replacer_[New_API]
Post by: ?StIfFLEr?? on 22 November, 2008, 07:45:19
[12:13:51] <?GiGaByTe?> Word Replacer [New API].lua:282: attempt to concatenate global 'sNick' (a nil value)
getting this error
Title: Re: Word_Replacer_[New_API]
Post by: ATAG on 22 November, 2008, 10:36:59
Now it works.
Title: Re: Word_Replacer_[New_API]
Post by: ?StIfFLEr?? on 22 November, 2008, 15:39:12
thanks buddy its working [applaud]
Title: Re: Word_Replacer_[New_API]
Post by: Powerpuffan on 27 November, 2008, 14:58:06
Hello!

It seem?s that wordreplacer is not working properly as it should.

It is not always the words who has been replaced with other words appears.

We have for example, a user as when she writes in main she can see that the substituted words appears, but when she writes in pm the words are not replaced.

When other users write their words in pm it?s been replaced.

What?s the difference?

If we have for example ?gl? that?s been replaced with ?Good Luck? then it doesn?t  always do so.

In Sweden, we have letters like ? ? and ?.

When we for example write the word ?gl?gg? in main it will be ?good luck?gg?, but if I?ll write ?glad? it will be ?glad? instead of ?Good Luckad?.

Why doesn?t it replaces the letters ?gl? with good luck? Gl reacts only when we spell words with ? ? and ?  in the original word.

Another thing is that if two people are talking in pm the words will not look the same of both users.

Here you have an example of what I mean:

Here is the pm of user Doc and looks like this:
[27:nov]  [14:20:28] <Power?> good luck
[27:nov]  [14:20:30] <Power?> gl?gg
[27:nov]  [14:20:31] <Power?> glad
[27:nov]  [14:20:32] <Power?> nighty night
[27:nov]  [14:20:38] <Doc> glad
[27:nov]  [14:20:39] <Doc> gl?gg
[27:nov]  [14:20:41] <Doc> nn

Here is the same pm but from user Power?, and it looks like this:
[27:nov]  [14:20:28] <Power?> gl
[27:nov]  [14:20:30] <Power?> gl?gg
[27:nov]  [14:20:31] <Power?> glad
[27:nov]  [14:20:32] <Power?> nn
[27:nov]  [14:20:38] <Doc> glad
[27:nov]  [14:20:39] <Doc> gl?gg
[27:nov]  [14:20:42] <Doc> nighty night

I truly hope that you understand what I mean. It?s difficult to translate it from Swedish to English.
Title: Re: Word_Replacer_[New_API]
Post by: ATAG on 27 November, 2008, 17:49:18
? is also in hungarian.. but i didn't try with non-english characters.. you have to use os.setlocale("your locale") in the script, but i don't know what is the swedish locale in windows :S

On linux i use os.setlocale("hu_HU.ISO-8859-2") and it works like a charm :)

PS: i try to make an alternative "string.lower" to fix the problem :)


Here it is. Change "strings" table for your language :)

-- word replacer script.
-- includes !me in pm.
-- all commands work in any pm window.
-- for PtokaX 0.3.3.0 16.06 and later.
-- version 1.2 public.
-- by plop
-- RC added by TiMeTrAVelleR
-- Convert to lua 5.11 by TiMeTrAVelleR
--// Converted to NEW API by Tw?sTčd-dčv?l (21/10/07)
--//Removed robocop profiles by Tw?sTčd-dčv?l (21/10/07)
-- fixed by ATAG @ 2008/11/22

FilterPm = 1 -- filter what? use 1 to enable, nil to disable the filter.
FilterMain = 1 -- filter what? use 1 to enable, nil to disable the filter.


iHistory = 50 -- maximum lines stored in the chat history.


sBot = "Word Replacer" -- name of the bot.
UserCommands = 1 -- Send usercommands on connect 1=on/0=off
tMenu = "Word Replacer" -- Usercommands menu name

tLevels = {
[-1] = 0,       --Unregistered User
[0] = 1, -- Master
[1] = 0, -- Operator
[2] = 0, -- VIPs
[3] = 0, -- Regs
}

local strings = {
["?"]="?",["?"]="?",["?"]="?",["?"]="?",["Ő"]="ő",["?"]="?",["Ű"]="ű",["?"]="?"
}

local slower = ""
for s in pairs(strings) do
slower = slower..s
end

local string_lower = function(s)
s = s:gsub("["..slower.."]", function(k) return strings[k] or k end)
return s
end

--regbot in the user list.
iRegBot = 0

UserConnected = function(user)
Core.GetUserAllData(user)
if UserCommands == 1 and tLevels[user.iProfile] == 1 then
Core.SendToUser(user,"$UserCommand 0 3|"..
"$UserCommand 1 3 "..tMenu.."\\WordReplacer Help$<%[mynick]> !wordreplacer&#124;|"..
"$UserCommand 1 3 "..tMenu.."\\Shows All Words Which Are Replaced And For What.$<%[mynick]> !showwords&#124;|"..
"$UserCommand 1 3 "..tMenu.."\\Add Word To Replace$<%[mynick]> !addword %[line:OldWord] %[line:NewWord]&#124;|"..
"$UserCommand 1 3 "..tMenu.."\\Delete Word$<%[mynick]> !delword %[line:Word]&#124;|"..
"$UserCommand 1 3 "..tMenu.."\\Chat History$<%[mynick]> !history&#124;|")
end
end

OpConnected = UserConnected
RegConnected = UserConnected

-- prefixes table, allowed for me.
-- also used 2 bypase commands from other scripts.
tPre = {
   ["+"] = 1,
   ["!"] = 1,
   ["/"] = 1,
   ["-"] = 1
}

tHistory = {}

tCommands = {

   ["showwords"] = {
fCom = function(user, data)
   local line = "\r\n\r\n\tOld\t\t\tNew\r\n"
line = line.."\t------------------------------------------------------------------------\r\n"
   for a,b in pairs(tBadWords) do
line = line.."\t"..a.."\t\t\t"..b.."\r\n"
   end
   line = line.."\r\n\tFunctions\r\n"
   for a,b in pairs(tBadFuncs) do
line = line.."\t"..a.."\r\n"
   end
   return line
end,
sHelp = "\t\t- shows all words which are replaced and for what.",
tLevels = {
   [-1] = 0,       --Unregistered User
           [0] = 1, -- Master
           [1] = 0, -- Operator
           [2] = 0, -- VIPs
           [3] = 0, -- Regs
},
   },

   ["history"] = {
fCom = function(user, data)
   local sText = "\r\n\r\n"
   for i=1,#tHistory do
sText = sText..tHistory[i].."\r\n"
   end
   return sText
end,
sHelp = "\t\t\t- shows the chat history.",
tLevels = {
   [-1] = 0,       --Unregistered User
           [0] = 1, -- Master
           [1] = 1, -- Operator
           [2] = 0, -- VIPs
           [3] = 0, -- Regs
},
   },

   ["addword"] = {
fCom = function(user,data)
   local s,e, word, replaced = data:find("%b<>%s*%S+%s*(%S*)%s*(.*)")
   if word == "" then
return "No word??"
   elseif replaced == "" then
return "replace "..word.." for what??"
   elseif word:find("plop") then
Core.Disconnect(user)
   else
word = Format(word)
replaced = Format(replaced)
tBadWords[word] = replaced
Save(tBadWords, "tBadWords")
return word.." Will Be Replaced For "..replaced
   end
end,
sHelp = "<word> <replaced>\t- adds the word and replaces it for replaced.",
tLevels = {
   [-1] = 0,       --Unregistered User
           [0] = 1, -- Master
           [1] = 1, -- Operator
           [2] = 0, -- VIPs
           [3] = 0, -- Regs
},
   },

   ["delword"] = {
fCom = function(user,data)
   local s,e, word = data:find("%b<>%s*%S+%s*(%S*)")
   if word == "" then
return "No word??"
   elseif not tBadWords[word] then
return word.." didn't excist in the word replacer"
   else
tBadWords[word] = nil
Save(tBadWords, "tBadWords")
return word.." Removed From The Word Replacer"
   end
   return 1
end,
sHelp = "<word>\t\t- removes the word from the replacer.",
tLevels = {
   [-1] = 0,       --Unregistered User
           [0] = 1, -- Master
           [1] = 1, -- Operator
           [2] = 0, -- VIPs
           [3] = 0, -- Regs
},
   },

   ["wordreplacer"] = {
fCom = function(user,data)
   local sText = "\r\n\r\n"
   local iProf = user.iProfile
   for a,b in pairs(tCommands) do
if b.tLevels[iProf] then
   sText = sText.."\t!"..a.."\t"..b.sHelp.."\r\n"
end
   end
   return sText
end,
sHelp = "\t\t- this help menu.",
tLevels = {
   [-1] = 0,       --Unregistered User
           [0] = 1, -- Master
           [1] = 1, -- Operator
           [2] = 0, -- VIPs
           [3] = 0, -- Regs
},
   },
}

function OnStartup()
   math.randomseed(math.floor(os.time()/10000))
   path = Core.GetPtokaXPath().."scripts/"
   dofile(path.."wordreplace/tBadWords.lua")
   dofile(path.."wordreplace/tBadFunctions.lua")
   local fFile = io.open(path.."wordreplace/tHistory.lua")
   if fFile then
dofile(path.."wordreplace/tHistory.lua")
fFile:close()
   end
   if iRegBot then
Core.RegBot(sBot,"friendly wordreplacer<[URL]www.plop.nl[/URL]>","lua.plop.nl",true)
   end
end

function CheckWord(word)
   local wordl = string_lower(word)
   if tBadWords[wordl] then
      return tBadWords[wordl]
   elseif tBadFuncs[wordl] then
      return tBadFuncs[wordl]()
   elseif tonumber(word) or word:find("%W") then
      return word
   else
      return word
   end
end

function addHistory(text)
   if #tHistory > iHistory then
      table.remove(tHistory, 1)
   end
   table.insert(tHistory, text)
end

function ChatArrival(user,data)
   local data = data:sub(1, -2)
   local s,e,cmd = data:find("%b<>%s+%p(%S+)")
   if cmd and tCommands[cmd] then
if tCommands[cmd].tLevels[user.iProfile] then
   Core.SendToUser(user,"<"..sBot.."> ".. tCommands[cmd].fCom(unpack({user, data})) )
   return true
else
   Core.SendToUser(user,"<"..sBot.."> no acces!!")
   return true
end
   end
   if FilterMain then
local s,e,data =data:find("%b<>%s+(.*)")
local data,c = data:gsub("(%S+)", function(word) return CheckWord(word) end)
local s,e,pre, cmd = data:find("^%s*(%W)(%w+)")
if pre and cmd and tPre[pre] then
   if cmd == "me" then
local sText = "* "..user.sNick.." "..data:sub(5, string.len(data))
Core.SendToAll(sText)
addHistory("["..os.date("%X").."] "..sText)
return true
   else
return false
   end
else
   local sText = "<"..user.sNick.."> "..data
   Core.SendToAll(sText)
   addHistory("["..os.date("%X").."] "..sText)
   return true
end
   end
end


function ToArrival(user,data)
   local data = data:sub(1, -2)
   local s,e,cmd = data:find("%$%b<>%s+%p(%S+)")
   local s,e,whoto = data:find("%S+%s*(%S+)")
   local usr = Core.GetUser(whoto)
   if cmd and tCommands[cmd] then
if tCommands[cmd].tLevels[user.iProfile] then
   if usr then
Core.SendToUser(user,"$To: "..user.sNick.." From: "..usr.sNick.." $<"..sBot.."> "..tCommands[cmd].fCom(unpack({user, data})).."|")
   else
Core.SendPmToUser(user,sBot, tCommands[cmd].fCom(unpack({user, data})) )
   end
   return true
else
   Core.SendPmToUser(user,sBot, "no acces!!")
   return true
end
   end
   if FilterPm then
if usr then
   local sNick = user.sNick
   local s,e,data = data:find("$%b<>%s*(.*)$")
   local data,c = data:gsub("(%S+)", function(word) return CheckWord(word) end)
   local s,e,pre, cmd = data:find("^%s*(%W)(%S+)")
   if pre and tPre[pre] then
if cmd == "me" then
   data = data:sub(4)
   Core.SendToUser(usr,"$To: "..usr.sNick.." From: "..sNick.." $* "..sNick.." "..data.."|")
   return true
end
return false
   else
Core.SendToUser(usr,"$To: "..usr.sNick.." From: "..sNick.." $<"..sNick.."> "..data.."|")
return true
   end
end
   end
end

function OnError(ErrorMsg)
   Core.SendToOps("<"..sBot.."> ".. ErrorMsg)
   local fFile = io.open(path.."error/wordreplacer.lst", "a+")
   fFile:write(ErrorMsg.."\n")
   fFile:close()
end

function Save(tTable, name)
   local fFile = io.open(path.."wordreplace/"..name..".lua", "w+")
   fFile:write(name.."={")
   for a,b in pairs(tTable) do
fFile:write("\t["..string.format("%q", a).."] = "..string.format("%q", b)..",\n")
   end
   fFile:write("\n}\n\n--plopyrights reserved")
   fFile:close()
   return "Done"
end

function OnExit()
   local fFile = io.open(path.."wordreplace/tHistory.lua", "w+")
   fFile:write("tHistory={\n")
   for i=1,#tHistory do
fFile:write("\t"..string.format("%q", tHistory[i])..",\n")
   end
   fFile:write("\n}\n\n--plopyrights reserved")
   fFile:close()
end

function Format(sText)
   local sText = string.format("%q", sText)
   sText , iSlash = string.gsub(sText, "\\", "\r\n")
   sText = string.sub(sText, 2, -2)
   return sText
end