PtokaX forum

Lua 5.3/5.2/5.1 Scripts (for PtokaX 0.4.0.0 and newer) => Help with scripts => Topic started by: merlin_xl54 on 23 June, 2009, 17:27:44

Title: Add Exclude kick for certain nicks
Post by: merlin_xl54 on 23 June, 2009, 17:27:44
First I would like to know if certain letters are not allowed, or only for specific uses? sNick, sBot "s", iAUT "i" etc. I added a kAUT in the tSettings of the script to set the time for a kick.

I need help adding an exclusion table for certain nicks. I added a kick for nicks below a certain uptime using the attached Top Hubbers script. I would like to be able to exclude a nick from being kicked and not the warning. The purpose is once kicked I would add them to be excluded to give them time to raise their uptime.

Any help would be appreciated.

M

Below is the section I changed/added a kick to. Was curious if it is coded correctly as it does work.


-- Less than allowed
if tNick.TotalTime/iAverage < tSettings.kAUT*60 then
-- kick
return Core.Kick(user, tSettings.sBot, "*** Your Average uptime (AUT) is less than "..tSettings.kAUT..
" hour(s). Please post to our forum before you try to connect again!"), true
-- Warn
elseif tNick.TotalTime/iAverage < tSettings.iAUT*60 then
Core.SendPmToNick(user.sNick, tSettings.sBot, "*** Your Average uptime (AUT) is less than "..tSettings.iAUT..
" hour(s). Users above this average will be considered for VIP, and may log in when the Hub is full!")
end




--[[

TopHubbers 2.03 - LUA 5.1.2 [Strict][API 2] by jiten
????????????????????????????????????????????????????
Based on: OnHub Time Logger 1.65 by chill and Robocop's layout

Usage: !tophubbers; !tophubbers x-y; !hubtime <nick>; !myhubtime

CHANGELOG:
??????????
Fixed: Typo in table.sort function;
Added: OnExit (3/21/2006)
Fixed: Missing pairs() in SaveToFile
Changed: Removed iGlobalTime and added TotalTime count to OnTimer
Changed: SecondsToTime function values (3/24/2006)
Changed: math.floor/mod in TopHubbers' function; (3/5/2006)
Changed: SecondsToTime month value (4/17/2006);
Added: !hubtime <nick> - requested by speedX;
Changed: SecondsToTime function and small code bits (8/16/2006)
Changed: Table indexes;
Changed: SecondsToTime function to MinutesToTime;
Fixed: Inaccurate average uptime stuff (8/17/2006)
Changed: Average uptime function;
Changed: Session time for offline users doesn't get reset;
Added: Average uptime warning on connect - requested by speedX (8/20/2006)
Added: Customized profiles - requested by Naithif (8/20/2006)
Added: User Commands - requested by TT;
Added: Rankings and related commands [!myrank & !topranks] - requested by speedX;
Added: Toggle rank info on connect - requested by TT;
Fixed: !tophubbers x-y;
Added: Comments to the code;
Changed: Some code bits;
Added: Toggle between total and average uptime (8/24/2006)
Fixed: Minimum average uptime warning - reported by speedX;
Added: Maximum shown hubbers - requested by Naithif (8/29/2006)
Fixed: LUA 5.0/5.1 compatibility - reported by speedX (11/8/2006)
Added: string.lower check - requested by SwapY and speedX (11/10/2006)

Changed: Script converted to API 2 (11/24/2007)
Fixed: Changed Serialize function (12/23/2007)
Added: Send messages in PM (01/22/2008)
Added: Timer to save data on regular intervals (07/18/2008)
Added: Kick user below Minimum Average uptime (6/20/2009)

]]--

