Author Topic: HOW-TO: Write your own bot = Lesson 4  (Read 10940 times)

0 Members and 1 Guest are viewing this topic.

Offline Naithif

  • Triple Ace
  • **
  • Posts: 199
  • Karma: +32/-13
Re: HOW-TO: Write your own bot = Lesson 4
« Reply #25 on: 13 November, 2006, 17:37:09 »
Try this
Haven't tested

Code: [Select]
Clock = 60000 --(millisec)
Bot = frmHub:GetHubBotName() --or write a nick for it in quotation marks
Fileloc = "scripts/anything.txt" --the name of the file to post (inside the PtokaX directory)

function Main()
SetTimer(Clock)
StartTimer()
end

OnTimer = function()

file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
else
SendToOps(Bot,"Error, specified file ("..Fileloc..") can't be found!")
end

end

A little modification for the file to display if it isn't exist
« Last Edit: 13 November, 2006, 17:41:23 by Naithif »

PtokaX forum

Re: HOW-TO: Write your own bot = Lesson 4
« Reply #25 on: 13 November, 2006, 17:37:09 »

Herodes

  • Guest
Re: HOW-TO: Write your own bot = Lesson 4
« Reply #26 on: 13 November, 2006, 17:43:07 »
if I want to show up a text file??
I mean if I have a txt file with command +bla
And I want to display the data in tht txt file in main, thn wat to do??

have you checked the Options/Text Files/Enable Text files support in the GUI?

Offline speedX

  • Lord
  • ***
  • Posts: 437
  • Karma: +59/-19
Re: HOW-TO: Write your own bot = Lesson 4
« Reply #27 on: 13 November, 2006, 17:47:17 »
Yup working, thx for the help Naithif (again)

But if you have time thn could you plzz explain tht OnTimer function??
Posted on: 13 November 2006, 16:46:02
have you checked the Options/Text Files/Enable Text files support in the GUI?

hey I'm not tht a big newbie :P
I just want the text file data to come in Main at constant intervals ;)
Thanking You,

speedX

Offline Naithif

  • Triple Ace
  • **
  • Posts: 199
  • Karma: +32/-13
Re: HOW-TO: Write your own bot = Lesson 4
« Reply #28 on: 13 November, 2006, 18:07:47 »
Ontimer can be described as tutorial as the following:

First we need to set the clock
Setting the clock
Code: [Select]
SetTimer(X)Where X can be a variable defined previously or an integer.
If you want it to be definable use a variable here like
Code: [Select]
SetTimer(Clock)
'Clock' is the variable it can be given at the start of the script.

We need an event to start the clock (until then it will not give any timed events)
Starting
Code: [Select]
StartTimer()
And we need a "timer tick" option that happens when the timer has all criterias, started, and the given time passed since start (or the previous tick)
(It's a function notice that)
Timer tick
Code: [Select]
OnTimer = function()
If you want the timer to set & start immediately when you start the script you can add the start command to 'Main' function (that runs at script start)

Code: [Select]
function Main()
SetTimer(Clock)
StartTimer()
end

Now, you need to define the events at each tick
Event
Code: [Select]
OnTimer = function()

file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
else
SendToOps(Bot,"Error, specified file ("..Fileloc..") can't be found!")
end

end

This is a separate function.
Anything between the first line 'OnTimer = function()' and the 'end' word gets executed when the timer's started at each tick repeatedly until stopped by 'StopTimer()'

This can be used for repeated things, timed events, or giving a delay to an event
For example, when you start the clock and stop it in the repeated action then the performed event only gets a delay
Delaying
Code: [Select]
Clock = 60000 --(millisec)
Bot = frmHub:GetHubBotName()

function Main()
SetTimer(Clock)
StartTimer()
end

OnTimer = function()

SendToAll(Bot, "This is a delayed message. 60 seconds passed since hub start")
StopTimer()

end

We've stopped it in 'OnTimer' function, so it'll only execute once, delayed with the 'Clock' variable
You can do this to delay something when someone types a command or from the script's start, or any other event happens

Shortly LOL
Cheers

Offline speedX

  • Lord
  • ***
  • Posts: 437
  • Karma: +59/-19
Re: HOW-TO: Write your own bot = Lesson 4
« Reply #29 on: 13 November, 2006, 18:15:40 »
Hey thx Naithif now tht time thing would get out of my mind ;) , this tutorials has been a gr8 help for me

Wat does this little code do? could u explain plzz
Code: [Select]
file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
[code]
[/code]
Thanking You,

speedX

Offline Naithif

  • Triple Ace
  • **
  • Posts: 199
  • Karma: +32/-13
Re: HOW-TO: Write your own bot = Lesson 4
« Reply #30 on: 13 November, 2006, 18:39:48 »
Hey thx Naithif now tht time thing would get out of my mind ;) , this tutorials has been a gr8 help for me

Wat does this little code do? could u explain plzz
Code: [Select]
file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
[code]
[/code]

It's --part of-- opening a text file ;)

Code: [Select]
file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
if file then
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
SendToAll(Bot,info)
file:close()
else
SendToOps(Bot,"Error, specified file ("..Fileloc..") can't be found!")
end

File reading
We give a variable 'file' equal to opening a file at PtokaX's location followed by scripts folder and filename for reading

file = io.open(frmHub:GetPtokaXLocation()..Fileloc, "r")
        /\                     /\                           /\     /\
        /                      /                            /        \  for reading
------                 ----                        ----           ------------               
open the file  -> Your hub's location   +  Folder+file

So it would come out as
Example
'H:\Hub\PtokaX\' (Hub's location) + 'scripts\anything.txt' (defined 'Fileloc' variable - folder + file)
Becomes
'H:\Hub\PtokaX\scripts\anything.txt'

Gathering the data and replacements
Then gather the read data for one local variable 'info'
Code: [Select]
local info = string.gsub(file:read("*a"),string.char(10), "\r\n")
Gather data to a local variable which becomes everything in the file except 'enter' character which will be output as "\r\n"
"\r\n" is the linebreaking inside scripts "\t" inserts a tab into the message
Example
Code: [Select]
info="\r\n\t\t\t Item1 \r\n\t\t\t Item2"Notice that "\r\n\t" are INSIDE the " " -s

Sending and closing
Code: [Select]
SendToAll(Bot,info)Send the gathered data by 'Bot' to everyone

Code: [Select]
file:close()Close the file

Existence of the file
The 'if file'/'else' is given because the file can be an error source when you rename it or something, and you can make the script to report it

'If file then' means if the file exists (file doesn't equal nil - a non existing file's output is nil)
'else' other case - file doesn't exists

A solution was to report it to give the Fileloc variable because it describes the folder and the file, making it easy to filter this error


« Last Edit: 13 November, 2006, 18:48:05 by Naithif »

PtokaX forum

Re: HOW-TO: Write your own bot = Lesson 4
« Reply #30 on: 13 November, 2006, 18:39:48 »