RecordBot - LUA 5.1x [Strict][API 2] - Page 2
 

RecordBot - LUA 5.1x [Strict][API 2]

Started by speedX, 05 July, 2009, 19:30:13

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

speedX

#25
Thanks for the Report

--[[

	RecordBot 1.7b - LUA 5.0/5.1 by jiten (11/8/2006)
	
	Converted to API 2 by speedX (07/05/2009)

	Based on RecordBot vKryFinal written by bonki 2003

	Description: Logs and displays a hub's all time share and user record.

	- Fixed: Huge users bug and some stuff (thx to T?M??r?V?ll?R)
	- Fixed: Stats sending before MOTD
	- Added: Top Share and Sharer Record (requested by XPMAN)
	- Added: Reg Bot switch (requested by (uk)jay)
	- Fixed: Nil Max Sharer (thx Cosmos)
	- Added: Ignore List (requested by chettedeboeuf)
	- Fixed: User Record Time (11/26/2005)
	- Added: Top Sharer and Share validation delay (requested by chettedeboeuf)
	- Changed: Command Parsing and profile permission structure
	- Fixed: Top Sharer and Share Delay bug (thx to chettedeboeuf)
	- Chaged: Some code rewritten
	- Added: Time/Date to each record message (requested by Troubadour)
	- Changed: Chat and ToArrival structure;
	- Removed: !rb.set command;
	- Merged: tSettings and Record table;
	- Changed: Some code bits and variables;
	- Updated: To LUA 5.1 (11/8/2006)
	- Updated: To API 2 (07/05/2009) by speedX
	
  + Changes by speedX:   
    - Added: Rightclick Menu  (Requested by TZB)
    - Added: Commands for separate Share Record ,User Record and Top Sharer (Requested by TZB)
    - Added: Command to get Top Hub UpTime  (Requested by TZB)
    - Fixed: User Record  (Reported by Dreams)

]]--

tSettings = {

	-- Bot Name
	sBot = "RecordBot",

	-- Toggle automatically register Bot Name [true = on, false = off]
	bRegister = true,

	-- RecordBot DB
	fRecord = "tRecord.txt",

	-- Ignore table
	tIgnore = { ["jiten"] = 1, ["yournick"] = 1, },

	-- Top Sharer and Top Share validation delay (minutes)
	iDelay = 0,
	
	-- Top Hub Up Time Message Interval (in minutes)
	iInterval = 60,

	-- Message settings [ true = on, false = off ]

		-- Show report in Main
		bMain = true,

		-- Show report in PM
		bPM = false,

		-- Show report on Login
		bLogin = true,

	-- Commands
	sHelp = "rb.help", sShow = "rb.show", sSetup = "rb.set", sReset = "rb.reset", sShareRecord = "rb.sharerecord", sUserRecord = "rb.userrecord", sTopSharer = "rb.topsharer", sUpTime = "rb.topuptime" 

}

tRecord, tDelay , tUserC , iCount = {}, {} , {} , 0

OnStartup = function()
  tSettings.fRecord = Core.GetPtokaXPath().."scripts/"..tSettings.fRecord
	if (tSettings.sBot ~= SetMan.GetString(21) or tSettings.bRegister) then Core.RegBot(tSettings.sBot,"","",true) end
	if loadfile(tSettings.fRecord) then dofile(tSettings.fRecord) end
	tSettings.iTimer = TmrMan.AddTimer(1000)
end

ChatArrival = function(user, data)
  -- Define vars
  local _,_, to = string.find(data, "^$To:%s(%S+)%s+From:")
  local _,_, msg = string.find(data, "%b<>%s(.*)|$") 
  -- Message sent to Bot or in Main
  if (to and to == tSettings.sBot) or not to then
    -- Parse command
    local _,_, cmd = string.find(msg, "^%p(%S+)")
    -- Exists
    if cmd and tCommands[string.lower(cmd)] then
      cmd = string.lower(cmd)
      -- If user has permission
      if tCommands[cmd].tLevels[user.iProfile] and tCommands[cmd].tLevels[user.iProfile] == 1 then
        if to and to == tSettings.sBot then
          return Core.SendPmToNick(user.sNick,tSettings.sBot,tCommands[cmd].fFunction(user)), true
        else
          return Core.SendToNick(user.sNick,"<"..tSettings.sBot.."> "..tCommands[cmd].fFunction(user)), true
        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