tSettings = {
-- Bot Name
sBot = SetMan.GetString(21),

-- Top Hubbers' DB
fOnline = "scripts/data/TopHubbers/tOnliners.tbl",

-- RightClick Menu
sMenu = "Top Hubbers",

-- Maximum hubbers to show when using !tophubbers
iMax = 15,

-- Send message to users with lower than specified Average uptime (AUT) [true = on; false = off]
bWarning = true,

-- Minimum Average uptime (hours) that triggers the warning
iAUT = 12,

-- Minimum Average uptime (hours) that triggers the kick
kAUT = 2,

-- Send hubtime stats on connect [true = on; false = off]
bRankOnConnect = false,

-- Profiles checked [0 = off; 1 = on]
tProfiles = { [-1] = 0, [0] = 0, [1] = 1, [2] = 1, [3] = 1, [4] = 1 },

-- Ranks criteria ["average" = Average total uptime; "total" = Total uptime]
sCriteria = "average",

-- Set your interval time to save data(in Hours)
sInterval = "12",

-- Reply in PM ?
sPmOnly = true,

-- Ranks
tRanks = {
--[[

The ranks must be added in ascending order [from the lowest to the highest]
{ "Rank", [time][string] }

[time] must be 1 or more digit(s)
[string] must be: s = second; m = minute; h = hour; D = day; W = week; M = month; Y = year

Example: { "God", "1M, 2D, 10s" }
Meaning: To become a God, your total uptime must be equal or higher than 1 month, 2 days and 10 seconds
]]--

-- Total uptime rank table
total = {
{ "Peasant", "1D, 1h, 1m, 1s" }, { "Pawn", "1D, 12h" }, { "Jester", "5D" },
{ "Warrior", "10D" }, { "Knight", "15D" }, { "Prince", "1M" },
{ "Dragonslayer", "2M" }, { "Conqueror", "3M" }, { "Viking", "4M" },
{ "King", "6M" }, { "Emperor", "8M" }, { "Hub Legend", "10M" },
{ "Hub God", "11M" }, { "SonicGod", "1Y, 1h, 1m, 1s" }
},

-- Daily average uptime rank table
average = {
{ "Peasant", "5D" }, { "Pawn", "15D" }, { "Jester", "1M" },
{ "Dragonslayer", "2M" }, { "Knight", "4M" }, { "Prince", "6M" },
{ "Warrior", "9M" }, { "Conqueror", "1Y" }, { "Viking", "1Y, 3M" },
{ "King", "1Y, 6M" }, { "Emperor", "1Y, 9M" }, { "Hub Legend", "2Y" },
{ "Hub God", "2Y, 6M" }, { "SonicGod", "3Y" }
}
}
}

tOnline = {}

OnStartup = function()
-- Register BotName if not registered
if tSettings.sBot ~= SetMan.GetString(21) then Core.RegBot(tSettings.sBot,"","",true) end
-- Load DB content
if loadfile(tSettings.fOnline) then dofile(tSettings.fOnline) end
-- Set and Start Timer
tSettings.iTimer = TmrMan.AddTimer(60*1000)
--Save Data Interval
tSettings.SaveData = TmrMan.AddTimer(tSettings.sInterval*60*60*1000)
end

OnTimer = function(iID)
if tSettings.iTimer and iID == tSettings.iTimer then
-- For each hubber
for i, v in pairs(tOnline) do
-- Online
if Core.GetUser(i) then
-- Sum
v.SessionTime = v.SessionTime + 1; v.TotalTime = v.TotalTime + 1
end
end
elseif tSettings.iTimer and iID == tSettings.SaveData then
local hFile = io.open(tSettings.fOnline, "w+") Serialize(tOnline, "tOnline", hFile); hFile:close()
end
end

OnExit = function()
-- Save
local hFile = io.open(tSettings.fOnline, "w+") Serialize(tOnline, "tOnline", hFile); hFile:close()
end

UserConnected = function(user)
-- If profile has permission to be logged
if tSettings.tProfiles[user.iProfile] and tSettings.tProfiles[user.iProfile] == 1 then
local tNick = GetOnliner(user.sNick)
-- User already in DB
if tNick then
-- Warning on connect
if tSettings.bWarning then
-- Days since first login
local iAverage = os.difftime(os.time(os.date("!*t")), tNick.Julian)/(60*60*24)
if iAverage < 1 then iAverage = 1 end
-- Less than allowed
if tNick.TotalTime/iAverage < tSettings.kAUT*60 then
-- kick
return Core.Kick(user, tSettings.sBot, "*** Your Average uptime (AUT) is less than "..tSettings.kAUT..
" hour(s). Please post to our forum before you try to connect again!"), true
-- Warn
elseif tNick.TotalTime/iAverage < tSettings.iAUT*60 then
Core.SendPmToNick(user.sNick, tSettings.sBot, "*** Your Average uptime (AUT) is less than "..tSettings.iAUT..
" hour(s). Users above this average will be considered for VIP, and may log in when the Hub is full!")
end
end
-- Reset and save time
tNick.SessionTime = 0; tNick.Enter = os.date()
-- Send rank info on connect
if tSettings.bRankOnConnect then tCommands["myhubtime"].fFunction(user) end
else
-- Create new entry
tOnline[user.sNick] = { Julian = os.time(os.date("!*t")), Enter = os.date(), SessionTime = 0, TotalTime = 0, Leave = os.date() }
end
end
-- Supports UserCommands
if Core.GetUserValue(user,12) then
-- For each entry in table
for i, v in pairs(tCommands) do
-- If member
if v.tLevels[user.iProfile] then
-- For each type
for n in ipairs(v.tRC) do
-- Send
Core.SendToNick(user.sNick, "$UserCommand 1 3 "..tSettings.sMenu.."\\"..v.tRC[n][1]..
"$<%[mynick]> !"..i..v.tRC[n][2].."&#124;")
end
end
end
end
end

