Hey Guys, I need some help with this problem I Have...
I Have stored Many, I mean many kicks/bans in seperate files and I want to count them...
Ex:
XUSER | IP | REASON
ZUSER | IP | REASON
XUSER | IP | REASON
AUSER | IP | REASON
and so on....
In my text file I have 5 lines with XUSER how do I make a count
on that... how do I do that...?
waiting for help...
/NightLitch
you want to count how many times each user was kicked ?
read the file line by line.. split each with regexp and store them in a table
if you want code.. ill need more info
OK here it goes:
the file looks like this:
USER IP REASON
how do a piece of code look like to count the lines in the file then...
ex:
Rasp 213.43.32.21 Bad Files
Leia 32.43.122.167 Bad Words
Rasp 213.43.32.21 Sharing Windows
Kall 65.211.21.43 Messing with you :-)
how would a code look like so Rasp(user) is counted as 2 ??
/NightLitch
Could you give me a piece of code Tezlo how you would have done....
plz man... or someone else...
/NightLitch
hold on man..
just gotta comment the code so that you know whats going on
nice thx man
function readkicks(filename)
-- feed it a filename
-- returns an associative table with a kick-count for each user and a total kick-count
local file = openfile(filename, "r")
assert(file, "cant open "..filename)
local total, table = 0, {} -- setup counter variables
local line = read(file, "*l") -- read first line
while line do
total = total+1 -- update total kick-count
local s, e, nick, ip, reason = strfind(line, "(%S-)%s(%S-)%s(.*)")
assert(s, "bad format on line "..total) -- shouldnt happen
table[nick] = (table[nick] or 0)+1 -- create/update users kick-count
line = read(file, "*l") -- read next line
end closefile(file)
return table, total
end
function Main()
local table, total = readkicks("kicks.dat")
for nick, count in table do SendToNick("tezlo", ">> "..nick.." = "..count) end
-- to test if it works put your nick here ^^
end
ThX Tezlo....
You're the greatest...
/NightLitch
Tezlo fix this for me... doesn't get it to work...
Bot = "Test-Bot"
KickCMD = "!kick"
KICKFILE = ("kicks.txt")
KickCON = {}
function Main()
frmHub:RegBot(Bot)
LoadFile(KICKFILE)
local table, total = readkicks(KICKFILE)
for nick, count in KickCON do SendToNick("Night", ">> "..nick.." = "..count) end
-- to test if it works put your nick here ^^
end
function DataArrival(user,data)
if (strsub(data, 1, 1) == "<" ) then
return MAINCOMMAND(user, data)
end
end
function dokick(user,data,cmd)
_,_,cmd,vNick,vReason = strfind( data, "%b<>%s+(%S+)%s+(%S+)%s+(.+)" )
if not vReason then
user:SendData(Bot,"Usage: !kick ")
return 0
else
userToBeKicked = GetItemByName(vNick)
if user and not userToBeKicked then
user:SendData(Bot, "The user is not in the userlist")
else
userToBeKicked:SendPM(Bot, "You are being kicked because: "..vReason)
SendToAll(Bot, "User: "..vNick.." has been KICKED, Reason: "..vReason)
userToBeKicked:TempBan()
writeto(KICKFILE)
write(vNick.." "..vReason)
writeto()
end
end
end
function LoadFile(file)
assert(readfrom(file),"File not found...")
dostring(read("*all"))
readfrom()
end
function MAINCOMMAND(user, data)
-- remove end pipe
data=strsub(data,1,strlen(data)-1)
--extract command
_,_,cmd=strfind(data, "%b<>%s+(%S+)")
--check if cmd exist
if not cmd then cmd = "0" end
-- If you like to use another "sign" in front of cmd's as well ad it here.. but it can only be one char long. You will aslo need to change in the senttobot.lua file
if (strsub(cmd, 1, 1) ~= "!") then
return 0
else
-- make the cmd caseinsensitive
cmd = strlower(cmd)
if strlen(cmd) > 1 then
-- remove the + or ! to only need to check for one thing...
cmd=strsub(cmd,2,strlen(cmd))
local COMMANDS = (operatorfunctions(user, data, cmd))
if not COMMANDS then
return 1
else
return COMMANDS
end
end
end
end
function operatorfunctions(user, data, cmd)
if user.iProfile == 1 or user.iProfile == 0 then
if (cmd == "kick") then
dokick(user,data,cmd)
return 1
end
end
end
function readkicks(filename)
-- feed it a filename
-- returns an associative table with a kick-count for each user and a total kick-count
local file = openfile(filename, "r")
assert(file, "cant open "..filename)
local total, KickCON = 0, {} -- setup counter variables
local line = read(file, "*l") -- read first line
while line do
total = total+1 -- update total kick-count
local s, e, nick, reason = strfind(line, "(%S-)%s(%S-)%s(.*)")
assert(s, "bad format on line "..total) -- shouldnt happen
KickCON[nick] = (KickCON[nick] or 0)+1 -- create/update users kick-count
line = read(file, "*l") -- read next line
end closefile(file)
return KickCON, total
end
And can get in the kick function to send UserToBeKicked.sIP either... so plz....
/NightLitch
only after you tell me why you are trying to LoadFile(KICKFILE) as lua code
ahhh... My bad... That shouldn't even be there... hehe
plz fix it... It would help alot with my script...
Becouse of this function I can view the reason to in MyInfo command to l8r...
sorry man.. i would have to rewrite it
the code doesnt really make much sense to me
and you dont really use readkicks() in it anyway
instead..
i wrote this little script that also logs !kicks
function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
-- its a mainchat message
-- look for a command (and arguments)
local s, e, cmd, args = strfind(data, "^%b<> %!(%S+)%s*(.*)%|$")
if s then
-- found one
cmd = strlower(cmd)
if user.bOperator and cmd == "kick" then
-- its a kick command.. available to OPs only
return doKick(user, args)
end
end
end
end
function doKick(user, args)
-- look for arguments
local s, e, nick, reason = strfind(args, "^(%S+)%s*(.*)$")
if not s then
-- havent found what needed.. show syntax and get out
user:SendData(">> syntax: !kick [reason]")
return 1
end
-- check if nick is online
local usr = GetItemByName(nick)
if not usr then
-- he isnt.. report and get out
user:SendData(">> "..nick.." is not online")
return 1
end
-- adjust reason a bit.. its an optional argument
if reason == "" then reason = "for no reason"
else reason = "because "..reason
end
-- open file for appending and log the kick
local f = openfile("kicks.dat", "a")
if f then
write(f, nick.." "..usr.sIP.." "..reason)
closefile(f)
end
-- tell the user hes being kicked and kick him
usr:SendData(" You are being kicked by "..user.sName.." "..reason)
usr:TempBan()
-- let everyone know and get out
SendToAll(" User "..nick.." has been kicked by "..user.sName.." "..reason)
return 1
end
no hard feelings..
but i dont want to fix your code
id rather see that you know what youre doing
Tezlo could you help me... plz
Ok I fix it myself then...
ThX anyways then... hmmf...
/NightLitch
yeh :| you cant really expect me to fix your code
if you took some time to learn lua there would be no need for this
and yes i do think you need to.. your code is a mess
Guess I need to learn more then....
Took your words hard, but you're right...
But I now a thing or two in LUA so don't place a LOOSER stamp on me like that... not preciated...
But I thank you for setting me in the right place...
Hope to get future help from you...
Gonna fix this and post it and hope you can help if it's wrong...
Regards to Tezlo / NightLitch
I got it to work Tezlo, but of course with your code...
So I am going to read up on LUA now...
I Am feeling really bad right now... Trying to do more than I can or understand...
But ThX...
/NightLitch
Ok have done my own now BUT my show command show it likes this:
???????????????????????????????????????????????
User: Kalle
Kicked: 2
User: Kalle
Kicked: 1
_______________________________________________
1:st user have another name and 2:nd is right but don't want all to be shown....
/NightLitch
Fixed the things I needed myself...
Here's the final code:
--=============================================--
--= Kick-Bot
--= By: NightLitch 2003
--= Grettings To: Tezlo for all the help
--= AND Guidence and learning me a
--= one or two to make me understand..
--=============================================--
Bot = "-Kick-Bot-"
KICKCOUNT = {}
function Main()
frmHub:RegBot(Bot)
end
function DataArrival(user,data)
if strsub(data, 1, 1) == "<" then
data=strsub(data,1,strlen(data)-1)
_,_,cmd=strfind(data, "%b<>%s+(%S+)")
if not cmd then cmd = "0" end
if (strsub(cmd, 1, 1) ~= "!") then
return 0
else
cmd = strlower(cmd)
if strlen(cmd) > 1 then
cmd=strsub(cmd,2,strlen(cmd))
local commands = (vCommands(user, data, cmd))
if not commands then
return 1
else
return commands
end
end
end
end
end
function vCommands(user, data, cmd)
if user.bOperator then
if (cmd == "kick" ) then
doKick(user, data, cmd)
return 1
elseif (cmd == "show" ) then
doshow(user,data)
return 1
end
end
end
function doshow(user,data)
_,_,cmd,uNick = strfind( data, "%b<>%s+(%S+)%s+(%S+)")
if uNick == nil then
user:SendData(Bot," Syntax: !show ")
else
if not uNick then
user:SendData(Bot, "The user is not in the userlist")
else
local CoreCheck = ""
user:SendData(Bot,"???????????????????????????????????????????????")
local KICKCOUNT, total = readkicks("kicks.dat")
for vNick, count in KICKCOUNT do
if uNick ~= vNick then
user:SendData(Bot,"User: "..uNick)
user:SendData(Bot,"Kicked: "..count)
CoreCheck = "1"
end
end
user:SendData(Bot,"_______________________________________________")
if CoreCheck == "" then
user:SendData(Bot,"User is not in List....")
end
end
end
end
function doKick(user, data, cmd)
local s,e,cmd,nick,reason = strfind( data, "%b<>%s+(%S+)%s+(%S+)%s+(.+)" )
if not s then
user:SendData(Bot,"Syntax: !kick [reason]")
return 1
end
local vUser = GetItemByName(nick)
if not vUser then
user:SendData(Bot,nick.." is not online or wrong name...")
return 1
end
if reason == "" then reason = "for no reason"
else reason = "because "..reason
end
local f = openfile("kicks.dat", "a")
if f then
write(f, nick.." "..vUser.sIP.." "..reason.."\r\n")
closefile(f)
end
vUser:SendData(Bot,"You are being KICKED by "..user.sName.." "..reason)
vUser:TempBan()
SendToAll(Bot,"User "..nick.." has been KICKED by "..user.sName.." "..reason)
return 1
end
function readkicks(filename)
-- feed it a filename
-- returns an associative table with a kick-count for each user and a total kick-count
local file = openfile(filename, "r")
assert(file, "cant open "..filename)
local total, KICKCOUNT = 0, {} -- setup counter variables
local line = read(file, "*l") -- read first line
while line do
total = total+1 -- update total kick-count
local s, e, nick, ip, reason = strfind(line, "(%S-)%s(%S-)%s(.*)")
assert(s, "bad format on line "..total) -- shouldnt happen
KICKCOUNT[nick] = (KICKCOUNT[nick] or 0)+1 -- create/update users kick-count
line = read(file, "*l") -- read next line
end closefile(file)
return KICKCOUNT, total
end
Many many many thx to Tezlo...
And I will someday learn more... And you will sertainlly place me right more times.... :-)
/NightLitch
(Sorry for my bad english)
Hi there,,
NightLitch,,
Nice one,... it should help some,, & thks for it,... ;)
Tezlo,...
Nice to see you around,, m8 :))
Is retrobot on the way ? :o)
l8tr, ;)
Credits goes to Tezlo most of it....
Tezlo or somebody...
I Have tested to get the last Kick-Message to be shown...
But I can't do it.... so could you guys help me....
I used this code and tried modifiy it... but....
function LoadTable(table,file)
local handle = openfile(file, "r")
if (handle) then
local line = read(handle)
while line do
s,e,nick,ip,reason = strfind( line, "(.*) (.*) (.*)")
table[nick]=reason
line = read(handle)
end
closefile(handle)
end
end
/NightLitch
I have solved it myself again... here my solve code:
function ReadLog(filename)
local handle = openfile(filename, "r")
if (handle) then
local line = read(handle)
while line do
s,e,nick,ip,reason = strfind( line, "(%S-)%s(%S-)%s(.*)")
KICKMESSAGE[nick]=reason
line = read(handle)
end
closefile(handle)
end
end
/NightLitch