This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

Ignore certain nicks (random reply script)

Help for those learning Tcl or writing their own scripts.
Post Reply
x
x0x
Op
Posts: 140
Joined: Tue Feb 10, 2009 6:42 am

Ignore certain nicks (random reply script)

Post by x0x »

This script replies to certain words (wildcard) with a random message. I would like to have it ignore a couple of nicknames tho. How to add this?

Code: Select all

bind pubm - *word* pub_random

proc pub_random {nick mask hand channel args} {
   global randommessage
   putquick "PRIVMSG $channel :[lindex $randommessage [rand [llength $randommessage]]]"
   }

set randommessage {
"Message 1"
"Message 2"
"Message 3"
}

putlog "Random Reply Script Loaded"
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Try this

Code: Select all

bind pubm - *word* pub_random

set temp(ignore) {
	"nick1"
	"nick2"
}

proc pub_random {nick mask hand channel args} {
	global randommessage temp

	foreach n $temp(ignore) { if {[string tolower $nick] == [string tolower $n]} { return } }

	putquick "PRIVMSG $channel :[lindex $randommessage [rand [llength $randommessage]]]"
}

set randommessage {
	"Message 1"
	"Message 2"
	"Message 3"
}

putlog "Random Reply Script Loaded"
x
x0x
Op
Posts: 140
Joined: Tue Feb 10, 2009 6:42 am

Post by x0x »

Works great! Thank you!
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

There no need to loop inside a list when you have list functions specially made for this kind of operations like lsearch that will "see if a list contains a particular element".

Also, to end a loop you should use break not return. There's no need for two transformations of strings in to lower format if you would have used string match -nocase for instance.

Anyway, the best option would have been a lsearch -nocase like this:

Code: Select all

if {[lsearch -nocase $temp(ignore) $nick]} return
instead of the foreach line.
Once the game is over, the king and the pawn go back in the same box.
x
x0x
Op
Posts: 140
Joined: Tue Feb 10, 2009 6:42 am

Post by x0x »

So how would the script look like if you add this fix?
User avatar
caesar
Mint Rubber
Posts: 3778
Joined: Sun Oct 14, 2001 8:00 pm
Location: Mint Factory

Post by caesar »

Replace the foreach line with the one I mentioned? :)
Once the game is over, the king and the pawn go back in the same box.
User avatar
Madalin
Master
Posts: 310
Joined: Fri Jun 24, 2005 11:36 am
Location: Constanta, Romania
Contact:

Post by Madalin »

Replace
foreach n $temp(ignore) { if {[string tolower $nick] == [string tolower $n]} { return } }
with
if {[lsearch -nocase $temp(ignore) $nick]} return
or using my version
foreach n $temp(ignore) { if {[string match -nocase $nick $n]} { break } }
This last version is better than the first i made (now its using string match and break) the secod is caesar idea
Post Reply