OpConnected = UserConnected
RegConnected = UserConnected

UserDisconnected = function(user)
local tNick = GetOnliner(user.sNick)
-- If profile must be logged and user is in DB
if tSettings.tProfiles[user.iProfile] and tSettings.tProfiles[user.iProfile] == 1 and tNick then
-- Log date
tNick.Leave = os.date()
end
end

OpDisconnected = UserDisconnected
RegDisconnected = UserDisconnected

ChatArrival = function(user,data)
local _,_, to = data:find("^$To:%s(%S+)%s+From:")
local _,_, msg = data:find("%b<>%s(.*)|$")
-- Message sent to Bot or in Main
if (to and to == tSettings.sBot) or not to then
-- Parse command
local _,_, cmd = msg:find("^%p(%S+)")
-- Exists
if cmd and tCommands[cmd:lower()] then
cmd = cmd:lower()
-- If user has permission
if tCommands[cmd].tLevels[user.iProfile] then
if to and to == tSettings.sBot then
return Core.SendPmToNick(user.sNick, tSettings.sBot, tCommands[cmd].fFunction(user, msg)), true
else
 if tSettings.sPmOnly then
  return Core.SendPmToNick(user.sNick, tSettings.sBot, tCommands[cmd].fFunction(user, msg)), true
else
   return Core.SendToNick(user.sNick, "<"..tSettings.sBot.."> "..tCommands[cmd].fFunction(user, msg)), true
end
end
else
if to and to == tSettings.sBot then
return Core.SendPmToNick(user.sNick, tSettings.sBot, "*** Error: You are not allowed to use this command!"), true
else
return Core.SendToNick(user.sNick, "<"..tSettings.sBot.."> *** Error: You are not allowed to use this command!"), true
end
end
end
end
end

ToArrival = ChatArrival

