PtokaX forum

Archive => Archived 5.0 boards => Finished Scripts => Topic started by: Dessamator on 22 March, 2005, 12:16:22

Title: Reminder or alarm bot
Post by: Dessamator on 22 March, 2005, 12:16:22

-- Reminder Bot v1.5
-- By Dessamator
-- Ideas from Post Bot by Nerbos(and some functions aswell),
-- (Mutor, Plop thx for the hints/help, jiten(thnx for the debugging/help))
-- added dates, for reminders in diferent days.
-- Small bugfix in adding reminders reported by xeonfx
-- Fixed some small bugs (Getitembyname & checking time), thnx bastya and Mutor
--## Most recent changes & fixes on : 14/01/2006

fAlarm = "fAlarm.dat"
tAlarm =
{ ["tReminders"]={},
  ["Cfg"]=
{     -- Cfg Table
--## Configuration ##--
["sBot"] = "Reminder", --BotName
},
}
    --## END ##--
tCmds = {
["time"] =  {function (sBot,sUser)
sUser:SendData(sBot,os.date("Today is the %dth %B %Y & The time is %X" ))
end,"$UserCommand 1 3 Reminder\\Time$<%[mynick]> !time&#124;"},
["alarm"]  = {function(sBot,sUser,sTime,sReminder,sDate)
if not(tAlarm["tReminders"][sUser.sName]) then tAlarm["tReminders"][sUser.sName] = {} end
if next(tAlarm) and tAlarm["tReminders"][sUser.sName] then
for i=1,table.getn(tAlarm["tReminders"][sUser.sName]) do
if tAlarm["tReminders"][sUser.sName][i] and (tAlarm["tReminders"][sUser.sName][i]["sReminder"] == sReminders) then
sUser:SendData(sBot,"The Reminder already exists")
return 1
end
end
end
if sTime~="" and sReminder~="" and string.find(sTime,"%d+%d+:%d+%d+") then
local _,_,sDate= string.find(sDate,"(%d%d/%d%d/%d%d)" )
if sDate and (sDate<=os.date("%m/%d/%y")) then sUser:SendData(sBot,"Incorrect Time. Try Again!") return 1 end
sDate=sDate or os.date("%m/%d/%y")
local tStore ={
["sReminder"] = sReminder,
["sCtime"]=  os.date("%H:%M"),
["sAtime"] = sTime,
["sCdate"] = os.date("%m/%d/%y"),
["sAdate"] = sDate
--["alerts"] = 0
}
table.insert(tAlarm["tReminders"][sUser.sName], tStore)
sUser:SendData(sBot,"Your Reminder has been set.")
else
sUser:SendData(sBot,"Syntax Error, !alarm <time: ex.: 19:19> <reminder> <date>(optional (MM/DD/YY ) Default is Today")
end 
OnExit()
end,"$UserCommand 1 3 Reminder\\Set Reminder$<%[mynick]> !alarm %[line:time ex.:"..os.date("%H:%M").."] %[line:Reminder] %[line:Date(MM/DD/YY) e.g.:"..os.date("%m/%d/%y").."]&#124;",},
["aread"] ={ function(sBot,sUser)
if not(SendReminder(sBot,sUser.sName,1)) then
sUser:SendData(sBot,"No Scheduled apointments")
end
end,"$UserCommand 1 3 Reminder\\View Reminder$<%[mynick]> !aread&#124;"},
["delalarm"] = {function (sBot,sUser,iID)
if iID == nil or tonumber(iID)==nil then
sUser:SendData(sBot,"Syntax Error, !delalarm <ID> , the ID must be specified!")
elseif (tAlarm["tReminders"][(sUser.sName)][tonumber(iID)] == nil) then
sUser:SendData(sBot,"That post doesnt exist!")
elseif  tAlarm["tReminders"][(sUser.sName)][tonumber(iID)] then
table.remove(tAlarm["tReminders"][sUser.sName],tonumber(iID))
sUser:SendData(sBot,"The post has been deleted!" )
         SaveToFile(fAlarm , tAlarm , "tAlarm")
else
sUser:SendData(sBot,"You are not authorized to delete the post !")
end
OnExit()
end,"$UserCommand 1 3 Reminder\\Remove Reminder$<%[mynick]> !delalarm %[line:Reminder <id> ex.: 1]&#124;",},
}

