-- ChatStats v3 Made By Optimus
-- Based on Tezlo chats
-- Added Send Commands By TiMeTrAVelleR
-- Madman fixed some in commands
-- Converted to lua5 by Madman
-- with some help by ?
sBot = "Chatstats" -- Name of Bot
SendComm = 1 -- Send UserCommands 1 = On 0 = Off
pMenu = "-=( ChatStats )=-" -- Name of Menu
Chatstats = {}
Sortstats = 2 -- 1=words / 2=posts
ChatStatsFile = "chatstats.tbl"
EnableChatStats = {
[0] = 1, -- Master
[1] = 1, -- Operators
[2] = 1, -- Vips
[3] = 1, -- Regs
[4] = 1, -- Moderators
[5] = 1, -- NetFounders
[-1] = 1, -- Users (UnRegged)
}
AllowedProfiles = {
[0] = 1, -- Masters
[1] = 0, -- Operators
[4] = 0, -- Moderator
[5] = 0, -- NetFounder
}
function Main()
--frmHub:RegBot(sBot)
loadTableFromFile(ChatStatsFile)
end
function NewUserConnected(user)
if SendComm == 1 and EnableChatStats[user.iProfile] == 1 then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
end
user:SendData("$UserCommand 1 3 "..pMenu.."\\TopChatters$<%[mynick]> !topchatters||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\My Chat Stat$<%[mynick]> !mychatstat||")
if AllowedProfiles[user.iProfile] == 1 then
user:SendData("$UserCommand 1 3 "..pMenu.."\\Del Chatter$<%[mynick]> !delchatter %[line:Nick]||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\Clear Chat Stats$<%[mynick]> !clearchatstats||")
end
end
end
OpConnected = NewUserConnected
function OnExit()
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
function ChatArrival(user, data)
if (string.sub(data, 1, 1) == "<" ) then
if EnableChatStats[user.iProfile] == 1 then
local s,e,str = string.find(data, "^%b<> (.*)%|$")
updStats(user.sName, str)
end
data=string.sub(data,1,string.len(data)-1)
s,e,cmd=string.find(data, "%b<>%s+(%S+)")
if (cmd == "!mychatstat") then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
return 1
else
user:SendData(sBot, "*** No chat statics found!")
return 1
end
elseif (cmd=="!topchatters") then
TCopy={}
if Chatstats then
for i,v in Chatstats do
table.insert(TCopy,{i,v.post,v.chars,v.words})
end
table.sort(TCopy,function(a,b) return (a[Sortstats] > b[Sortstats]) end)
local chat = "Current Top Chatters:\r\n\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
chat = chat.."\t Nr.\tPosts:\tChars:\tWords:\tName:\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
for i = 1,25 do
if TCopy[i] then
-- Nr: Posts: Chars: Words: Name:
chat = chat.."\t "..i..".\t "..TCopy[i][2].."\t "..TCopy[i][3].."\t "..TCopy[i][4].."\t"..TCopy[i][1].."\r\n"
end
end
user:SendData(sBot, chat)
TCopy={}
end
return 1
elseif (cmd == "!delchatter") and AllowedProfiles[user.iProfile] == 1 then
local s,e,cmd,name = string.find( data, "%b<>%s+(%S+)%s+(%S+)" )
if name then
if Chatstats[name] then
Chatstats[name] = nil
user:SendData(sBot, "Chatstats from user "..name.." are now removed!")
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
else
user:SendData(sBot, "*** Chatstats from user "..name.." not found!")
end
else
user:SendData(sBot, "*** Usage: !delchatter ")
end
return 1
elseif (cmd == "!clearchatstats") and AllowedProfiles[user.iProfile] == 1 then
Chatstats = {}
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
user:SendData(sBot, "Chatstats are cleared by "..user.sName)
return 1
end
end
end
function updStats(nick, str)
local tmp = Chatstats[nick] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x")}
tmp["post"], tmp["chars"], tmp["words"], tmp["time"] = tmp["post"]+1, tmp["chars"]+string.len(str), tmp["words"]+cntargs(str,"(%a+)"), os.date("%x")
Chatstats[nick] = tmp
-- saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
function cntargs(str, rule)
local s,n = string.gsub(str, rule, "")
return n
end
----------------------------------------------
-- load & save Tables
----------------------------------------------
function Serialize(tTable, sTableName, hFile)
write(hFile,sTableName.." = {\n")
for key,value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key)
if(type(value) == "table") then
Serialize(value,sKey,hFile)
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value)
write(hFile,sKey.." = "..sValue)
end write(hFile,",\n")
end write(hFile,"}")
end
-----------------------------------------------------------
function saveTableToFile(file, table, tablename)
local handle = openfile(file,"w")
Serialize(table,tablename,handle)
write(handle,"\n") closefile(handle)
end
-----------------------------------------------------------
function loadTableFromFile(file)
readfrom(file)
dostring(read("*all"))
readfrom()
end
need some help converting this to lua 5, i done the most, but i need help with the tabels
can someone please help? i only got this script left to convert... then i can change to new PX, dont want to change untill i got all my script in lua 5 =)
Try this ( i just changed the IO stuff using http://board.univ-angers.fr/thread.php?threadid=3672&boardid=30&styleid=1&sid=72ac05990b9fac0eede696f844d29fa3 )
-- ChatStats v3 Made By Optimus
-- Based on Tezlo chats
-- Added Send Commands By TiMeTrAVelleR
-- Madman fixed some in commands
-- Converted to lua5 by Madman
-- with some help by ?
sBot = "Chatstats" -- Name of Bot
SendComm = 1 -- Send UserCommands 1 = On 0 = Off
pMenu = "-=( ChatStats )=-" -- Name of Menu
Chatstats = {}
Sortstats = 2 -- 1=words / 2=posts
ChatStatsFile = "chatstats.tbl"
EnableChatStats = {
[0] = 1, -- Master
[1] = 1, -- Operators
[2] = 1, -- Vips
[3] = 1, -- Regs
[4] = 1, -- Moderators
[5] = 1, -- NetFounders
[-1] = 1, -- Users (UnRegged)
}
AllowedProfiles = {
[0] = 1, -- Masters
[1] = 0, -- Operators
[4] = 0, -- Moderator
[5] = 0, -- NetFounder
}
function Main()
--frmHub:RegBot(sBot)
loadTableFromFile(ChatStatsFile)
end
function NewUserConnected(user)
if SendComm == 1 and EnableChatStats[user.iProfile] == 1 then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
end
user:SendData("$UserCommand 1 3 "..pMenu.."\\TopChatters$<%[mynick]> !topchatters||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\My Chat Stat$<%[mynick]> !mychatstat||")
if AllowedProfiles[user.iProfile] == 1 then
user:SendData("$UserCommand 1 3 "..pMenu.."\\Del Chatter$<%[mynick]> !delchatter %[line:Nick]||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\Clear Chat Stats$<%[mynick]> !clearchatstats||")
end
end
end
OpConnected = NewUserConnected
function OnExit()
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
function ChatArrival(user, data)
if (string.sub(data, 1, 1) == "<" ) then
if EnableChatStats[user.iProfile] == 1 then
local s,e,str = string.find(data, "^%b<> (.*)%|$")
updStats(user.sName, str)
end
data=string.sub(data,1,string.len(data)-1)
s,e,cmd=string.find(data, "%b<>%s+(%S+)")
if (cmd == "!mychatstat") then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
return 1
else
user:SendData(sBot, "*** No chat statics found!")
return 1
end
elseif (cmd=="!topchatters") then
TCopy={}
if Chatstats then
for i,v in Chatstats do
table.insert(TCopy,{i,v.post,v.chars,v.words})
end
table.sort(TCopy,function(a,b) return (a[Sortstats] > b[Sortstats]) end)
local chat = "Current Top Chatters:\r\n\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
chat = chat.."\t Nr.\tPosts:\tChars:\tWords:\tName:\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
for i = 1,25 do
if TCopy[i] then
-- Nr: Posts: Chars: Words: Name:
chat = chat.."\t "..i..".\t "..TCopy[i][2].."\t "..TCopy[i][3].."\t "..TCopy[i][4].."\t"..TCopy[i][1].."\r\n"
end
end
user:SendData(sBot, chat)
TCopy={}
end
return 1
elseif (cmd == "!delchatter") and AllowedProfiles[user.iProfile] == 1 then
local s,e,cmd,name = string.find( data, "%b<>%s+(%S+)%s+(%S+)" )
if name then
if Chatstats[name] then
Chatstats[name] = nil
user:SendData(sBot, "Chatstats from user "..name.." are now removed!")
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
else
user:SendData(sBot, "*** Chatstats from user "..name.." not found!")
end
else
user:SendData(sBot, "*** Usage: !delchatter ")
end
return 1
elseif (cmd == "!clearchatstats") and AllowedProfiles[user.iProfile] == 1 then
Chatstats = {}
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
user:SendData(sBot, "Chatstats are cleared by "..user.sName)
return 1
end
end
end
function updStats(nick, str)
local tmp = Chatstats[nick] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x")}
tmp["post"], tmp["chars"], tmp["words"], tmp["time"] = tmp["post"]+1, tmp["chars"]+string.len(str), tmp["words"]+cntargs(str,"(%a+)"), os.date("%x")
Chatstats[nick] = tmp
-- saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
function cntargs(str, rule)
local s,n = string.gsub(str, rule, "")
return n
end
----------------------------------------------
-- load & save Tables
----------------------------------------------
function Serialize(tTable, sTableName, hFile)
write(hFile,sTableName.." = {\n")
for key,value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key)
if(type(value) == "table") then
Serialize(value,sKey,hFile)
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value)
write(hFile,sKey.." = "..sValue)
end write(hFile,",\n")
end write(hFile,"}")
end
-----------------------------------------------------------
function saveTableToFile(file, table, tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
-----------------------------------------------------------
function loadTableFromFile(file)
local handle = io.open(file,"r")
if (handle ~= nil) then
loadstring(handle:read("*all"))
handle:flush()
handle:close()
end
end
Thanks, seems to work.. but i still got problem with the
Serializeion itself...
and changeing write to io.write dont help...
it gives syntax error....
function Serialize(tTable, sTableName, hFile)
write(hFile,sTableName.." = {\n")
for key,value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key)
if(type(value) == "table") then
Serialize(value,sKey,hFile)
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value)
write(hFile,sKey.." = "..sValue)
end
write(hFile,",\n")
end
write(hFile,"}")
end
Use this serialize m8, should work then....
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end
Well.. that didnt gave error... but it didnt work either...
i need to svae the table to a file.. witch that code dont do =(
You still need to keep this in script, which is what saves the file....
function saveTableToFile(file, table, tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
Just tested it, all works fine, saves aswell :)
but i do.... :/
Just incase your not sure..
-- ChatStats v3 Made By Optimus
-- Based on Tezlo chats
-- Added Send Commands By TiMeTrAVelleR
-- Madman fixed some in commands
-- Converted to lua5 by Madman with very little help by Jelf
-- with some help by ?
sBot = "Chatstats" -- Name of Bot
SendComm = 1 -- Send UserCommands 1 = On 0 = Off
pMenu = "-=( ChatStats )=-" -- Name of Menu
Chatstats = {}
Sortstats = 2 -- 1=words / 2=posts
ChatStatsFile = "chatstats.tbl"
EnableChatStats = {
[0] = 1, -- Master
[1] = 1, -- Operators
[2] = 1, -- Vips
[3] = 1, -- Regs
[4] = 1, -- Moderators
[5] = 1, -- NetFounders
[-1] = 1, -- Users (UnRegged)
}
AllowedProfiles = {
[0] = 1, -- Masters
[1] = 0, -- Operators
[4] = 0, -- Moderator
[5] = 0, -- NetFounder
}
function Main()
--frmHub:RegBot(sBot)
loadTableFromFile(ChatStatsFile)
end
function NewUserConnected(user)
if SendComm == 1 and EnableChatStats[user.iProfile] == 1 then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
end
user:SendData("$UserCommand 1 3 "..pMenu.."\\TopChatters$<%[mynick]> !topchatters||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\My Chat Stat$<%[mynick]> !mychatstat||")
if AllowedProfiles[user.iProfile] == 1 then
user:SendData("$UserCommand 1 3 "..pMenu.."\\Del Chatter$<%[mynick]> !delchatter %[line:Nick]||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\Clear Chat Stats$<%[mynick]> !clearchatstats||")
end
end
end
OpConnected = NewUserConnected
function OnExit()
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
function ChatArrival(user, data)
if (string.sub(data, 1, 1) == "<" ) then
if EnableChatStats[user.iProfile] == 1 then
local s,e,str = string.find(data, "^%b<> (.*)%|$")
updStats(user.sName, str)
end
data=string.sub(data,1,string.len(data)-1)
s,e,cmd=string.find(data, "%b<>%s+(%S+)")
if (cmd == "!mychatstat") then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
return 1
else
user:SendData(sBot, "*** No chat statics found!")
return 1
end
elseif (cmd=="!topchatters") then
TCopy={}
if Chatstats then
for i,v in Chatstats do
table.insert(TCopy,{i,v.post,v.chars,v.words})
end
table.sort(TCopy,function(a,b) return (a[Sortstats] > b[Sortstats]) end)
local chat = "Current Top Chatters:\r\n\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
chat = chat.."\t Nr.\tPosts:\tChars:\tWords:\tName:\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
for i = 1,25 do
if TCopy[i] then
-- Nr: Posts: Chars: Words: Name:
chat = chat.."\t "..i..".\t "..TCopy[i][2].."\t "..TCopy[i][3].."\t "..TCopy[i][4].."\t"..TCopy[i][1].."\r\n"
end
end
user:SendData(sBot, chat)
TCopy={}
end
return 1
elseif (cmd == "!delchatter") and AllowedProfiles[user.iProfile] == 1 then
local s,e,cmd,name = string.find( data, "%b<>%s+(%S+)%s+(%S+)" )
if name then
if Chatstats[name] then
Chatstats[name] = nil
user:SendData(sBot, "Chatstats from user "..name.." are now removed!")
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
else
user:SendData(sBot, "*** Chatstats from user "..name.." not found!")
end
else
user:SendData(sBot, "*** Usage: !delchatter ")
end
return 1
elseif (cmd == "!clearchatstats") and AllowedProfiles[user.iProfile] == 1 then
Chatstats = {}
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
user:SendData(sBot, "Chatstats are cleared by "..user.sName)
return 1
end
end
end
function updStats(nick, str)
local tmp = Chatstats[nick] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x")}
tmp["post"], tmp["chars"], tmp["words"], tmp["time"] = tmp["post"]+1, tmp["chars"]+string.len(str), tmp["words"]+cntargs(str,"(%a+)"), os.date("%x")
Chatstats[nick] = tmp
-- saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
function cntargs(str, rule)
local s,n = string.gsub(str, rule, "")
return n
end
----------------------------------------------
-- load & save Tables
----------------------------------------------
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end
-----------------------------------------------------------
function saveTableToFile(file, table, tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
-----------------------------------------------------------
function loadTableFromFile(file)
local handle = io.open(file,"r")
if (handle ~= nil) then
loadstring(handle:read("*all"))
handle:flush()
handle:close()
end
end
thats the exact code i use...
The only time the file is saved is when the script is stopped
Are you geting the file chatstats.tbl ??
When i stop the script the file is saved with all chat info
yes i do... i'm sorry... i did some more testing.. it works fine..
Excellent :D
hmm....dont know what i'm doing wrong, but i must be doin something if you say it works..
the script saved the stats the 1st time, but after that it loses the new info everytime i restart the script.... any ideas?
Try this one (not tested):
-- ChatStats v3 Made By Optimus
-- Based on Tezlo chats
-- Added Send Commands By TiMeTrAVelleR
-- Madman fixed some in commands
-- Converted to lua5 by Madman with very little help by Jelf
-- with some help by ?
-- fixed stats saving on exit by jiten
sBot = "Chatstats" -- Name of Bot
SendComm = 1 -- Send UserCommands 1 = On 0 = Off
pMenu = "-=( ChatStats )=-" -- Name of Menu
Chatstats = {}
Sortstats = 2 -- 1=words / 2=posts
ChatStatsFile = "chatstats.tbl"
EnableChatStats = {
[0] = 1, -- Master
[1] = 1, -- Operators
[2] = 1, -- Vips
[3] = 1, -- Regs
[4] = 1, -- Moderators
[5] = 1, -- NetFounders
[-1] = 1, -- Users (UnRegged)
}
AllowedProfiles = {
[0] = 1, -- Masters
[1] = 0, -- Operators
[4] = 0, -- Moderator
[5] = 0, -- NetFounder
}
function Main()
frmHub:RegBot(sBot)
dofile(ChatStatsFile)
end
function NewUserConnected(user)
if SendComm == 1 and EnableChatStats[user.iProfile] == 1 then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
end
user:SendData("$UserCommand 1 3 "..pMenu.."\\TopChatters$<%[mynick]> !topchatters||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\My Chat Stat$<%[mynick]> !mychatstat||")
if AllowedProfiles[user.iProfile] == 1 then
user:SendData("$UserCommand 1 3 "..pMenu.."\\Del Chatter$<%[mynick]> !delchatter %[line:Nick]||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\Clear Chat Stats$<%[mynick]> !clearchatstats||")
end
end
end
OpConnected = NewUserConnected
function OnExit()
if isEmpty(Chatstats) then
else
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
end
function ChatArrival(user, data)
if (string.sub(data, 1, 1) == "<" ) then
if EnableChatStats[user.iProfile] == 1 then
local s,e,str = string.find(data, "^%b<> (.*)%|$")
updStats(user.sName, str)
end
data=string.sub(data,1,string.len(data)-1)
s,e,cmd=string.find(data, "%b<>%s+(%S+)")
if (cmd == "!mychatstat") then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
return 1
else
user:SendData(sBot, "*** No chat statics found!")
return 1
end
elseif (cmd=="!topchatters") then
TCopy={}
if Chatstats then
for i,v in Chatstats do
table.insert(TCopy,{i,v.post,v.chars,v.words})
end
table.sort(TCopy,function(a,b) return (a[Sortstats] > b[Sortstats]) end)
local chat = "Current Top Chatters:\r\n\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
chat = chat.."\t Nr.\tPosts:\tChars:\tWords:\tName:\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
for i = 1,25 do
if TCopy[i] then
-- Nr: Posts: Chars: Words: Name:
chat = chat.."\t "..i..".\t "..TCopy[i][2].."\t "..TCopy[i][3].."\t "..TCopy[i][4].."\t"..TCopy[i][1].."\r\n"
end
end
user:SendData(sBot, chat)
TCopy={}
end
return 1
elseif (cmd == "!delchatter") and AllowedProfiles[user.iProfile] == 1 then
local s,e,cmd,name = string.find( data, "%b<>%s+(%S+)%s+(%S+)" )
if name then
if Chatstats[name] then
Chatstats[name] = nil
user:SendData(sBot, "Chatstats from user "..name.." are now removed!")
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
else
user:SendData(sBot, "*** Chatstats from user "..name.." not found!")
end
else
user:SendData(sBot, "*** Usage: !delchatter ")
end
return 1
elseif (cmd == "!clearchatstats") and AllowedProfiles[user.iProfile] == 1 then
Chatstats = {}
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
user:SendData(sBot, "Chatstats are cleared by "..user.sName)
return 1
end
end
end
function updStats(nick, str)
local tmp = Chatstats[nick] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x")}
tmp["post"], tmp["chars"], tmp["words"], tmp["time"] = tmp["post"]+1, tmp["chars"]+string.len(str), tmp["words"]+cntargs(str,"(%a+)"), os.date("%x")
Chatstats[nick] = tmp
-- saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
function cntargs(str, rule)
local s,n = string.gsub(str, rule, "")
return n
end
----------------------------------------------
-- load & save Tables
----------------------------------------------
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end
-----------------------------------------------------------
function saveTableToFile(file, table, tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
-----------------------------------------------------------
function loadTableFromFile(file)
local handle = io.open(file,"r")
if (handle ~= nil) then
loadstring(handle:read("*all"))
handle:flush()
handle:close()
end
end
-------------table checker by herodes
--- for an associative table, like ["smth"] = "smth else",
function isEmpty(t)
for i,v in t do
return false;
end
return true;
end;
Cheers.
nope...still goes back to first save.. :(
First post updated (not tested). Try it...
works perfect! thx a lot! :))
yw :]
-- ChatStats v3 Made By Optimus
-- Based on Tezlo chats
-- Added Send Commands By TiMeTrAVelleR
-- Madman fixed some in commands
-- Converted to lua5 by Madman with very little help by Jelf
-- with some help by ?
-- fixed stats saving on exit by jiten
---- Modded by Madman
-- Added so it's creates ChatStatsFile, if it's missing
-- Fixed so it dont counts commands
-- Added a IgnoreTable, users in that wont be counted
----
sBot = "Chatstats" -- Name of Bot
SendComm = 1 -- Send UserCommands 1 = On 0 = Off
pMenu = "-=( ChatStats )=-" -- Name of Menu
ChatStatsTo = "user" -- Send TopChatters to? user or all
Chatstats = {}
Sortstats = 2 -- 1=words / 2=posts
ChatStatsFile = "chatstats.tbl"
IgnoreTable = {
-- 0=dont ignore/1=ignore
["Madman"] = 0,
["-=FakeKiller=-"] = 1,
}
EnableChatStats = {
[0] = 1, -- Master
[1] = 1, -- Operators
[2] = 1, -- Vips
[3] = 1, -- Regs
[4] = 1, -- Moderators
[5] = 1, -- NetFounders
[-1] = 1, -- Users (UnRegged)
}
AllowedProfiles = {
[0] = 1, -- Masters
[1] = 0, -- Operators
[4] = 0, -- Moderator
[5] = 0, -- NetFounder
}
function Main()
frmHub:RegBot(sBot)
local file = io.open(ChatStatsFile, "r")
if file then
file:close()
else
local file = io.open(ChatStatsFile, "w+")
file:write()
file:close()
end
dofile(ChatStatsFile)
end
function NewUserConnected(user)
if SendComm == 1 and EnableChatStats[user.iProfile] == 1 then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
end
user:SendData("$UserCommand 1 3 "..pMenu.."\\TopChatters$<%[mynick]> !topchatters||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\My Chat Stat$<%[mynick]> !mychatstat||")
if AllowedProfiles[user.iProfile] == 1 then
user:SendData("$UserCommand 1 3 "..pMenu.."\\Del Chatter$<%[mynick]> !delchatter %[line:Nick]||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\Clear Chat Stats$<%[mynick]> !clearchatstats||")
end
end
end
OpConnected = NewUserConnected
function OnExit()
if isEmpty(Chatstats) then
else
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
end
function IsCmd(str)
return string.sub(str, 1, 1) == "!" or string.sub(str, 1, 1) == "?" or string.sub(str, 1, 1) == "+" or string.sub(str, 1, 1) == "#"
end
function ChatArrival(user, data)
if EnableChatStats[user.iProfile] == 1 then
local s,e,cmd = string.find(data,"%b<>%s+(%S+)")
if IsCmd(cmd) then
elseif IgnoreTable[user.sName] == 1 then
else
local s,e,str = string.find(data, "^%b<> (.*)%|$")
updStats(user.sName, str)
end
end
data=string.sub(data,1,string.len(data)-1)
s,e,cmd=string.find(data, "%b<>%s+(%S+)")
if (cmd == "!mychatstat") then
if Chatstats[user.sName] then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
return 1
else
user:SendData(sBot, "*** No chat statics found!")
return 1
end
elseif (cmd=="!topchatters") then
TCopy={}
if Chatstats then
for i,v in Chatstats do
table.insert(TCopy,{i,v.post,v.chars,v.words})
end
table.sort(TCopy,function(a,b) return (a[Sortstats] > b[Sortstats]) end)
local chat = "Current Top Chatters:\r\n\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
chat = chat.."\t Nr.\tPosts:\tChars:\tWords:\tName:\r\n"
chat = chat.."\t ------------------------------------------------------------------------\r\n"
for i = 1,25 do
if TCopy[i] then
-- Nr: Posts: Chars: Words: Name:
chat = chat.."\t "..i..".\t "..TCopy[i][2].."\t "..TCopy[i][3].."\t "..TCopy[i][4].."\t"..TCopy[i][1].."\r\n"
end
end
if ChatStatsTo == "user" then
user:SendData(sBot, chat)
elseif ChatStatsTo == "all" then
SendToAll(sBot, chat)
end
TCopy={}
end
return 1
elseif (cmd == "!delchatter") and AllowedProfiles[user.iProfile] == 1 then
local s,e,cmd,name = string.find( data, "%b<>%s+(%S+)%s+(%S+)" )
if name then
if Chatstats[name] then
Chatstats[name] = nil
user:SendData(sBot, "Chatstats from user "..name.." are now removed!")
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
else
user:SendData(sBot, "*** Chatstats from user "..name.." not found!")
end
else
user:SendData(sBot, "*** Usage: !delchatter ")
end
return 1
elseif (cmd == "!clearchatstats") and AllowedProfiles[user.iProfile] == 1 then
Chatstats = {}
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
user:SendData(sBot, "Chatstats are cleared by "..user.sName)
return 1
end
end
function updStats(nick, str)
local tmp = Chatstats[nick] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x")}
tmp["post"], tmp["chars"], tmp["words"], tmp["time"] = tmp["post"]+1, tmp["chars"]+string.len(str), tmp["words"]+cntargs(str,"(%a+)"), os.date("%x")
Chatstats[nick] = tmp
saveTableToFile(ChatStatsFile, Chatstats, "Chatstats")
end
function cntargs(str, rule)
local s,n = string.gsub(str, rule, "")
return n
end
----------------------------------------------
-- load & save Tables
----------------------------------------------
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]",key) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",value) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end
-----------------------------------------------------------
function saveTableToFile(file, table, tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:flush()
handle:close()
end
-----------------------------------------------------------
function loadTableFromFile(file)
local handle = io.open(file,"r")
if (handle ~= nil) then
loadstring(handle:read("*all"))
handle:flush()
handle:close()
end
end
-------------table checker by herodes
--- for an associative table, like ["smth"] = "smth else",
function isEmpty(t)
for i,v in t do
return false;
end
return true;
end;
i have to much free time... =)
had some lil free time..-- ChatStats v3 Made By Optimus
-- Based on Tezlo chats
-- Added Send Commands By TiMeTrAVelleR
-- Madman fixed some in commands
-- Converted to lua5 by Madman with very little help by Jelf
-- with some help by ?
-- fixed stats saving on exit by jiten
---- Modded by Madman
-- Added so it's creates ChatStatsFile, if it's missing
-- Fixed so it dont counts commands
-- Added a IgnoreTable, users in that wont be counted
---- Touched by Herodes
-- addded : Averaging statistics for words per posts and chars per posts.
-- changed: ChatArrival parsing
-- changed: display of mychatstat
----
sBot = "Chatstats" -- Name of Bot
SendComm = 1 -- Send UserCommands 1 = On 0 = Off
pMenu = "-=( ChatStats )=-" -- Name of Menu
ChatStatsTo = "user" -- Send TopChatters to? user or all
Chatstats = {}
Sortstats = 2 -- 1=words / 2=posts
ChatStatsFile = "chatstats.tbl"
IgnoreTable = {
-- 0=dont ignore/1=ignore
["Madman"] = 0,
["-=FakeKiller=-"] = 1,
}
EnableChatStats = {
[0] = 1, -- Master
[1] = 1, -- Operators
[2] = 1, -- Vips
[3] = 1, -- Regs
[4] = 1, -- Moderators
[5] = 1, -- NetFounders
[-1] = 1, -- Users (UnRegged)
}
AllowedProfiles = {
[0] = 1, -- Masters
[1] = 0, -- Operators
[4] = 0, -- Moderator
[5] = 0, -- NetFounder
}
function Main()
frmHub:RegBot(sBot)
local file = io.open(ChatStatsFile, "r")
if file then
file:close()
else
local file = io.open(ChatStatsFile, "w+")
file:write()
file:close()
end
dofile(ChatStatsFile)
end
function NewUserConnected(user)
if ( (SendComm == 1) and (EnableChatStats[user.iProfile] == 1) ) then
if (Chatstats[user.sName]) then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
end
if (user.bUserCommand) then
user:SendData("$UserCommand 1 3 "..pMenu.."\\TopChatters$<%[mynick]> !topchatters||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\My Chat Stat$<%[mynick]> !mychatstat||")
if (AllowedProfiles[user.iProfile] == 1) then
user:SendData("$UserCommand 1 3 "..pMenu.."\\Del Chatter$<%[mynick]> !delchatter %[line:Nick]||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\Clear Chat Stats$<%[mynick]> !clearchatstats||")
end
end
end
end
OpConnected = NewUserConnected
function OnExit()
if not isEmpty(Chatstats) then
SaveToFile(ChatStatsFile, Chatstats, "Chatstats")
end
end
function ChatArrival(user, data)
if AllowedProfiles[user.iProfile] == 1 then
local data = string.sub( data , 1, -2 )
local s,e, cmd = string.find ( data, "%b<>%s+[%!%?%+%#](%S+)" )
if cmd then
local tCmds = {
["mychatstat"] = function (user, data)
if Chatstats[user.sName] then
user:SendData(sBot, StatsToString( Chatstats[user.sName], user.sName ))
else
user:SendData(sBot, "*** No chat statics found!")
end
return 1
end,
["topchatters"] = function ( user, data )
tCopy={}
if Chatstats then
for i,v in Chatstats do
table.insert( tCopy, { i, v.post, v.chars, v.words, v.smileys} )
end
table.sort( tCopy, function(a, b) return (a[Sortstats] > b[Sortstats]) end)
local chat = "Current Top Chatters:\r\n\r\n"
chat = chat.."\t ------------------------------------------------------------------------------------------------------------\r\n"
chat = chat.."\t Nr.\tPosts:\tChars:\tWords:\tHappy:\tSad:\tName:\r\n"
chat = chat.."\t ------------------------------------------------------------------------------------------------------------\r\n"
for i = 1,25 do
if tCopy[i] then
-- Nr: Posts: Chars: Words: Name:
chat = chat.."\t "..i..".\t "..tCopy[i][2].."\t "..tCopy[i][3].."\t "..tCopy[i][4].."\t "..tCopy[i][5].happy.."\t "..tCopy[i][5].sad.."\t"..tCopy[i][1].."\r\n"
end
end
if ChatStatsTo == "user" then
user:SendData(sBot, chat)
elseif ChatStatsTo == "all" then
SendToAll(sBot, chat)
end
tCopy=nil
end
return 1
end,
["delchatter"] = function (user, data)
if AllowedProfiles[user.iProfile] == 1 then
local s,e,cmd,name = string.find( data, "%b<>%s+(%S+)%s+(%S+)" )
if name then
if Chatstats[name] then
Chatstats[name] = nil
user:SendData(sBot, "Chatstats from user "..name.." are now removed!")
SaveToFile(ChatStatsFile, Chatstats, "Chatstats")
else
user:SendData(sBot, "*** Chatstats from user "..name.." not found!")
end
else
user:SendData(sBot, "*** Usage: !delchatter ")
end
end
return 1
end,
["clearchatstats"] = function (user, data)
if AllowedProfiles[user.iProfile] == 1 then
Chatstats = {}
SaveToFile(ChatStatsFile, {}, "Chatstats")
user:SendData(sBot, "Chatstats are cleared by "..user.sName)
end
return 1
end,
}
if tCmds[cmd] then return tCmds[cmd](user, data); end;
return 1;
end
end
if EnableChatStats[user.iProfile] == 1 and not IgnoreTable[user.sName] then
local s,e,str = string.find(data, "^%b<> (.*)%|$")
updStats(user.sName, str)
end
end
function updStats(nick, str)
local function cntargs(str, rule) local s,n = string.gsub(str, rule, "");return n;end;
local t = Chatstats[nick] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x"), ["smileys"] = { ["happy"] = 0, ["sad"] = 0,}, }
t.post = t.post + 1
t.chars = t.chars + string.len(str)
t.words = t.words + cntargs( str, "(%a+)")
t.smileys.happy = t.smileys.happy + cntargs( str, "%s-(%:%-?%))")
t.smileys.sad = t.smileys.sad + cntargs( str, "%s-(%:%-?%()")
t.time = os.date("%x")
Chatstats[nick] = t
SaveToFile( ChatStatsFile, Chatstats, "Chatstats" )
end
function StatsToString( tTable, nick )
local function doRatio( val, max ) if (max==0) then max=1;end;return string.format("%0.3f",(val/max));end;
local function doPerc ( val, max ) if (max==0) then max=1;end;return string.format("%0.2f",((val*100)/max));end;
local sMsg = "\r\n\t\t\tHere are the stats for "..nick
sMsg = sMsg.."\r\n\t--------------------------------------------------------------------------------------------------------------------------------------------------"
sMsg = sMsg.."\r\n\tPosts in MainChat :\t\t "..tTable.post
sMsg = sMsg.."\r\n\tWords in Posts :\t\t "..tTable.words.." [ "..doRatio( tTable.words, tTable.post ).." words per post ]"
sMsg = sMsg.."\r\n\tCharasters in Posts :\t "..tTable.chars.." [ "..doRatio( tTable.chars, tTable.post ).." chars per post ]"
sMsg = sMsg.."\r\n\t--------------------------------------------------------------------------------------------------------------------------------------------------"
sMsg = sMsg.."\r\n\t\tYou were happy in "..doPerc(tTable.smileys.happy, tTable.post).."% of your posts, and sad in "..doPerc(tTable.smileys.sad, tTable.post).."% of them."
return sMsg
end
----------------------------------------------
-- load & save Tables
----------------------------------------------
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]", string.gsub(key, "\"", "\"") ) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",string.gsub(value, "\"", "\"") ) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end
-----------------------------------------------------------
function SaveToFile(file, table, tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:close()
end
-----------------------------------------------------------
function LoadFromFile(file)
local handle = io.open(file,"r")
if handle then
loadstring(handle:read("*all"))
handle:flush()
handle:close()
end
end
-----------------------------------------------------------
function isEmpty(t)
return (next(t) == nil);
end;
Found some bugs in ur script, Herodes.
When typing any command in main chat with prefixes like !, ?, +, even though they aren't chatstats related, their content doesn't show up.
Just changed this and it's working ok:
if tCmds[cmd] then
return tCmds[cmd](user, data), 1
end
Btw, there are other problems.
The Top Chatters list doesn't appear correctly sorted by words (see the quote) and some users with more words/posts are replaced new ones with less when the table reaches the 25 users limit.
Check this:
[15:13:11] Current Top Chatters:
------------------------------------------------------------------------------------------------------------
Nr. Posts: Chars: Words: Happy: Sad: Name:
------------------------------------------------------------------------------------------------------------
1. 44 1484 165 2 0 dark_politics
2. 6 128 24 0 0 Recruta_70
3. 1 3 1 0 0 PiZaDoX_Mz
4. 20 367 60 0 1 Mavic
5. 44 432 84 1 1 Killer
6. 46 499 96 0 0 Cleopatra
And one more thing, not operators can't use the normal commands, because of the "if AllowedProfiles[user.iProfile] == 1 then" in the beginning of ChatArrival function that goes against the restrictions for each command.
Cheers ;)
I think it should be ok now ...-- ChatStats v3 Made By Optimus
-- Based on Tezlo chats
-- Added Send Commands By TiMeTrAVelleR
-- Madman fixed some in commands
-- Converted to lua5 by Madman with very little help by Jelf
-- with some help by ?
-- fixed stats saving on exit by jiten
---- Modded by Madman
-- Added so it's creates ChatStatsFile, if it's missing
-- Fixed so it dont counts commands
-- Added a IgnoreTable, users in that wont be counted
---- Touched by Herodes
-- addded : Averaging statistics for words per posts and chars per posts.
-- changed: ChatArrival parsing
-- changed: display of mychatstat
---
-- fixed: sorting problem.
-- added: max name showing.
-- some small optimisations.
sBot = "Chatstats" -- Name of Bot
SendComm = 1 -- Send UserCommands 1 = On 0 = Off
pMenu = "-=( ChatStats )=-" -- Name of Menu
ChatStatsTo = "user" -- Send TopChatters to? user or all
iHowManyToShow = 25
Chatstats = {}
Sortstats = 2 -- 1:words / 2:posts / 3:happy smilies / 4:sad smilies
ChatStatsFile = "chatstats.tbl"
IgnoreTable = {
-- 0=dont ignore/1=ignore
["Madman"] = 0,
["-=FakeKiller=-"] = 1,
}
EnableChatStats = {
[0] = 1, -- Master
[1] = 1, -- Operators
[2] = 1, -- Vips
[3] = 1, -- Regs
[4] = 1, -- Moderators
[5] = 1, -- NetFounders
[-1] = 1, -- Users (UnRegged)
}
AllowedProfiles = {
[0] = 1, -- Masters
[1] = 1, -- Operators
[2] = 0, -- VIPs
[3] = 0, -- Regs
[4] = 0, -- Moderator
[5] = 0, -- NetFounder
[-1] = 0, -- Unregged
}
function Main()
frmHub:RegBot(sBot)
local file = io.open(ChatStatsFile, "r")
if file then
file:close()
else
local file = io.open(ChatStatsFile, "w+")
file:write()
file:close()
end
dofile(ChatStatsFile)
end
function NewUserConnected(user)
if ( (SendComm == 1) and (EnableChatStats[user.iProfile] == 1) ) then
if (Chatstats[user.sName]) then
user:SendData(sBot, "---===[ Your Chat Stats: You Made "..Chatstats[user.sName]["post"].." Posts In Main Used "..Chatstats[user.sName]["chars"].." Characters, And "..Chatstats[user.sName]["words"].." Words ]===---")
end
if (user.bUserCommand) then
user:SendData("$UserCommand 1 3 "..pMenu.."\\TopChatters$<%[mynick]> !topchatters||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\My Chat Stat$<%[mynick]> !mychatstat||")
if (AllowedProfiles[user.iProfile] == 1) then
user:SendData("$UserCommand 1 3 "..pMenu.."\\Del Chatter$<%[mynick]> !delchatter %[line:Nick]||")
user:SendData("$UserCommand 1 3 "..pMenu.."\\Clear Chat Stats$<%[mynick]> !clearchatstats||")
end
end
end
end
OpConnected = NewUserConnected
function OnExit()
if next(Chatstats) then
SaveToFile(ChatStatsFile, Chatstats, "Chatstats")
end
end
function ChatArrival(user, data)
local data = string.sub( data , 1, -2 )
local s,e, cmd = string.find ( data, "%b<>%s+[%!%?%+%#](%S+)" )
if cmd then
local tCmds = {
["mychatstat"] = function (user, data)
if Chatstats[user.sName] then
user:SendData(sBot, StatsToString( Chatstats[user.sName], user.sName ))
else
user:SendData(sBot, "*** No chat statics found!")
end
return 1
end,
["topchatters"] = function ( user, data )
if AllowedProfiles[user.iProfile] == 1 then
local tCopy={}
if Chatstats then
for i,v in Chatstats do
table.insert( tCopy, { i, v.post, v.chars, v.words, v.smileys} )
end
local t = { [1] = "words", [2] = "posts", [3] = "smileys.happy", [4] = "smileys.sad" }; t = t[Sortstats];
table.sort( tCopy, function(a, b) return (a[t] > b[t]) end)
local m = "Current Top Chatters:\r\n\r\n"
m = m.."\t ------------------------------------------------------------------------------------------------------------\r\n"
m = m.."\t Nr.\tPosts:\tChars:\tWords:\tHappy:\tSad:\tName:\r\n"
m = m.."\t ------------------------------------------------------------------------------------------------------------\r\n"
for i = 1, iHowManyToShow do
if tCopy[i] then
-- Nr: Posts: Chars: Words: Name:
m = m.."\t "..i..".\t "..tCopy[i][2].."\t "..tCopy[i][3].."\t "..tCopy[i][4].."\t "..tCopy[i][5].happy.."\t "..tCopy[i][5].sad.."\t"..tCopy[i][1].."\r\n"
end
end
if ChatStatsTo == "user" then
user:SendData( sBot, m )
elseif ChatStatsTo == "all" then
SendToAll( sBot, m )
end
tCopy = nil
end
end
return 1
end,
["delchatter"] = function (user, data)
if AllowedProfiles[user.iProfile] == 1 then
local s,e,cmd,name = string.find( data, "%b<>%s+(%S+)%s+(%S+)" )
if not name then user:SendData(sBot, "*** Usage: !delchatter "); return 1; end;
if not Chatstats[name] then user:SendData(sBot, "*** Chatstats from user "..name.." not found!"); return 1; end;
Chatstats[name] = nil
user:SendData(sBot, "Chatstats from user "..name.." are now removed!")
SaveToFile(ChatStatsFile, Chatstats, "Chatstats")
end
return 1
end,
["clearchatstats"] = function (user, data)
if AllowedProfiles[user.iProfile] == 1 then
Chatstats = {}
SaveToFile(ChatStatsFile, {}, "Chatstats")
user:SendData(sBot, "Chatstats are cleared by "..user.sName)
end
return 1
end,
}
if tCmds[cmd] then return tCmds[cmd](user, data); end;
end
if EnableChatStats[user.iProfile] == 1 and not IgnoreTable[user.sName] then
local s,e,str = string.find(data, "^%b<> (.*)%|$")
if str then
local function cntargs(str, rule) local s,n = string.gsub(str, rule, "");return n;end;
local u = Chatstats[user.sName] or {["post"]=0, ["chars"]=0, ["words"]=0, ["time"]=os.date("%x"), ["smileys"] = { ["happy"] = 0, ["sad"] = 0,}, }
u.post = u.post + 1
u.chars = u.chars + string.len(str)
t.words = u.words + cntargs( str, "(%a+)")
u.smileys.happy = u.smileys.happy + cntargs( str, "%s-(%:%-?%))")
u.smileys.sad = u.smileys.sad + cntargs( str, "%s-(%:%-?%()")
u.time = os.date("%x")
Chatstats[user.sName] = u
SaveToFile( ChatStatsFile, Chatstats, "Chatstats" )
end
end
end
function StatsToString( tTable, nick )
local function doRatio( val, max ) if (max==0) then max=1;end;return string.format("%0.3f",(val/max));end;
local function doPerc ( val, max ) if (max==0) then max=1;end;return string.format("%0.2f",((val*100)/max));end;
local sMsg = "\r\n\t\t\tHere are the stats for "..nick
sMsg = sMsg.."\r\n\t--------------------------------------------------------------------------------------------------------------------------------------------------"
sMsg = sMsg.."\r\n\tPosts in MainChat :\t\t "..tTable.post
sMsg = sMsg.."\r\n\tWords in Posts :\t\t "..tTable.words.." [ "..doRatio( tTable.words, tTable.post ).." words per post ]"
sMsg = sMsg.."\r\n\tCharasters in Posts :\t "..tTable.chars.." [ "..doRatio( tTable.chars, tTable.post ).." chars per post ]"
sMsg = sMsg.."\r\n\t--------------------------------------------------------------------------------------------------------------------------------------------------"
sMsg = sMsg.."\r\n\t\tYou were happy in "..doPerc(tTable.smileys.happy, tTable.post).."% of your posts, and sad in "..doPerc(tTable.smileys.sad, tTable.post).."% of them."
return sMsg
end
----------------------------------------------
-- load & save Tables
----------------------------------------------
function Serialize(tTable, sTableName, sTab)
assert(tTable, "tTable equals nil");
assert(sTableName, "sTableName equals nil");
assert(type(tTable) == "table", "tTable must be a table!");
assert(type(sTableName) == "string", "sTableName must be a string!");
sTab = sTab or "";
sTmp = ""
sTmp = sTmp..sTab..sTableName.." = {\n"
for key, value in tTable do
local sKey = (type(key) == "string") and string.format("[%q]", string.gsub(key, "\"", "\"") ) or string.format("[%d]",key);
if(type(value) == "table") then
sTmp = sTmp..Serialize(value, sKey, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q",string.gsub(value, "\"", "\"") ) or tostring(value);
sTmp = sTmp..sTab.."\t"..sKey.." = "..sValue
end
sTmp = sTmp..",\n"
end
sTmp = sTmp..sTab.."}"
return sTmp
end
-----------------------------------------------------------
function SaveToFile(file, table, tablename)
local handle = io.open(file,"w+")
handle:write(Serialize(table, tablename))
handle:close()
end
-----------------------------------------------------------
function LoadFromFile(file)
local handle = io.open(file,"r")
if handle then
loadstring(handle:read("*all"))
handle:close()
end
end
Got one more:
Syntax ...crai\Desktop\17.03.dbg\scripts\chatstats.lua_5.0.lua:175: bad argument #1 to `len' (string expected, got nil)
Cheers
QuoteOriginally posted by jiten
Got one more:
Syntax ...crai\Desktop\17.03.dbg\scripts\chatstats.lua_5.0.lua:175: bad argument #1 to `len' (string expected, got nil)
Cheers
script two posts earlier than this is edited. this error was patched ..
i am using the 2nd script posted by herodes but everytime it says *** No chat statics found!