HOW-TO: Write your own bot = Lesson 8
 

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 8

Started by pHaTTy, 14 November, 2003, 22:02:25

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

pHaTTy

Ok we will be dealing with tables a bit later on but first gonna add a few things and make this script abit better

from lesson 7 we had:

Bot = "Keiko" 

version = "0.7" 
NEWCON = 1 
prefix = "!"
TimeMins = 5  --set it to 5 mins
SendC = SendToAll

MessageTimer = 1

function Main() 
	frmHub:RegBot(Bot) 
	SetTimer(TimeMins*60000)
	StartTimer()
end 

function OnTimer()
	if MessageTimer == 1 then 
		randomtimer = random(2) --only 2 data's
		if randomtimer == 1 then
			SendC(Bot,"-------------------------------")
			SendC(Bot,"--Write your own bot lesson "..version.."--")
			SendC(Bot,"-------------------------------")
		elseif randomtimer == 2 then
			SendC(Bot,"-----------------------------------------")
			SendC(Bot,"--This lesson bot was created by Phatty--")
			SendC(Bot,"-----------------------------------------")
		end
	end
end

function NewUserConnected(user) 
	if NEWCON == 1 then 
		user:SendData (Bot,"A User has connected") 
	end 
end 

function OpConnected(user) 
Pillock = GetProfileName(user.iProfile)
thisisthevariable = random(5)
	if thisisthevariable == 1 then
		SendC(Bot,user.sName.." has entered the hub") 

	elseif thisisthevariable == 2 then
		SendC(Bot,Pillock.." "..user.sName..",has entered, feel his wrath")  

	elseif thisisthevariable == 3 then
		SendC(Bot,user.sName.." is one of the big bosses, who just entered") 

	elseif thisisthevariable == 4 then
		SendC(Bot,user.sName.." has sneaked into the hub")  

	elseif thisisthevariable == 5 then
		SendC(Bot,user.sName.." has just kicked down the door") 
	end
end



function DataArrival(user,data) 
	if strsub(data, 1, 1) == "<" then 
		data=strsub(data,1,strlen(data)-1) 
		s,e,cmd = strfind(data,"%b<>%s+(%S+)") 
		if cmd == prefix.."version" then 
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty") 
		return 1 
		elseif cmd == prefix.."help" then 
			user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P") 
		return 1 
		elseif cmd == prefix.."death" then 
			user:Disconnect()
			dead = random(5)
			if dead == 1 then
				SendC(Bot,user.sName.." has killed their self, and fell out the hub")
			elseif dead == 2 then
				SendC(Bot,user.sName.." has killed their self, what a pillock")
			elseif dead == 3 then
				SendC(Bot,user.sName.." is a total nutter, they killed em'self")
			elseif dead == 4 then
				SendC(Bot,user.sName.." is now dead, falling from the hub")
			elseif dead == 5 then
				SendC(Bot,user.sName.." jumps a mile high with a rope around their nech and drops down a hole, thats the last we seen of em")
			end
		return 1 
		end 
	end
end

adding a bit randomisation to the death command

function DataArrival(user,data) 
	if strsub(data, 1, 1) == "<" then 
		data=strsub(data,1,strlen(data)-1) 
		s,e,cmd = strfind(data,"%b<>%s+(%S+)") 
		if cmd == prefix.."version" then 
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty") 
		return 1 
		elseif cmd == prefix.."help" then 
			user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P") 
		return 1 
		elseif cmd == prefix.."death" then 
			user:Disconnect()
			dead = random(5)
			if dead == 1 then
				SendC(Bot,user.sName.." has killed their self, and fell out the hub")
			elseif dead == 2 then
				SendC(Bot,user.sName.." has killed their self, what a pillock")
			elseif dead == 3 then
				SendC(Bot,user.sName.." is a total nutter, they killed em'self")
			elseif dead == 4 then
				SendC(Bot,user.sName.." is now dead, falling from the hub")
			elseif dead == 5 then
				SendC(Bot,user.sName.." jumps a mile high with a rope around their nech and drops down a hole, thats the last we seen of em")
			end
		return 1 
		end 
	end
end

now the death command looks to big to be in data arrival, i think im gonna remove it and reassert it in a function

