HOW-TO: Write your own bot = Lesson 4 - Page 2
 

News:

29 December 2022 - PtokaX 0.5.3.0 (20th anniversary edition) released...
11 April 2017 - PtokaX 0.5.2.2 released...
8 April 2015 Anti child and anti pedo pr0n scripts are not allowed anymore on this board!
28 September 2015 - PtokaX 0.5.2.1 for Windows 10 IoT released...
3 September 2015 - PtokaX 0.5.2.1 released...
16 August 2015 - PtokaX 0.5.2.0 released...
1 August 2015 - Crowdfunding for ADC protocol support in PtokaX ended. Clearly nobody want ADC support...
30 June 2015 - PtokaX 0.5.1.0 released...
30 April 2015 Crowdfunding for ADC protocol support in PtokaX
26 April 2015 New support hub!
20 February 2015 - PtokaX 0.5.0.3 released...
13 April 2014 - PtokaX 0.5.0.2 released...
23 March 2014 - PtokaX testing version 0.5.0.1 build 454 is available.
04 March 2014 - PtokaX.org sites were temporary down because of DDOS attacks and issues with hosting service provider.

Main Menu

HOW-TO: Write your own bot = Lesson 4

Started by pHaTTy, 25 October, 2003, 14:55:06

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Naithif

#25
Try this
Haven't tested

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

Herodes

Quote from: speedX on 13 November, 2006, 17:31:08
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?

speedX

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
Quote from: Herodes on 13 November, 2006, 17:43:07
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

Naithif

Ontimer can be described as tutorial as the following:

First we need to set the clock
Setting the clock
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
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
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
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)

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


Now, you need to define the events at each tick
Event
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
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

speedX

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
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

Naithif

#30
Quote from: speedX 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
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 ;)

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'
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
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
SendToAll(Bot,info)

Send the gathered data by 'Bot' to everyone

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



SMF spam blocked by CleanTalk