OnExit = function()
	SaveToFile(tRecord, "tRecord", tSettings.fRecord)
end

tCommands = {
	[tSettings.sHelp] =	{ 
		fFunction = function(user)
			-- Header
			local sMsg = "\r\n\r\n\t\t\t"..string.rep("=", 80).."\r\n"..string.rep("\t", 6).."RecordBot - LUA 5.1x version by jiten "..
			"\t\t\t\r\n\t\t\t"..string.rep("-", 160).."\r\n\t\t\tAvailable Commands:".."\r\n\r\n"
			-- Loop through table
			for i,v in pairs(tCommands) do
				-- If user has permission
				if v.tLevels[user.iProfile] and v.tLevels[user.iProfile] == 1 then
					-- Populate
					sMsg = sMsg.."\t\t\t!"..i.."\t\t"..v.tDesc.."\r\n"
				end
			end
			-- Send
			return sMsg.."\t\t\t"..string.rep("-",160);
		end, 
		tLevels = {
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
		},
		tDesc = "\tDisplays this help message\t\t\t!"..tSettings.sHelp.."",
		tRC = {{ "RecordBot Help","" }},
	},
	[tSettings.sShow] = { 
		fFunction = function(user)
			if next(tRecord) then
				local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tValue\t\tDate - Time\n\t"..string.rep ("-", 100).."\r\n"..
				"\tShare\t\t"..(DoShareUnits(tRecord.iShare) or 0).." \t\t"..(tRecord.tShare or "n/a")..
				"\r\n\tUsers\t\t"..(tRecord.iUsers or 0).." user(s)\t\t"..(tRecord.tUsers or "n/a")..
				"\r\n\tTop Sharer\t"..(tRecord.sMaxSharer or "n/a").." ("..(DoShareUnits(tRecord.iMaxSharer) or 0)..
				")\t"..(tRecord.tMaxSharer or "n/a")..
				"\r\n\tTop UpTime\t"..(SecondsToTime(tRecord.iHubTime,true) or "n/a").."\t"..(tRecord.tHubTime or "n/a")..
        "\r\n\t"..string.rep ("-", 100)
				return msg
			else
				return "*** Error: No records have been saved."
			end
		end,
		tLevels = {
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		},
		tDesc = "\tShows this hub's all time share and user record\t!"..tSettings.sShow,
		tRC = {{ "Show All Records","" }},
	},
	[tSettings.sReset] = {
		fFunction = function(user)
			tRecord = {}; Core.SendToAll("<"..tSettings.sBot.."> *** Hub records have been reset!");
			return "You Have Reset All Records!"
		end, 
		tLevels = {
			[-1] = 0, [0] = 1, [1] = 0, [2] = 0, [3] = 0, [4] = 0, [5] = 1, 
		},
		tDesc = "\tResets all records\t\t\t\t!"..tSettings.sReset,
		tRC = {{ "Reset Record","" }},
	},
	[tSettings.sShareRecord] = { 
		fFunction = function(user)
			if next(tRecord) then
				local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tValue\t\tDate - Time\n\t"..string.rep ("-", 100).."\r\n"..
				"\tShare\t\t"..(DoShareUnits(tRecord.iShare) or 0).." \t\t"..(tRecord.tShare or "n/a")..
				"\r\n\t"..string.rep ("-", 100)
				return msg
			else
				return "*** Error: No records have been saved."
			end
		end,
		tLevels = {
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		},
		tDesc = "\tShows hub's all time share record\t!"..tSettings.sShareRecord,
		tRC = {{ "Share Record","" }},
	},
	[tSettings.sUserRecord] = { 
		fFunction = function(user)
			if next(tRecord) then
				local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tValue\t\tDate - Time\n\t"..string.rep ("-", 100).."\r\n"..
				"\tUsers\t\t"..(tRecord.iUsers or 0).." user(s)\t\t"..(tRecord.tUsers or "n/a")..
				"\r\n\t"..string.rep ("-", 100)
				return msg
			else
				return "*** Error: No records have been saved."
			end
		end,
		tLevels = {
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		},
		tDesc = "\tShows hub's all time user record\t!"..tSettings.sUserRecord,
		tRC = {{ "User Record","" }},
	},
	[tSettings.sTopSharer] = { 
		fFunction = function(user)
			if next(tRecord) then
				local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tValue\t\tDate - Time\n\t"..string.rep ("-", 100).."\r\n"..
				"\tTop Sharer\t"..(tRecord.sMaxSharer or "n/a").." ("..(DoShareUnits(tRecord.iMaxSharer) or 0)..
				")\t"..(tRecord.tMaxSharer or "n/a").."\r\n\t"..string.rep ("-", 100)
				return msg
			else
				return "*** Error: No records have been saved."
			end
		end,
		tLevels = {
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		},
		tDesc = "\tShows hub's all time top sharer\t!"..tSettings.sTopSharer,
		tRC = {{ "Top Sharer","" }},
	},
	[tSettings.sUpTime] = { 
		fFunction = function(user)
			if next(tRecord) then
				local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tDate - Time\tValue\n\t"..string.rep ("-", 100).."\r\n"..
				"\tTop Uptime\t"..(tRecord.tHubTime or "n/a").."\t"..(SecondsToTime(tRecord.iHubTime,true) or "n/a").."\r\n\t"..string.rep ("-", 100)
				return msg
			else
				return "*** Error: No records have been saved."
			end
		end,
		tLevels = {
			[-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1, 
		},
		tDesc = "\tShows hub's Top Uptime\t!"..tSettings.sUpTime,
		tRC = {{ "Top Uptime","" }},
	},
};

UserConnected = function(user)
	if not tSettings.tIgnore[user.sNick] then
		tDelay[user] = {}
		tDelay[user]["iTime"] = tSettings.iDelay*60
		table.insert(tUserC,user.sNick)
	end
	for i,v in pairs(tCommands) do
		if v.tLevels[user.iProfile] and v.tLevels[user.iProfile] == 1 then
			for n in ipairs(v.tRC) do
				Core.SendToNick(user.sNick, "$UserCommand 1 3 RecordBot\\"..v.tRC[n][1].."$<%[mynick]> +"..i..v.tRC[n][2].."&#124;|")
			end
		end
	end
end

OpConnected,RegConnected = UserConnected,UserConnected

OnTimer = function(iID)
if tSettings.iTimer and iID == tSettings.iTimer then
	for nick,v in pairs(tDelay) do
		tDelay[nick]["iTime"] = tDelay[nick]["iTime"] - 1
		if tDelay[nick]["iTime"] <= 0 then
			if Core.GetUser(nick.sNick) then
				local iTotalShare, iShare, sNick = Core.GetCurrentSharedSize(), Core.GetUserValue(nick,16) , nick.sNick
				tRecord.iShare = (tRecord.iShare or 0)
				if (iTotalShare > tRecord.iShare) then
					tRecord.iShare, tRecord.tShare = iTotalShare, os.date()
					SaveToFile(tRecord, "tRecord", tSettings.fRecord)
					if tSettings.bPM then Core.SendPmToNick(nick.sNick, tSettings.sBot, "*** Thanks, buddie. You have just raised the all-time share record to "..DoShareUnits(iTotalShare).." :)"); end;
					if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** "..nick.sNick.." has just raised the all-time share record to: "..DoShareUnits(iTotalShare).." on "..os.date("%x")); end;
				end

				tRecord.iMaxSharer = (tRecord.iMaxSharer or 0)
				if (iShare > tRecord.iMaxSharer) then
					tRecord.iMaxSharer, tRecord.sMaxSharer, tRecord.tMaxSharer = iShare, nick.sNick, os.date()
					SaveToFile(tRecord, "tRecord", tSettings.fRecord)
					if tSettings.bPM then Core.SendPmToNick(nick.sNick,tSettings.sBot,"*** Thanks, buddie. You are our highest sharer with: "..DoShareUnits(iShare).."."); end;
					if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** "..nick.sNick.." is our all-time biggest sharer with: "..DoShareUnits((iShare)).." since "..os.date("%x").." :)"); 	end;
				end

				if tSettings.bLogin then
					local sMsg = "\r\n\r\n\t"..string.rep("=", 50).."\r\n\t\t\tStats\r\n\t"..
					string.rep("-", 100).."\r\n\r\n\tShare record: "..(DoShareUnits(tonumber(tRecord.iShare)) or 0).." [ "..
					(tRecord.tShare or "n/a").." ]\r\n\tUser record: "..(tRecord.iUsers or 0).." users [ "..
					(tRecord.tUsers or "n/a").." ]\r\n\tTop Sharer: "..(tRecord.sMaxSharer or "n/a")..
					" ("..(DoShareUnits(tRecord.iMaxSharer) or 0)..") [ "..(tRecord.tMaxSharer or "n/a").." ]\r\n\tTop UpTime: "..
          (SecondsToTime(tRecord.iHubTime,true) or "n/a").." [ "..(tRecord.tHubTime or "n/a").." ]\r\n"
					Core.SendToNick(nick.sNick,"<"..tSettings.sBot.."> "..sMsg)
				end;
			end
			tDelay[nick] = nil
		end
	end
	iTotalUpTime = Core.GetUpTime()
	tRecord.iHubTime = (tRecord.iHubTime or 0)
				if iTotalUpTime > tRecord.iHubTime then
				  tRecord.iHubTime , tRecord.tHubTime = iTotalUpTime , os.date()
				  SaveToFile(tRecord, "tRecord", tSettings.fRecord)
				  if tSettings.iInterval <= 0 then
				    if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** The Hub has crossed its peak uptime :D : "..SecondsToTime(tRecord.iHubTime,true)); 	end;
				  else
				    if iCount == 0 then
				      if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** The Hub has crossed its peak uptime :D : "..SecondsToTime(tRecord.iHubTime,true)); 	end;
				      iCount = iCount + 1
				    elseif iCount == tSettings.iInterval*60 then
				      if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** The Hub has crossed its peak uptime :D : "..SecondsToTime(tRecord.iHubTime,true)); 	end;
				      iCount = 1
				    else
				      iCount = iCount + 1
				    end
				  end
        end
    if table.getn(tUserC) > 0 then
      local iUserCount = Core.GetUsersCount()
      tRecord.iUsers, tRecord.tUsers = (tRecord.iUsers or 0), (tRecord.tUsers or "n/a")
      if (iUserCount > tRecord.iUsers) then
        tRecord.iUsers, tRecord.tUsers = iUserCount, os.date()
        SaveToFile(tRecord, "tRecord", tSettings.fRecord)
        if tSettings.bPM then
          Core.SendPmToNick(tUserC[1],tSettings.sBot,"*** Thanks, buddie. You've just raised the all-time share record!");
        end;
        if tSettings.bMain then
          Core.SendToAll("<"..tSettings.sBot.."> *** "..tUserC[1].." has just raised the all-time user record to: "..
          tRecord.iUsers.." users at "..os.date().." :)");
        end;
      end
      table.remove(tUserC,1)
    end