function DataArrival(user,data) 
	if strsub(data, 1, 1) == "<" then 
		data=strsub(data,1,strlen(data)-1) 
		s,e,cmd = strfind(data,"%b<>%s+(%S+)") 
		if cmd == prefix.."version" then 
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty") 
		return 1 
		elseif cmd == prefix.."help" then 
			user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P") 
		return 1 
		elseif cmd == prefix.."death" then 
			deathfunction(user) --the function i will use instead, send the users name with (user)
		return 1 
		end 
	end
end

now to make the function

function deathfunction(pillock) -- reciving name of user from deathfunction(user)
	dead = random(5)
	if dead == 1 then
		SendC(Bot,user.sName.." has killed their self, and fell out the hub")
	elseif dead == 2 then
		SendC(Bot,user.sName.." has killed their self, what a pillock")
	elseif dead == 3 then
		SendC(Bot,user.sName.." is a total nutter, they killed em'self")
	elseif dead == 4 then
		SendC(Bot,user.sName.." is now dead, falling from the hub")
	elseif dead == 5 then
		SendC(Bot,user.sName.." jumps a mile high with a rope around their nech and drops down a hole, thats the last we seen of em")
	end
pillock:Disconnect() --notice that its now pillock, becase the recieve data was not user, it was pillock
end

together we have:

Bot = "Keiko" 

version = "0.7" 
NEWCON = 1 
prefix = "!"
TimeMins = 5  --set it to 5 mins
SendC = SendToAll

MessageTimer = 1

function Main() 
	frmHub:RegBot(Bot) 
	SetTimer(TimeMins*60000)
	StartTimer()
end 

function OnTimer()
	if MessageTimer == 1 then 
		randomtimer = random(2) --only 2 data's
		if randomtimer == 1 then
			SendC(Bot,"-------------------------------")
			SendC(Bot,"--Write your own bot lesson "..version.."--")
			SendC(Bot,"-------------------------------")
		elseif randomtimer == 2 then
			SendC(Bot,"-----------------------------------------")
			SendC(Bot,"--This lesson bot was created by Phatty--")
			SendC(Bot,"-----------------------------------------")
		end
	end
end

function NewUserConnected(user) 
	if NEWCON == 1 then 
		user:SendData (Bot,"A User has connected") 
	end 
end 

function OpConnected(user) 
Pillock = GetProfileName(user.iProfile)
thisisthevariable = random(5)
	if thisisthevariable == 1 then
		SendC(Bot,user.sName.." has entered the hub") 

	elseif thisisthevariable == 2 then
		SendC(Bot,Pillock.." "..user.sName..",has entered, feel his wrath")  

	elseif thisisthevariable == 3 then
		SendC(Bot,user.sName.." is one of the big bosses, who just entered") 

	elseif thisisthevariable == 4 then
		SendC(Bot,user.sName.." has sneaked into the hub")  

	elseif thisisthevariable == 5 then
		SendC(Bot,user.sName.." has just kicked down the door") 
	end
end

function deathfunction(pillock) -- reciving name of user from deathfunction(user)
	dead = random(5)
	if dead == 1 then
		SendC(Bot,user.sName.." has killed their self, and fell out the hub")
	elseif dead == 2 then
		SendC(Bot,user.sName.." has killed their self, what a pillock")
	elseif dead == 3 then
		SendC(Bot,user.sName.." is a total nutter, they killed em'self")
	elseif dead == 4 then
		SendC(Bot,user.sName.." is now dead, falling from the hub")
	elseif dead == 5 then
		SendC(Bot,user.sName.." jumps a mile high with a rope around their nech and drops down a hole, thats the last we seen of em")
	end
pillock:Disconnect()
end



function DataArrival(user,data) 
	if strsub(data, 1, 1) == "<" then 
		data=strsub(data,1,strlen(data)-1) 
		s,e,cmd = strfind(data,"%b<>%s+(%S+)") 
		if cmd == prefix.."version" then 
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty") 
		return 1 
		elseif cmd == prefix.."help" then 
			user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P") 
		return 1 
		elseif cmd == prefix.."death" then 
			deathfunction(user)
		return 1 
		end 
	end
end

now we are going to use a table to filter badwords that are said in the main hub

TheBadWords = { "fuck", "twat", "suck", "fucker" }

now remember for checking tet it has to go in the data arrival as this is where data enters the script

