property nm : 0
property rn2 : 0
property tot2 : 0
property txt : “”
property tot : 0
property array1 : {“Who wants a body massage?”, “mmmm mmmm body massage”, “I like to ride my bicycle.”, “We have plenty of towels, thanks.”}
property valid1 : false
on pubmsg(con, source, sourcehost, target, thestring)
if valid1 is false then
set rn2 to random number from 1 to 2
set valid1 to true
tell application "Snak 4.9" to type "_1 Completed"
else
if thestring contains "a" or thestring contains "e" or thestring contains "i" or thestring contains "o" or thestring contains "u" or thestring contains "y" then
if tot is less than rn2 and valid1 is true then
set tot to tot + 1
tell application "Snak 4.9" to type "_2 Completed"
else
if tot is equal to rn2 and valid1 is true then
if tot is equal to rn2 or tot is greater than rn2 then
set valid1 to false
set tot to 0
_reply(con, source, sourcehost, target, thestring)
end if
end if
end if
end if
end if
end pubmsg
on _reply(con, source, sourcehost, target, thestring)
tell application “Snak 4.9”
set nm to random number from 1 to (count items of array1)
[b]type (item nm of array1) in (channel target of connection con)[/b]
end tell
end _reply
The part in bold is where the error is coming from, I can’t spot any problems and I have code (in other scripts) nearly like this that works fine.
Without knowing the specific error message you get (you didn’t say), it’s hard to tell for sure, but my first guess is the problem lies somewhere in coercing the array item to a string.
Snak expects a string object to type in the channel not a reference to an array item.
Try this instead (also, not the optimization of selecting a random item from a list):
on _reply(con, source, sourcehost, target, thestring)
tell application "Snak 4.9"
set replyText to (some item of array1) as string
type replyText in channel target of connection con
end tell
end _reply
All of the help offered here is offered by other scripters who owe you nothing. Maybe everyone is busy attending to their own needs. Maybe you haven’t presented the problem in a way that others can understand. Maybe you haven’t provided enough info. Maybe you need to rephrase the problem/question. Maybe, if you don’t get pissy, you’ll get the help that you obviously feel is owed to you.
I’m sorry for getting agitated but this is the second time this week that someone has taken a “you owe me something” attitude towards the very people who are attempting to help.
I certainly don’t understand the problem and the code you posted isn’t very helpful because you just posted the handlers, not the calls to the handlers with the loaded parameters (what are “con, source, sourcehost, target, thestring”?) – very difficult to troubleshoot your problem this way.
con - connection name (dalnet…)
source - the name of the person who wrote it
sourcehost - their hostmask 66.etc…
target - target is…wherever the text was written (almost like channel)
thestring - whatever was written to trigger the event
Okaaaay…and what exactly are you trying to do? From what I can glean, when a message comes in to you, you set a flag from false to true and set a random number to either 1 or 2 (why?) and type “_1 Completed” (what does this signify?) On subsequent calls to the handler, you check to make sure the text contains a vowel (why?) and check how many times it’s been called since it was initialized (either 1 or 2) which is random because you are checking against the random 1 or 2 you set above (why?) and either type “_2 Completed” (significance?) or you type something random from a senseless list of strings and reset everything.
Apart from all that, your code seems sound. I don’t see how you could possibly get an error “Can’t get item 0 of {}” since the “0” is a random number from 1 to 4 and the “{}” is your list of inane strings unless your list of strings (“array1”) is actually empty (i.e., {}). And why do you list the properties “tot2” and “txt” when they are not used at all? Can you give more information about your system? Can you try logging out and back in and trying it again?
Is I am coding together an irc bot (already many scripts done), and my latest addition is going to be every random amount of lines (1-2 for testing / but probably 20 - 50 once it works) it will display a random message quote.
on pubmsg()
That’s when somone (anyone in the channel types a public message) it calls the actions list it.
The first event sets the random lines. It displays “_1 So I know it worked properly”
The second event adds to a total on once it hits that random value it triggers the reply function. It gives me the “_2 So I know its completed”
Now I still haven’t found the magic code to fix my problem, but I have come up with new code (doesn’t crash Snak as often).
property nm : 0
property rn2 : 0
property tot2 : 0
property txt : "Yea, the french will do that to you."
property tot : 0
property array1 : {}
property valid1 : false
-- {"Who wants a body massage?", "mmmm mmmm body massage", "I like to ride my bicycle.", "We have plenty of towels, thanks."}
set end of array1 to txt as string
on pubmsg(con, source, sourcehost, target, thestring)
if valid1 is false then
set rn2 to random number from 1 to 2
set valid1 to true
tell application "Snak 4.9" to type "_1 Completed"
else
if thestring contains "a" or thestring contains "e" or thestring contains "i" or thestring contains "o" or thestring contains "u" or thestring contains "y" then
if tot is less than rn2 and valid1 is true then
set tot to tot + 1
tell application "Snak 4.9" to type "_2 Completed"
else
if tot is equal to rn2 and valid1 is true then
if tot is equal to rn2 or tot is greater than rn2 then
set valid1 to false
set tot to 0
try
_reply(con, source, sourcehost, target, thestring)
on error errmsg number errNum
display dialog "ERROR: " & errNum & " :: " & errmsg
end try
end if
end if
end if
end if
end if
end pubmsg
on _reply(con, source, sourcehost, target, thestring)
tell application "Snak 4.9"
set nm to random number from 1 to (count items of array1)
type (item nm of array1) in (channel target of connection con)
end tell
end _reply
--set nm to random number from 1 to (count items of array1)
--type (item nm of array1) in (channel target of connection con)
I have a few things added into there for small testing purposes.
property array1 : {"Who wants a body massage?", "mmmm mmmm body massage", "I like to ride my bicycle.", "We have plenty of towels, thanks."}
on pubmsg(con, target)
tell application "Snak 4.9" to type (some item of array1) in (channel target of connection con)
end pubmsg