end
end

SecondsToTime = function(iSeconds, bSmall)
	-- Build table with time fields
	local T = os.date("!*t", tonumber(iSeconds)); 
	-- Format to string
	local sTime = string.format("%i year(s), %i month(s), %i day(s), %i hour(s), %i minute(s)", T.year-1970, 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

-- By kepp and NotRambitWombat 
DoShareUnits = function(intSize)
	if intSize and intSize ~= 0 then 
		local tUnits, intSize, sUnits = { "Bytes", "KB", "MB", "GB", "TB" }, tonumber(intSize)
		for index in ipairs(tUnits) do 
			if(intSize < 1024) then sUnits = tUnits[index];  break;  else   intSize = intSize / 1024;  end 
		end 
		return string.format("%0.1f %s",intSize, sUnits);
	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

SaveToFile = function(table,tablename,file)
	local hFile = io.open(file,"w+") Serialize(table,tablename,hFile); hFile:close()
  collectgarbage("collect")
	io.flush() 
end
Thanking You,

speedX

Dreams


Gnuff?

I believe i found a bug here:
-- Toggle automatically register Bot Name [true = on, false = off]
	bRegister = false,


It still shows up in userlist despite false
Gnuff

speedX

#28
Try this. Post bugs if any....

--[[

   RecordBot 1.7c - LUA 5.0/5.1 by jiten (11/8/2006)
   
   Converted to API 2 by speedX (07/05/2009)

   Based on RecordBot vKryFinal written by bonki 2003

   Description: Logs and displays a hub's all time share and user record.

   - Fixed: Huge users bug and some stuff (thx to T?M??r?V?ll?R)
   - Fixed: Stats sending before MOTD
   - Added: Top Share and Sharer Record (requested by XPMAN)
   - Added: Reg Bot switch (requested by (uk)jay)
   - Fixed: Nil Max Sharer (thx Cosmos)
   - Added: Ignore List (requested by chettedeboeuf)
   - Fixed: User Record Time (11/26/2005)
   - Added: Top Sharer and Share validation delay (requested by chettedeboeuf)
   - Changed: Command Parsing and profile permission structure
   - Fixed: Top Sharer and Share Delay bug (thx to chettedeboeuf)
   - Chaged: Some code rewritten
   - Added: Time/Date to each record message (requested by Troubadour)
   - Changed: Chat and ToArrival structure;
   - Removed: !rb.set command;
   - Merged: tSettings and Record table;
   - Changed: Some code bits and variables;
   - Updated: To LUA 5.1 (11/8/2006)
   - Updated: To API 2 (07/05/2009) by speedX
   
  + Changes by speedX:   
    - Added: Rightclick Menu  (Requested by TZB)
    - Added: Commands for separate Share Record ,User Record and Top Sharer (Requested by TZB)
    - Added: Command to get Top Hub UpTime  (Requested by TZB)
    - Fixed: User Record  (Reported by Dreams)
    - Fixed: Bot Registration

]]--

tSettings = {

   -- Bot Name
   sBot = "RecordBot",

   -- Toggle automatically register Bot Name [true = on, false = off]
   bRegister = true,

   -- RecordBot DB
   fRecord = "tRecord.txt",

   -- Ignore table
   tIgnore = { ["jiten"] = 1, ["yournick"] = 1, },

   -- Top Sharer and Top Share validation delay (minutes)
   iDelay = 0,
   
   -- Top Hub Up Time Message Interval (in minutes)
   iInterval = 60,

   -- Message settings [ true = on, false = off ]

      -- Show report in Main
      bMain = true,

      -- Show report in PM
      bPM = false,

      -- Show report on Login
      bLogin = true,

   -- Commands
   sHelp = "rb.help", sShow = "rb.show", sSetup = "rb.set", sReset = "rb.reset", sShareRecord = "rb.sharerecord", sUserRecord = "rb.userrecord", sTopSharer = "rb.topsharer", sUpTime = "rb.topuptime"

}

tRecord, tDelay , tUserC , iCount = {}, {} , {} , 0

OnStartup = function()
  tSettings.fRecord = Core.GetPtokaXPath().."scripts/"..tSettings.fRecord
   if tSettings.bRegister then Core.RegBot(tSettings.sBot,"","",true) end
   if loadfile(tSettings.fRecord) then dofile(tSettings.fRecord) end
   tSettings.iTimer = TmrMan.AddTimer(1000)
end

ChatArrival = function(user, data)
  -- Define vars
  local _,_, to = string.find(data, "^$To:%s(%S+)%s+From:")
  local _,_, msg = string.find(data, "%b<>%s(.*)|$")
  -- Message sent to Bot or in Main
  if (to and to == tSettings.sBot) or not to then
    -- Parse command
    local _,_, cmd = string.find(msg, "^%p(%S+)")
    -- Exists
    if cmd and tCommands[string.lower(cmd)] then
      cmd = string.lower(cmd)
      -- If user has permission
      if tCommands[cmd].tLevels[user.iProfile] and tCommands[cmd].tLevels[user.iProfile] == 1 then
        if to and to == tSettings.sBot then
          return Core.SendPmToNick(user.sNick,tSettings.sBot,tCommands[cmd].fFunction(user)), true
        else
          return Core.SendToNick(user.sNick,"<"..tSettings.sBot.."> "..tCommands[cmd].fFunction(user)), true
        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

OnExit = function()
   SaveToFile(tRecord, "tRecord", tSettings.fRecord)
end

tCommands = {
   [tSettings.sHelp] =   {
      fFunction = function(user)
         -- Header
         local sMsg = "\r\n\r\n\t\t\t"..string.rep("=", 80).."\r\n"..string.rep("\t", 6).."RecordBot - LUA 5.1x version by jiten "..
         "\t\t\t\r\n\t\t\t"..string.rep("-", 160).."\r\n\t\t\tAvailable Commands:".."\r\n\r\n"
         -- Loop through table
         for i,v in pairs(tCommands) do
            -- If user has permission
            if v.tLevels[user.iProfile] and v.tLevels[user.iProfile] == 1 then
               -- Populate
               sMsg = sMsg.."\t\t\t!"..i.."\t\t"..v.tDesc.."\r\n"
            end
         end
         -- Send
         return sMsg.."\t\t\t"..string.rep("-",160);
      end,
      tLevels = {
         [-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
      },
      tDesc = "\tDisplays this help message\t\t\t!"..tSettings.sHelp.."",
      tRC = {{ "RecordBot Help","" }},
   },
   [tSettings.sShow] = {
      fFunction = function(user)
         if next(tRecord) then
            local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tValue\t\tDate - Time\n\t"..string.rep ("-", 100).."\r\n"..
            "\tShare\t\t"..(DoShareUnits(tRecord.iShare) or 0).." \t\t"..(tRecord.tShare or "n/a")..
            "\r\n\tUsers\t\t"..(tRecord.iUsers or 0).." user(s)\t\t"..(tRecord.tUsers or "n/a")..
            "\r\n\tTop Sharer\t"..(tRecord.sMaxSharer or "n/a").." ("..(DoShareUnits(tRecord.iMaxSharer) or 0)..
            ")\t"..(tRecord.tMaxSharer or "n/a")..
            "\r\n\tTop UpTime\t"..(SecondsToTime(tRecord.iHubTime,true) or "n/a").."\t"..(tRecord.tHubTime or "n/a")..
        "\r\n\t"..string.rep ("-", 100)
            return msg
         else
            return "*** Error: No records have been saved."
         end
      end,
      tLevels = {
         [-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
      },
      tDesc = "\tShows this hub's all time share and user record\t!"..tSettings.sShow,
      tRC = {{ "Show All Records","" }},
   },
   [tSettings.sReset] = {
      fFunction = function(user)
         tRecord = {}; Core.SendToAll("<"..tSettings.sBot.."> *** Hub records have been reset!");
         return "You Have Reset All Records!"
      end,
      tLevels = {
         [-1] = 0, [0] = 1, [1] = 0, [2] = 0, [3] = 0, [4] = 0, [5] = 1,
      },
      tDesc = "\tResets all records\t\t\t\t!"..tSettings.sReset,
      tRC = {{ "Reset Record","" }},
   },
   [tSettings.sShareRecord] = {
      fFunction = function(user)
         if next(tRecord) then
            local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tValue\t\tDate - Time\n\t"..string.rep ("-", 100).."\r\n"..
            "\tShare\t\t"..(DoShareUnits(tRecord.iShare) or 0).." \t\t"..(tRecord.tShare or "n/a")..
            "\r\n\t"..string.rep ("-", 100)
            return msg
         else
            return "*** Error: No records have been saved."
         end
      end,
      tLevels = {
         [-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
      },
      tDesc = "\tShows hub's all time share record\t!"..tSettings.sShareRecord,
      tRC = {{ "Share Record","" }},
   },
   [tSettings.sUserRecord] = {
      fFunction = function(user)
         if next(tRecord) then
            local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tValue\t\tDate - Time\n\t"..string.rep ("-", 100).."\r\n"..
            "\tUsers\t\t"..(tRecord.iUsers or 0).." user(s)\t\t"..(tRecord.tUsers or "n/a")..
            "\r\n\t"..string.rep ("-", 100)
            return msg
         else
            return "*** Error: No records have been saved."
         end
      end,
      tLevels = {
         [-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
      },
      tDesc = "\tShows hub's all time user record\t!"..tSettings.sUserRecord,
      tRC = {{ "User Record","" }},
   },
   [tSettings.sTopSharer] = {
      fFunction = function(user)
         if next(tRecord) then
            local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tValue\t\tDate - Time\n\t"..string.rep ("-", 100).."\r\n"..
            "\tTop Sharer\t"..(tRecord.sMaxSharer or "n/a").." ("..(DoShareUnits(tRecord.iMaxSharer) or 0)..
            ")\t"..(tRecord.tMaxSharer or "n/a").."\r\n\t"..string.rep ("-", 100)
            return msg
         else
            return "*** Error: No records have been saved."
         end
      end,
      tLevels = {
         [-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
      },
      tDesc = "\tShows hub's all time top sharer\t!"..tSettings.sTopSharer,
      tRC = {{ "Top Sharer","" }},
   },
   [tSettings.sUpTime] = {
      fFunction = function(user)
         if next(tRecord) then
            local msg = "\r\n\r\n\t"..string.rep ("=", 50).."\r\n\tRecord\t\tDate - Time\tValue\n\t"..string.rep ("-", 100).."\r\n"..
            "\tTop Uptime\t"..(tRecord.tHubTime or "n/a").."\t"..(SecondsToTime(tRecord.iHubTime,true) or "n/a").."\r\n\t"..string.rep ("-", 100)
            return msg
         else
            return "*** Error: No records have been saved."
         end
      end,
      tLevels = {
         [-1] = 1, [0] = 1, [1] = 1, [2] = 1, [3] = 1, [4] = 1, [5] = 1,
      },
      tDesc = "\tShows hub's Top Uptime\t!"..tSettings.sUpTime,
      tRC = {{ "Top Uptime","" }},
   },
};

UserConnected = function(user)
   if not tSettings.tIgnore[user.sNick] then
      tDelay[user] = {}
      tDelay[user]["iTime"] = tSettings.iDelay*60
      table.insert(tUserC,user.sNick)
   end
   for i,v in pairs(tCommands) do
      if v.tLevels[user.iProfile] and v.tLevels[user.iProfile] == 1 then
         for n in ipairs(v.tRC) do
            Core.SendToNick(user.sNick, "$UserCommand 1 3 RecordBot\\"..v.tRC[n][1].."$<%[mynick]> +"..i..v.tRC[n][2].."&#124;|")
         end
      end
   end
end

OpConnected,RegConnected = UserConnected,UserConnected

OnTimer = function(iID)
if tSettings.iTimer and iID == tSettings.iTimer then
   for nick,v in pairs(tDelay) do
      tDelay[nick]["iTime"] = tDelay[nick]["iTime"] - 1
      if tDelay[nick]["iTime"] <= 0 then
         if Core.GetUser(nick.sNick) then
            local iTotalShare, iShare, sNick = Core.GetCurrentSharedSize(), Core.GetUserValue(nick,16) , nick.sNick
            tRecord.iShare = (tRecord.iShare or 0)
            if (iTotalShare > tRecord.iShare) then
               tRecord.iShare, tRecord.tShare = iTotalShare, os.date()
               SaveToFile(tRecord, "tRecord", tSettings.fRecord)
               if tSettings.bPM then Core.SendPmToNick(nick.sNick, tSettings.sBot, "*** Thanks, buddie. You have just raised the all-time share record to "..DoShareUnits(iTotalShare).." :)"); end;
               if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** "..nick.sNick.." has just raised the all-time share record to: "..DoShareUnits(iTotalShare).." on "..os.date("%x")); end;
            end

            tRecord.iMaxSharer = (tRecord.iMaxSharer or 0)
            if (iShare > tRecord.iMaxSharer) then
               tRecord.iMaxSharer, tRecord.sMaxSharer, tRecord.tMaxSharer = iShare, nick.sNick, os.date()
               SaveToFile(tRecord, "tRecord", tSettings.fRecord)
               if tSettings.bPM then Core.SendPmToNick(nick.sNick,tSettings.sBot,"*** Thanks, buddie. You are our highest sharer with: "..DoShareUnits(iShare).."."); end;
               if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** "..nick.sNick.." is our all-time biggest sharer with: "..DoShareUnits((iShare)).." since "..os.date("%x").." :)");    end;
            end

            if tSettings.bLogin then
               local sMsg = "\r\n\r\n\t"..string.rep("=", 50).."\r\n\t\t\tStats\r\n\t"..
               string.rep("-", 100).."\r\n\r\n\tShare record: "..(DoShareUnits(tonumber(tRecord.iShare)) or 0).." [ "..
               (tRecord.tShare or "n/a").." ]\r\n\tUser record: "..(tRecord.iUsers or 0).." users [ "..
               (tRecord.tUsers or "n/a").." ]\r\n\tTop Sharer: "..(tRecord.sMaxSharer or "n/a")..
               " ("..(DoShareUnits(tRecord.iMaxSharer) or 0)..") [ "..(tRecord.tMaxSharer or "n/a").." ]\r\n\tTop UpTime: "..
          (SecondsToTime(tRecord.iHubTime,true) or "n/a").." [ "..(tRecord.tHubTime or "n/a").." ]\r\n"
               Core.SendToNick(nick.sNick,"<"..tSettings.sBot.."> "..sMsg)
            end;
         end
         tDelay[nick] = nil
      end
   end
   iTotalUpTime = Core.GetUpTime()
   tRecord.iHubTime = (tRecord.iHubTime or 0)
            if iTotalUpTime > tRecord.iHubTime then
              tRecord.iHubTime , tRecord.tHubTime = iTotalUpTime , os.date()
              SaveToFile(tRecord, "tRecord", tSettings.fRecord)
              if tSettings.iInterval <= 0 then
                if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** The Hub has crossed its peak uptime :D : "..SecondsToTime(tRecord.iHubTime,true));    end;
              else
                if iCount == 0 then
                  if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** The Hub has crossed its peak uptime :D : "..SecondsToTime(tRecord.iHubTime,true));    end;
                  iCount = iCount + 1
                elseif iCount == tSettings.iInterval*60 then
                  if tSettings.bMain then Core.SendToAll("<"..tSettings.sBot.."> *** The Hub has crossed its peak uptime :D : "..SecondsToTime(tRecord.iHubTime,true));    end;
                  iCount = 1
                else
                  iCount = iCount + 1
                end
              end
        end
    if table.getn(tUserC) > 0 then
      local iUserCount = Core.GetUsersCount()
      tRecord.iUsers, tRecord.tUsers = (tRecord.iUsers or 0), (tRecord.tUsers or "n/a")
      if (iUserCount > tRecord.iUsers) then
        tRecord.iUsers, tRecord.tUsers = iUserCount, os.date()
        SaveToFile(tRecord, "tRecord", tSettings.fRecord)
        if tSettings.bPM then
          Core.SendPmToNick(tUserC[1],tSettings.sBot,"*** Thanks, buddie. You've just raised the all-time share record!");
        end;
        if tSettings.bMain then
          Core.SendToAll("<"..tSettings.sBot.."> *** "..tUserC[1].." has just raised the all-time user record to: "..
          tRecord.iUsers.." users at "..os.date().." :)");
        end;
      end
      table.remove(tUserC,1)
    end
end
end

SecondsToTime = function(iSeconds, bSmall)
   -- Build table with time fields
   local T = os.date("!*t", tonumber(iSeconds));
   -- Format to string
   local sTime = string.format("%i year(s), %i month(s), %i day(s), %i hour(s), %i minute(s)", T.year-1970, 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

-- By kepp and NotRambitWombat
DoShareUnits = function(intSize)
   if intSize and intSize ~= 0 then
      local tUnits, intSize, sUnits = { "Bytes", "KB", "MB", "GB", "TB" }, tonumber(intSize)
      for index in ipairs(tUnits) do
         if(intSize < 1024) then sUnits = tUnits[index];  break;  else   intSize = intSize / 1024;  end
      end
      return string.format("%0.1f %s",intSize, sUnits);
   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

SaveToFile = function(table,tablename,file)
   local hFile = io.open(file,"w+") Serialize(table,tablename,hFile); hFile:close()
  collectgarbage("collect")
   io.flush()
end
Thanking You,

speedX

cspetee

I think bSmall isn't work for me.
bSmall got true value but i got this: 0 year(s) 0 month(s) 2 day(s) 3 hour(s) 17 minute(s)
I would like to get this: 2 day(s) 3 hour(s) 17 minute(s)
What can I do?

speedX

Quote from: cspetee on 10 March, 2011, 03:55:42
I think bSmall isn't work for me.
bSmall got true value but i got this: 0 year(s) 0 month(s) 2 day(s) 3 hour(s) 17 minute(s)
I would like to get this: 2 day(s) 3 hour(s) 17 minute(s)
What can I do?

Its working fine here...
Thanking You,

speedX

cspetee

I found the error. I deleted the comma in the string.format but i forgot delete also in the regexp of sTime:gsub.
It's my fault, sorry.

speedX

Quote from: cspetee on 10 March, 2011, 12:29:41
I found the error. I deleted the comma in the string.format but i forgot delete also in the regexp of sTime:gsub.
It's my fault, sorry.

No probs
Thanking You,

speedX

SMF spam blocked by CleanTalk