function DataArrival(user,data) 
	if strsub(data, 1, 1) == "<" then 
		data=strsub(data,1,strlen(data)-1) 
		s,e,cmd = strfind(data,"%b<>%s+(%S+)") 
		for key,checkWord in TheBadWords do
			if (strfind(data, checkWord, 1, 1)) then
				return 1
			return 1
			end
		end
		if cmd == prefix.."version" then 
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty") 
		return 1 
		elseif cmd == prefix.."help" then 
			user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P") 
		return 1 
		elseif cmd == prefix.."death" then 
			deathfunction(user)
		return 1 
		end 
	end
end

now there we go this will now work :)

Bot = "Keiko" 

version = "0.8" 
NEWCON = 1 
prefix = "!"
TimeMins = 5  --set it to 5 mins
SendC = SendToAll

MessageTimer = 1

TheBadWords = { "fuck", "twat", "suck", "fucker" }

function Main() 
	frmHub:RegBot(Bot) 
	SetTimer(TimeMins*60000)
	StartTimer()
end 

function OnTimer()
	if MessageTimer == 1 then 
		randomtimer = random(2) --only 2 data's
		if randomtimer == 1 then
			SendC(Bot,"-------------------------------")
			SendC(Bot,"--Write your own bot lesson "..version.."--")
			SendC(Bot,"-------------------------------")
		elseif randomtimer == 2 then
			SendC(Bot,"-----------------------------------------")
			SendC(Bot,"--This lesson bot was created by Phatty--")
			SendC(Bot,"-----------------------------------------")
		end
	end
end

function NewUserConnected(user) 
	if NEWCON == 1 then 
		user:SendData (Bot,"A User has connected") 
	end 
end 

function OpConnected(user) 
Pillock = GetProfileName(user.iProfile)
thisisthevariable = random(5)
	if thisisthevariable == 1 then
		SendC(Bot,user.sName.." has entered the hub") 

	elseif thisisthevariable == 2 then
		SendC(Bot,Pillock.." "..user.sName..",has entered, feel his wrath")  

	elseif thisisthevariable == 3 then
		SendC(Bot,user.sName.." is one of the big bosses, who just entered") 

	elseif thisisthevariable == 4 then
		SendC(Bot,user.sName.." has sneaked into the hub")  

	elseif thisisthevariable == 5 then
		SendC(Bot,user.sName.." has just kicked down the door") 
	end
end

function deathfunction(pillock) -- reciving name of user from deathfunction(user)
	dead = random(5)
	if dead == 1 then
		SendC(Bot,user.sName.." has killed their self, and fell out the hub")
	elseif dead == 2 then
		SendC(Bot,user.sName.." has killed their self, what a pillock")
	elseif dead == 3 then
		SendC(Bot,user.sName.." is a total nutter, they killed em'self")
	elseif dead == 4 then
		SendC(Bot,user.sName.." is now dead, falling from the hub")
	elseif dead == 5 then
		SendC(Bot,user.sName.." jumps a mile high with a rope around their nech and drops down a hole, thats the last we seen of em")
	end
pillock:Disconnect()
end


function DataArrival(user,data) 
	if strsub(data, 1, 1) == "<" then 
		data=strsub(data,1,strlen(data)-1) 
		s,e,cmd = strfind(data,"%b<>%s+(%S+)") 
		for key,checkWord in TheBadWords do
			if (strfind(data, checkWord, 1, 1)) then
				return 1
			return 1
			end
		end
		if cmd == prefix.."version" then 
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty") 
		return 1 
		elseif cmd == prefix.."help" then 
			user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P") 
		return 1 
		elseif cmd == prefix.."death" then 
			deathfunction(user)
		return 1 
		end 
	end
end

next on next post, not enough space
Resistance is futile!

pHaTTy

now that was checking a table, now we are going to store data in a table, we will accomplish this by the badwords, we will count each badword that is said by the users and after 3 times it will disconnect them

BadWord={}


and now the code

function DataArrival(user,data) 
	if strsub(data, 1, 1) == "<" then 
		data=strsub(data,1,strlen(data)-1) 
		s,e,cmd = strfind(data,"%b<>%s+(%S+)") 
		for key,checkWord in TheBadWords do
			if (strfind(data, checkWord, 1, 1)) then
				if BadWord[user.sName]==nil then
					BadWord[user.sName]=0
				elseif BadWord[user.sName]== 3 then
					user:Disconnect()
				else
					BadWord[user.sName]=BadWord[user.sName]+1
				end
			return 1
			end
		end
		if cmd == prefix.."version" then 
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty") 
		return 1 
		elseif cmd == prefix.."help" then 
			user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P") 
		return 1 
		elseif cmd == prefix.."death" then 
			deathfunction(user)
		return 1 
		end 
	end