tCommands = {
tophubbers = {
fFunction = function(user, data)
-- Table isn't empty
if next(tOnline) then
-- Parse limits
local _,_, iStart, iEnd = data:find("^%S+%s+(%d+)%-(%d+)$")
-- Set if not set
iStart, iEnd = (iStart or 1), (iEnd or tSettings.iMax)
-- Header
local tCopy, msg = {}, "\r\n\t"..string.rep("=", 137).."\r\n\tNr.  Total:\t\t\t\t\tSession:\t"..
"Entered Hub:\tLeft Hub:\t\tRank:\t\tStatus:\tName:\r\n\t"..string.rep("-", 240).."\r\n"
-- Loop through hubbers
for i, v in pairs(tOnline) do
-- Insert stats to temp table
table.insert(tCopy, { sEnter = v.Enter, iSessionTime = tonumber(v.SessionTime),
iTotalTime = tonumber(v.TotalTime), sLeave = v.Leave, sNick = i, sRank = GetRank(i) } )
end
-- Sort by total time
table.sort(tCopy, function(a, b) return (a.iTotalTime > b.iTotalTime) end)
-- Loop through temp table
for i = iStart, iEnd, 1 do
-- i exists
if tCopy[i] then
-- Populate
local sStatus, v = "*Offline*", tCopy[i]; local sRank = v.sRank
if Core.GetUser(v.sNick) then sStatus = "*Online*" end
if v.sRank:len() < 9 then sRank = sRank.."\t" end
msg = msg.."\t"..i..".    "..MinutesToTime(v.iTotalTime).."\t"..v.iSessionTime..
" min\t"..v.sEnter.."\t"..v.sLeave.."\t"..sRank.."\t"..sStatus.."\t"..v.sNick.."\r\n"
end
end
msg = msg.."\t"..string.rep("-", 240)
-- Send
return  "Current Top Hubbers:\r\n"..msg.."\r\n"
else
return "*** Error: Top Hubbers' table is currently empty!"
end
end,
tLevels = {
[0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1,
},
tRC = { { "Show Top "..tSettings.iMax.." Hubbers", "" }, { "Show Top x-y Hubbers", " %[line:x-y]" } }
},
hubtime = {
fFunction = function(user, data)
-- Parse nick
local _,_, nick = data:find("^%S+%s+(%S+)$")
-- Exists
if nick then
-- Return
return BuildStats(user, nick)
else
return "*** Syntax Error: Type !hubtime <nick>"
end
end,
tLevels = {
[0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1,
},
tRC = { { "Show a User's Stats", " %[line:Nick]" } }
},
myhubtime = {
fFunction = function(user)
-- Return
return BuildStats(user, user.sNick)
end,
tLevels = {
[0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1,
},
tRC = { { "Show My Stats", "" } }
},
}

BuildStats = function(user, nick)
local tNick = GetOnliner(nick)
-- In DB
if tNick then
-- Average uptime in days
local iAverage = os.difftime(os.time(os.date("!*t")), tNick.Julian)/(60*60*24)
if iAverage < 1 then iAverage = 1 end
-- Generate message
local sMsg = "\r\n\r\n\t"..string.rep("=", 51).."\r\n\t\t\tStats:\r\n\t"..
string.rep("-", 89).."\r\n\t- Nick: "..nick.."\r\n\t- Total uptime: "..
MinutesToTime(tNick.TotalTime, true).."\r\n\t- Daily average uptime (AUT): "..
MinutesToTime((tNick.TotalTime/iAverage), true).."\r\n\t- Current Rank: "..GetRank(nick).."\r\n"
-- Send stats
return "Here are the stats you requested for "..nick..", Remember, users with 12 hours (AUT) will be considered for VIP, and may bypass the Hub full rule."  ..sMsg
else
return "*** Error: No record found for '"..nick.."'!"
end
end

GetRank = function(nick)
local tNick = GetOnliner(nick)
if tNick then
-- Custom time table
local tTime, sRank, iAverage = { s = 1/60, m = 1, h = 60, D = 60*24, W = 60*24*7, M = 60*24*30, Y = 60*24*30*12 }, tSettings.tRanks[tSettings.sCriteria:lower()][1][1]
-- Average enabled
if tSettings.bAverage then
-- Days since first login
iAverage = os.difftime(os.time(os.date("!*t")), tNick.Julian)/(60*60*24)
if iAverage < 1 then iAverage = 1 end
end
-- For each rank
for n in ipairs(tSettings.tRanks[tSettings.sCriteria:lower()]) do
local iTime = 0
-- For each digit and time string
for i, v in tSettings.tRanks[string.lower(tSettings.sCriteria)][n][2]:gmatch("(%d+)(%w)") do
-- Process
if i and tTime[v] then iTime = iTime + i*tTime[v] end
end
local iValue = tNick.TotalTime
-- Average
if tSettings.bAverage then iValue = iValue/iAverage end
-- Process rank if user hasn't logged in for the first time today
if os.date("%d%m%y", tNick.Julian) ~= os.date("%d%m%y") and iValue > iTime then
sRank = tSettings.tRanks[tSettings.sCriteria:lower()][n][1]
end
end
return sRank
end
end

MinutesToTime = function(iSeconds, bSmall)
-- Build table with time fields
local T = os.date("!*t", tonumber(iSeconds*60));
-- Format to string
local sTime = string.format("%i month(s), %i day(s), %i hour(s), %i minute(s)", T.month-1, T.day-1, T.hour, T.min)
-- Small stat?
if bSmall then
-- For each digit
for i in sTime:gmatch("%d+") do
-- Reduce if is preceeded by 0
if tonumber(i) == 0 then sTime = sTime:gsub("^"..i.."%s(%S+),%s", "") end
end
end
-- Return
return sTime
end

GetOnliner = function(user)
-- For each hubber
for i, v in pairs(tOnline) do
-- Compare
if i:lower() == user:lower() then
-- Return
return tOnline[i]
end
end
end

Serialize = function(tTable, sTableName, hFile, sTab)
sTab = sTab or "";
hFile:write(sTab..sTableName.." = {\n");
for key, value in pairs(tTable) do
if (type(value) ~= "function") then
local sKey = (type(key) == "string") and string.format("[%q]", key) or string.format("[%d]", key);
if(type(value) == "table") then
Serialize(value, sKey, hFile, sTab.."\t");
else
local sValue = (type(value) == "string") and string.format("%q", value) or tostring(value);
hFile:write(sTab.."\t"..sKey.." = "..sValue);
end
hFile:write(",\n");
end
end
hFile:write(sTab.."}");
end
Title: Re: Add Exclude kick for certain nicks
Post by: CrazyGuy on 25 June, 2009, 00:45:35
Quote from: merlin_xl54 on 23 June, 2009, 17:27:44
First I would like to know if certain letters are not allowed, or only for specific uses? sNick, sBot "s", iAUT "i" etc. I added a kAUT in the tSettings of the script to set the time for a kick.

All letters are allowed. This specific usage of letters your refer to is a naming standard in which the first letter of the variable suggests the type. Those you see often in our scripts and the API are

s for string
i for integer (=number in LUA)
t for table
b for boolean

This to make it easier for yourself and/or others to understand your code. You do however not have to follow these standards.

Quote from: merlin_xl54 on 23 June, 2009, 17:27:44
I need help adding an exclusion table for certain nicks. I added a kick for nicks below a certain uptime using the attached Top Hubbers script. I would like to be able to exclude a nick from being kicked and not the warning. The purpose is once kicked I would add them to be excluded to give them time to raise their uptime.

I'm not quite sure what you mean exactly.
If you want to exclude certain nicks, you can add them to a table and check this table before executing the action.
add this to the beginning of your code

tExclude = {}


and change your code to the following

-- Less than allowed
if tNick.TotalTime/iAverage < tSettings.kAUT*60 then
-- kick if not excluded
if not tExclude[user.sNick] then
-- add to exclude list for next time
tExclude[user.sNick] = 1
-- kick
return Core.Kick(user, tSettings.sBot, "*** Your Average uptime (AUT) is less than "..tSettings.kAUT..
" hour(s). Please post to our forum before you try to connect again!"), true
end
-- Warn
elseif tNick.TotalTime/iAverage < tSettings.iAUT*60 then
Core.SendPmToNick(user.sNick, tSettings.sBot, "*** Your Average uptime (AUT) is less than "..tSettings.iAUT..
" hour(s). Users above this average will be considered for VIP, and may log in when the Hub is full!")
end


Something like that ?

Quote from: merlin_xl54 on 23 June, 2009, 17:27:44
Was curious if it is coded correctly as it does work

If it works, it is probably coded correctly  ;)

Title: Re: Add Exclude kick for certain nicks
Post by: merlin_xl54 on 25 June, 2009, 05:34:01
Thanks CrazyGuy...

Works perfectly!

I went with the first line only. I want to kick anyone not in the tExclude tabe if their uptime is below what I set etc...


-- kick if not excluded
if not tExclude[user.sNick] then


Not sure what this was for? Would that kick them the first time and then exclude them the next time etc?

-- add to exclude list for next time
tExclude[user.sNick] = 1
         
Title: Re: Add Exclude kick for certain nicks
Post by: CrazyGuy on 25 June, 2009, 12:45:22
exactly
Title: Re: Add Exclude kick for certain nicks
Post by: merlin_xl54 on 25 June, 2009, 14:01:30
Quote from: CrazyGuy on 25 June, 2009, 12:45:22
exactly

Where does it store the information? I don't see it in the table I created in the script. Does it lose that info when the hub, client, PC, and/or script restarts?


tExclude = {
["merlin_xl54"]= true,
               },
Title: Re: Add Exclude kick for certain nicks
Post by: CrazyGuy on 25 June, 2009, 23:09:10
Yes
Title: Re: Add Exclude kick for certain nicks
Post by: merlin_xl54 on 30 June, 2009, 23:48:15
After using a bit I've noticed that after adding a user to the tExcluded table it keeps them from being kicked as I would like, but it also stops the subsequent warning.

The time for the kick and warning are different in the tSettings etc and I was hoping that the tExclude table would only apply to the kick.
I only use the line pertaining to the table. I removed the  --add to exclude list for next time line.

Any suggestions appreciated.

-M
Title: Re: Add Exclude kick for certain nicks
Post by: CrazyGuy on 01 July, 2009, 01:20:53
The Exclude table is only looked at when the kick-time has passed in the above code. It should have no bearing on the warning part.