function Main()
frmHub:RegBot(tAlarm["Cfg"]["sBot"])
if loadfile(fAlarm) then dofile(fAlarm) end
SetTimer(60*1000);
StartTimer()
end

-- right clicks command
function NewUserConnected(sUser)
sUser:SendData("$UserCommand 255 3 |")
sUser:SendData("$UserCommand 0 3 |")
for i,v in pairs(tCmds) do
sUser:SendData(v[2])
end
end
OpConnected=NewUserConnected
--

function ChatArrival(sUser, data)
local s,e,sCmd,sArg1,sArg2,sArg3 = string.find(data,"%b<>%s+!(%S*)%s*(%S*)%s*(%S*)%s*(.*)|")
if sCmd and tCmds[sCmd] then
return 1,tCmds[sCmd][1](tAlarm["Cfg"]["sBot"],sUser,sArg1,sArg2,sArg3)
end
end

ToArrival=ChatArrival

function OnTimer()
for sUser,iID in pairs(tAlarm["tReminders"]) do
if tAlarm["tReminders"][sUser] and next(tAlarm["tReminders"][sUser]) then
SendReminder(tAlarm["Cfg"]["sBot"],sUser)
if tAlarm["tReminders"][sUser] and table.getn(tAlarm["tReminders"][sUser])==0 then
tAlarm["tReminders"][sUser] =nil OnExit()
end
end
end
end

function SendReminder(sBot,sUser,status)
local sTmp,tRemind,bFound,iATime,iADate = "\r\n\Your reminder(s) :\r\n\ID\t\Created on:\t\Scheduled for\t\t\Reminder\r\n"..string.rep("==",30).."\r\n"
if tAlarm["tReminders"][sUser] and next(tAlarm["tReminders"][sUser]) then
for i=1,table.getn(tAlarm["tReminders"][sUser]) do
tRemind =tAlarm["tReminders"][sUser][i]
if tRemind then
iATime = (string.gsub(tRemind.sAtime,"%:",""))
iADate = (string.gsub(tRemind.sAdate,"/",""))
if status or  tonumber(os.date("%H%M"))==tonumber(iATime) and tonumber(os.date("%m%d%y"))==tonumber(iADate) then
sTmp = sTmp
..i.."\t"..tRemind.sCtime.." "..tRemind["sCdate"].."\t"
..tRemind.sAtime.." "..tRemind["sAdate"].."\t"
.."\t"..tRemind["sReminder"].."\r\n"
if GetItemByName(sUser) then
GetItemByName(sUser):SendPM(sBot,sTmp)
end
end
if tonumber(os.date("%H%M"))>tonumber(iATime)  and tonumber(os.date("%m%d%y"))>=tonumber(iADate)then
table.remove(tAlarm["tReminders"][sUser],i)
end
end
end
return tRemind
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(file,table,tablename)
local hFile = io.open(file,"w+") Serialize(table,tablename,hFile); hFile:close() 
end

function OnExit()
SaveToFile(fAlarm , tAlarm , "tAlarm")
end



Title:
Post by: bastya_elvtars on 22 March, 2005, 12:32:06
Very good idea! You won't mind me to grab and rewrite later, will you? ;)
Title:
Post by: Dessamator on 22 March, 2005, 13:09:31
QuoteOriginally posted by bastya_elvtars
Very good idea! You won't mind me to grab and rewrite later, will you? ;)

ooh yes i do, no thieves, hehhe, just kidding use it the way u want to, rewrite change it, whatever, no probs here,  :D
Title:
Post by: Oz on 22 August, 2005, 21:26:55
hi

have this error

0.3.3.21.nt.dbg\scripts\alarmbot.lua:253: attempt to perform arithmetic on a nil value

if u can, plzzz correct this error

tnx
Title:
Post by: Dessamator on 22 August, 2005, 22:14:04
i just tested it, and it worked perfectly, when did that error happen?
Title:
Post by: Oz on 23 August, 2005, 05:18:53
wgat do u min (when did that error happen?)

i'm make notice that this error show up every minute.