end

ok now we have :

Bot = "Keiko" 

version = "0.8" 
NEWCON = 1 
prefix = "!"
TimeMins = 5  --set it to 5 mins
SendC = SendToAll

MessageTimer = 1
BadWord={}

TheBadWords = { "fuck", "twat", "suck", "fucker" }

function Main() 
	frmHub:RegBot(Bot) 
	SetTimer(TimeMins*60000)
	StartTimer()
end 

function OnTimer()
	if MessageTimer == 1 then 
		randomtimer = random(2) --only 2 data's
		if randomtimer == 1 then
			SendC(Bot,"-------------------------------")
			SendC(Bot,"--Write your own bot lesson "..version.."--")
			SendC(Bot,"-------------------------------")
		elseif randomtimer == 2 then
			SendC(Bot,"-----------------------------------------")
			SendC(Bot,"--This lesson bot was created by Phatty--")
			SendC(Bot,"-----------------------------------------")
		end
	end
end

function NewUserConnected(user) 
	if NEWCON == 1 then 
		user:SendData (Bot,"A User has connected") 
	end 
end 

function OpConnected(user) 
Pillock = GetProfileName(user.iProfile)
thisisthevariable = random(5)
	if thisisthevariable == 1 then
		SendC(Bot,user.sName.." has entered the hub") 

	elseif thisisthevariable == 2 then
		SendC(Bot,Pillock.." "..user.sName..",has entered, feel his wrath")  

	elseif thisisthevariable == 3 then
		SendC(Bot,user.sName.." is one of the big bosses, who just entered") 

	elseif thisisthevariable == 4 then
		SendC(Bot,user.sName.." has sneaked into the hub")  

	elseif thisisthevariable == 5 then
		SendC(Bot,user.sName.." has just kicked down the door") 
	end
end

function deathfunction(pillock) -- reciving name of user from deathfunction(user)
	dead = random(5)
	if dead == 1 then
		SendC(Bot,user.sName.." has killed their self, and fell out the hub")
	elseif dead == 2 then
		SendC(Bot,user.sName.." has killed their self, what a pillock")
	elseif dead == 3 then
		SendC(Bot,user.sName.." is a total nutter, they killed em'self")
	elseif dead == 4 then
		SendC(Bot,user.sName.." is now dead, falling from the hub")
	elseif dead == 5 then
		SendC(Bot,user.sName.." jumps a mile high with a rope around their nech and drops down a hole, thats the last we seen of em")
	end
pillock:Disconnect()
end


function DataArrival(user,data) 
	if strsub(data, 1, 1) == "<" then 
		data=strsub(data,1,strlen(data)-1) 
		s,e,cmd = strfind(data,"%b<>%s+(%S+)") 
		for key,checkWord in TheBadWords do
			if (strfind(data, checkWord, 1, 1)) then
				if BadWord[user.sName]==nil then
					BadWord[user.sName]=0
				elseif BadWord[user.sName]== 3 then
					user:Disconnect()
				else
					BadWord[user.sName]=BadWord[user.sName]+1
				end
			return 1
			end
		end
		if cmd == prefix.."version" then 
			user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty") 
		return 1 
		elseif cmd == prefix.."help" then 
			user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P") 
		return 1 
		elseif cmd == prefix.."death" then 
			deathfunction(user)
		return 1 
		end 
	end
end

now in the next lesson we will probablly be looking at parsing text and a bit more on tables ;)

l8rr,,

-phatz
Resistance is futile!

pHaTTy

someone finally spotted the error hahaha

pillock = user

pillock:Disconnect() ahahahah

user:Disconnect()



well done SaintSinner
Resistance is futile!

plop

there is another bug.
what happens when a user says 3x a bad word and comes back in the hub and says 1 more bad word ???

plop
http://www.plop.nl lua scripts/howto\'s.
http://www.thegoldenangel.net
http://www.vikingshub.com
http://www.lua.org

>>----> he who fights hatred with hatred, drives the spreading of hatred <----<<

Intel

i know i am weeks late to this lesson but...cant we say
user:Kick("Too many Bad Words")
Punnakku

((KMN))Gazza-95

phatty i have done all the lessons so far and the script has come out in lua 3 and i need it lua 5 is there any way i can change that

Markitos

Quote from: ((KMN))Gazza-95 on 21 April, 2006, 23:03:33
phatty i have done all the lessons so far and the script has come out in lua 3 and i need it lua 5 is there any way i can change that

yes...check this example and visit www.lua.org
--[[

HOW-TO: Write your own bot lesson 8
Optimized by Markitos:
-- Lua 5/5.1 Compatible
-- Use of string.rep ------->>>> string.rep (s, n) Returns a string made of n concatenated copies of string s.

]]--

Bot = "Keiko"

version = "0.81"
NEWCON = 1
TimeMins = 5  --set it to 5 mins
SendC = SendToAll

MessageTimer = 1
BadWord={}

TheBadWords = { "fuck", "twat", "suck", "fucker" }

function Main()
	frmHub:RegBot(Bot)
	SetTimer(TimeMins*60000)
	StartTimer()
end

function OnTimer()
	if MessageTimer == 1 then
		randomtimer = math.random(2) --only 2 data's
		if randomtimer == 1 then
			SendC(Bot,""..string.rep("-",15).."")
			SendC(Bot,"--Write your own bot lesson "..version.."--")
			SendC(Bot,""..string.rep("-",15).."")
		elseif randomtimer == 2 then
			SendC(Bot,""..string.rep("-",15).."")
			SendC(Bot,"--This lesson bot was created by Phatty--")
			SendC(Bot,""..string.rep("-",15).."")
		end
	end
end

function NewUserConnected(user)
	if NEWCON == 1 then
		user:SendData (Bot,"A User has connected")
	end
end

function OpConnected(user)
	Pillock = GetProfileName(user.iProfile)
	thisisthevariable = math.random(5)
	if thisisthevariable == 1 then
		SendC(Bot,user.sName.." has entered the hub")
	elseif thisisthevariable == 2 then
		SendC(Bot,Pillock.." "..user.sName..",has entered, feel his wrath")  
	elseif thisisthevariable == 3 then
		SendC(Bot,user.sName.." is one of the big bosses, who just entered")
	elseif thisisthevariable == 4 then
		SendC(Bot,user.sName.." has sneaked into the hub")  
	elseif thisisthevariable == 5 then
		SendC(Bot,user.sName.." has just kicked down the door")
	end
end

function deathfunction(user) -- reciving name of user from deathfunction(user)
	dead = math.random(5)
	if dead == 1 then
		SendC(Bot,user.sName.." has killed their self, and fell out the hub")
	elseif dead == 2 then
		SendC(Bot,user.sName.." has killed their self, what a pillock")
	elseif dead == 3 then
		SendC(Bot,user.sName.." is a total nutter, they killed em'self")
	elseif dead == 4 then
		SendC(Bot,user.sName.." is now dead, falling from the hub")
	elseif dead == 5 then
		SendC(Bot,user.sName.." jumps a mile high with a rope around their nech and drops down a hole, thats the last we seen of em")
	end
	user:Disconnect()
end


function ChatArrival(user,data)
	if string.sub(data, 1, 1) == "<" then
		data=string.sub(data,1,string.len(data)-1)
		s,e,cmd = string.find(data,"%b<>%s+[%!%+](%S+)")
		for key,checkWord in ipairs(TheBadWords) do
			if (string.find(data, checkWord, 1, 1)) then
				if BadWord[user.sName]==nil then
					BadWord[user.sName]=0
				elseif BadWord[user.sName]== 3 then
					user:Disconnect()
				else
				BadWord[user.sName]=BadWord[user.sName]+1
				end
				return 1
			end
		end
		if cmd == "version" then
			return user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty"),1
		elseif cmd == "help" then
			return user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P"),1
		elseif cmd == "death" then
			return deathfunction(user),1
		end
	end
end

6Marilyn6Manson6

function ChatArrival(user,data)
	data=string.sub(data,1,string.len(data)-1)
	s,e,cmd = string.find(data,"%b<>%s+[%!%+](%S+)")
	for key,checkWord in ipairs(TheBadWords) do
		if (string.find(data, checkWord, 1, 1)) then
			if BadWord[user.sName]==nil then
				BadWord[user.sName]=0
			elseif BadWord[user.sName]== 3 then
				user:Disconnect()
			else
				BadWord[user.sName]=BadWord[user.sName]+1
			end
			return 1
		end
	end
	if cmd == "version" then
		return user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty"),1
	elseif cmd == "help" then
		return user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P"),1
	elseif cmd == "death" then
		return deathfunction(user),1
	end