- [07:56] Syntax ...ings\Oz\Desktop\0.3.3.21.nt.dbg\scripts\alarmbot.lua:253: attempt to perform arithmetic on a nil value
- [07:57] Syntax ...ings\Oz\Desktop\0.3.3.21.nt.dbg\scripts\alarmbot.lua:253: attempt to perform arithmetic on a nil value
- [07:58] Syntax ...ings\Oz\Desktop\0.3.3.21.nt.dbg\scripts\alarmbot.lua:253: attempt to perform arithmetic on a nil value
- [07:59] Syntax ...ings\Oz\Desktop\0.3.3.21.nt.dbg\scripts\alarmbot.lua:253: attempt to perform arithmetic on a nil value
- [08:00] Syntax ...ings\Oz\Desktop\0.3.3.21.nt.dbg\scripts\alarmbot.lua:253: attempt to perform arithmetic on a nil value
- [08:01] Syntax
...ings\Oz\Desktop\0.3.3.21.nt.dbg\scripts\alarmbot.lua:253: attempt to perform arithmetic on a nil value

line 253:     local hour = tonumber(hour2) - tonumber(hour1)
Title:
Post by: Dessamator on 23 August, 2005, 19:32:50
Well, try this :

1. Stop the script.
2. Delete fAlarm.dat from ur scripts folder.
3. Start the script.
Title: Re: Reminder or alarm bot
Post by: speedX on 26 August, 2006, 20:08:40
hey Dessamator, nice script...but can u add a clock to it.....i mean wen a user type !time it displays the date & time....
like this
Quote
            Today is 10th August 2006 & Time is 16:16

I kno there is already such script wich displays time...but i would like both in one script only....

thank u...
Title: Re: Reminder or alarm bot
Post by: bastya_elvtars on 29 August, 2006, 11:07:14
Bit OT, but the European style is not that common, in Hungary we use YYYY. MM. DD. (perhaps string.gsub(mydate,"%.%S+","/") whatever).
Title: Re: Reminder or alarm bot
Post by: Dessamator on 25 September, 2006, 21:06:13
Sorry for the late reply, but anyways SpeedX, thats a simple request that can be added, and mutor, yah i noticed it returned a string, trying to convert it to a number was bothering some calculations(if i recall correctly) I havent touched that script in a while so im not even sure of the implications of changing it to a number, anyway I'll check it out.
Title: Re: Reminder or alarm bot
Post by: Dessamator on 27 September, 2006, 23:28:49
Quote from: Mutor on 26 August, 2006, 20:55:22
It will happen when attempting to perform arithmetic on a nil value
once nil error  corrected it will then report
attempt to perform arithmetic on a string value
os.date() returns a string not an integer Dessamator.

Great script but needs a little love.

You could easily convert the time to a number,
tonumber(os.date("%H")..os.date("%M"))
as well as convert the date.

One problem I see in converting the date from a string to an
integer is the European style of date you use, which I never
understood listing the day of the month first.
dd/mm/yy will impede the math although the American style
mm/dd/yy wont, this is why Americans are both cooler and look
better in tight pants.

82606 vs 26806  if you see my point.

Well there was an error with the script but it wasnt really related to arithmetic, SpeedX was using an older version of the script which did indeed have an error, the only error it really had was sending a that the script tried to send a pm to a nick even if it wasnt online.

But anyways for the sake of preventing other bugs i changed it to "Tonumber", and used ur advise on dates.
Title: Re: Reminder or alarm bot
Post by: Dessamator on 28 September, 2006, 00:15:28
Indeed I did use some math logic, but somehow it worked :). I guess it compared the Values of the strings, and if one was indeed bigger than the other the code went along fine, but well, except for the auto cleaner, which checked if one string was greater than the other, the rest of the script's logic was fine, at least it seemed that way to me.
Title: Re: Reminder or alarm bot
Post by: Dessamator on 28 September, 2006, 09:07:04
Well, i believe the string value isnt always really 1 if it contains something, for a simple reason, as the code was before, it would check if a string was greater than the other string.Since neither string was  nil, both would have to be 1 according to your theory, and if that happened the cleaner would never have worked.

Unless it never worked and i didnt notice :).
Title: Re: Reminder or alarm bot
Post by: speedX on 08 October, 2006, 12:51:26
Quote from: Dessamator on 25 September, 2006, 21:06:13
Sorry for the late reply, but anyways SpeedX, thats a simple request that can be added

Well is it done ??
Title: Re: Reminder or alarm bot
Post by: Psycho_Chihuahua on 08 October, 2006, 14:59:55
just add something like

..os.date("Today is %dth %B %Y & the Time is %X")..

wherever you need it in the script
Title: Re: Reminder or alarm bot
Post by: speedX on 08 October, 2006, 16:52:08
hi guys....is it possible to create a reminder wich is of one line(more than one word) instead of only one word ??