end


This function is most correct :D

Markitos

#8
Quote from: Mutor on 22 April, 2006, 01:41:51
Quote from: 6Marilyn6Manson6 on 22 April, 2006, 00:04:03
This function is most correct :D

Is it?

Some obversavations, and merely just pointing out the choices we have...

Tables are more efficient than if elseif...
I think a table is best here if more than a 1 or 2 elseifs

String subbing..
data=string.sub(data,1,string.len(data)-1)
Why string.sub unless your adjusting the string, the line above has no effect
It simply represents the orifinal data.

s,e,cmd = string.find(data,"%b<>%s+[%!%+](%S+)")
If you dont need a global variable here, why have one? local s,e,cmd...
As for the pattern, %b<>%s+
Do you expect more than a single space, this will match one or an infinite number of space chars here
Consider %b<>%s[%!...    or use a space   %b<> [%!...
more on the pattern...
+[%!%+](%S+)"
This is fine if you do want to restrict the prefix to just these charachters.
Now for the command name itself, is it really ok to be any non space charachter?
If you dont mind, its fine, but I'd rather specify, and write that pattern as...
"%b<>%s%p(%w+)"


food for thought...



Yes i agree with you...tables are more eficient but i was off to bed so did a quick djob  ;D
And thanks for the hint about pattern matching.

Going to rewrite...

Edited// I rewrote the ChatArrival but it gives me an error
--[[

HOW-TO: Write your own bot lesson 8
Optimized by Markitos:
-- Lua 5/5.1 Compatible
-- Use of string.rep ------->>>> string.rep (s, n) Returns a string made of n concatenated copies of string s.

]]--

Bot = "Keiko"

version = "0.81"
NEWCON = 1
TimeMins = 5  --set it to 5 mins
SendC = SendToAll

MessageTimer = 1
BadWord={}

TheBadWords = { "fuck", "twat", "suck", "fucker" }

function Main()
frmHub:RegBot(Bot)
SetTimer(TimeMins*60000)
StartTimer()
end

function OnTimer()
	if MessageTimer == 1 then
		randomtimer = math.random(2) --only 2 data's
		if randomtimer == 1 then
			SendC(Bot,""..string.rep("-",15).."")
			SendC(Bot,"--Write your own bot lesson "..version.."--")
			SendC(Bot,""..string.rep("-",15).."")
		elseif randomtimer == 2 then
			SendC(Bot,""..string.rep("-",15).."")
			SendC(Bot,"--This lesson bot was created by Phatty--")
			SendC(Bot,""..string.rep("-",15).."")
		end
	end
end

function NewUserConnected(user)
	if NEWCON == 1 then
		user:SendData (Bot,"A User has connected")
	end
end

function OpConnected(user)
	Pillock = GetProfileName(user.iProfile)
	thisisthevariable = math.random(5)
	if thisisthevariable == 1 then
		SendC(Bot,user.sName.." has entered the hub")
	elseif thisisthevariable == 2 then
		SendC(Bot,Pillock.." "..user.sName..",has entered, feel his wrath") 
	elseif thisisthevariable == 3 then
		SendC(Bot,user.sName.." is one of the big bosses, who just entered")
	elseif thisisthevariable == 4 then
		SendC(Bot,user.sName.." has sneaked into the hub") 
	elseif thisisthevariable == 5 then
		SendC(Bot,user.sName.." has just kicked down the door")
	end
end

function deathfunction(user) -- reciving name of user from deathfunction(user)
	dead = math.random(5)
	if dead == 1 then
		SendC(Bot,user.sName.." has killed their self, and fell out the hub")
	elseif dead == 2 then
		SendC(Bot,user.sName.." has killed their self, what a pillock")
	elseif dead == 3 then
		SendC(Bot,user.sName.." is a total nutter, they killed em'self")
	elseif dead == 4 then
		SendC(Bot,user.sName.." is now dead, falling from the hub")
	elseif dead == 5 then
		SendC(Bot,user.sName.." jumps a mile high with a rope around their nech and drops down a hole, thats the last we seen of em")
	end
	user:Disconnect()
end


function ChatArrival(user,data)
	local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")
	if cmd then
		local tCmds = {
			["version"] = function(user)
				user:SendData(Bot,"This bot is Learn to write a bot version: "..version.." by Phatty")
			end,
			["help"] = function(user)
				user:SendData(Bot,"This is where the help text wud go, bt im not gonna waste my time :P")
			end,
			["death"] = function(user)
				deathfunction(user)
			end,
		}
		if tCmds[cmd] then
			return tCmds[cmd](user),1
		end
		for key,checkWord in ipairs(TheBadWords) do
			if (string.find(data, checkWord, 1, 1)) then
				if BadWord[user.sName]==nil then
					BadWord[user.sName]=0
				elseif BadWord[user.sName]== 3 then
					user:Disconnect()
				else
					BadWord[user.sName]=BadWord[user.sName]+1
				end
				return 1
			end
		end
	end
end

[10:58]Ptokax 0.3.4.0f0.dbg\scripts\test.lua:89: `}' expected (to close `{' at line 85) near `['

Markitos

#9
Is there anything else to short in the script?

Markitos

Quote from: Mutor on 22 April, 2006, 16:23:38
I dont understand your question, please rephrase.

For what its worth I'd been using this command format in a global commands table...

tCmds = {
	cmdname = function(user,data)
		if user then
		        --DoStuff
		else
			return "Description",cmdarg1,cmdarg2
		end
	end,
}


It allows for one function to exec a command, return a description [for use in script help] or
to generate context menus (right click)
As a global table, I can also call the commands from other functions like this  to execute:
tCmds.cmdname(user)
or like this to return Description and args:
tCmds.cmdname()
Indeed...i saw that kind of code in your scripts anyway thanks for the help  ;)

((KMN))Gazza-95


Markitos

Quote from: ((KMN))Gazza-95 on 23 April, 2006, 20:35:15
MARKITOS IT STILL SEZ LUA 3
Lua 3??? I converted it to lua5/5.1...
There was an issue alright but its already solved.

bastya_elvtars

Everything could have been anything else and it would have just as much meaning.

PPK

"Most of you are familiar with the virtues of a programmer. There are three, of course: laziness, impatience, and hubris." - Larry Wall


speedX

#16
Hello guys,
If I want to add tables means I want some commands wich will be used by specific profile nos. only, thn wat and whr shud I add.

And this small Bot I tried to make, but its not working :(
Bot = "HOPS"


function ChatArrival(user,data)
	local s,e,cmd = string.find(data,"%b<>%s%p(%w+)")
	if cmd then
          local tCmds = {
                    ["myip"] = function(user)
                          user:SendData(Bot,"Your IP is: "..user.sIP)
                    end
          }
  end
end

Thx for any help
Thanking You,

speedX

speedX

Thx Mutor, If I want only specific profile nos. to use the command thn wat shud I do? also If i want all to see the command thn wat shud be code??
Thx for any help
Thanking You,

speedX

Naithif

Hey Mutor, since this is a tutorial page, I wanted to ask something for some time now - What's the difference between reading commands from table and reading commands directly?
From what I saw it's the same, but you need to write more with a table :S
Is it faster or eats less mem?  ???

if cmd then
			local tCmds = {
			["myip"] = function()
				...
			end


if cmd == "myip" then
			...
		end

speedX

Quote from: Mutor on 23 November, 2006, 15:56:20
It should seem obvious to you that you will need to the user's profile.
If I would have known scripting, thn I would have never asked tht question....

Quote from: Mutor on 23 November, 2006, 15:56:20
Do you do any research? This type of code is built into more than half
of every script written for PtokaX since the dawn of time. Or would you
rather pose such questions and wait until someone writes the code for you?
Its not tht Sir, as all scripters have different style of writing so newbie's like us find it difficult to understand wat a particular command does.

Anyways thx for all the help, and sorry if I was rude......
Thanking You,

speedX

Naithif

Quote from: Mutor on 23 November, 2006, 17:12:02
...
A table can be looped or hashed and functions within that table
may be called again and again saving you redundant code.
...

Hashed?  :o
There are always things to learn (and there are some surprising ones)  :D
Thanks Mutor, I'll experiment with these thingies to see what's up with them

SMF spam blocked by CleanTalk