| View previous topic :: View next topic |
| Author |
Message |
Gemster Halfop
Joined: 04 Oct 2010 Posts: 51
|
Posted: Sat Feb 26, 2011 9:13 pm Post subject: If statment for command only on that channel ? |
|
|
Hi,
had a look through a links that i have bookmarked from willyw
Anyways im looking for an if statment where a command can only responded to on a channel.
for example if a user types .help in the channel #help the bot will respond but if they type .help in another channel like #lobby it wont respond.
Thanks
Gemster |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sat Feb 26, 2011 10:46 pm Post subject: Re: If statment for command only on that channel ? |
|
|
| Gemster wrote: |
...
for example if a user types .help in the channel #help the bot will respond but if they type .help in another channel like #lobby it wont respond.
|
Here's a rough idea (not tested):
| Code: |
bind pub - ".help" foo
proc foo {nick uhost handle chan text} {
if {"$chan" != "#help"} {
return
}
# whatever other commands you want go here
}
|
Even though I didn't test it for errors, I bet you get the idea.  |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sun Feb 27, 2011 5:13 am Post subject: |
|
|
Since FOO is not equal with foo you have to either use 'string tolower' on $chan where all upper (or title) case letters are converted to lower case OR 'string equal -nocase' where the strings are compared in a case-insensitive manner.
Examples:
| Code: |
if {[string tolower $chan] == "#help"} {
# do whatever
}
|
or:
| Code: |
if {[string equal -nocase $chan "#help"]} {
# do whatever
}
|
Edit: fixed minor typo. _________________ Once the game is over, the king and the pawn go back in the same box.
Last edited by caesar on Sun Feb 27, 2011 2:17 pm; edited 1 time in total |
|
| Back to top |
|
 |
doggo Halfop
Joined: 05 Jan 2010 Posts: 97
|
Posted: Sun Feb 27, 2011 6:28 am Post subject: |
|
|
seems there is a few ways..
this way is my fav
| Code: |
if {$chan != "#help"} {putquick "privmsg $chan :!help dont work in here;return}
//return, with a chan msg explaining why
if {$chan != "#help"} {return}
//return, without a channel msg
|
_________________ NON geeky!! http://gotcode4u.com/ |
|
| Back to top |
|
 |
Gemster Halfop
Joined: 04 Oct 2010 Posts: 51
|
Posted: Sun Feb 27, 2011 9:26 am Post subject: |
|
|
Thanks guys for the help.
Using willyw's vsrsion as its the simplest and works great
caesar,
| Code: | if {[string tolower $chan] = "#help"} {
# do whatever
} |
It dont make any difference if the chan name is upper or lower case
doggo,
Some channel have botserv where !help does work.
Thanks
Gemster |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sun Feb 27, 2011 11:38 am Post subject: |
|
|
| Gemster wrote: |
...
Using willyw's vsrsion as its the simplest and works great
...
|
Don't gloss over the word "rough" in my other post above.
I just wanted to get you pointed in the right direction.
There could be loopholes to fall through, with what I quickly did.
Caesar may very well have a point - experiment carefully and be sure.
I'm glad it got you going though. |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sun Feb 27, 2011 11:48 am Post subject: |
|
|
| doggo wrote: | seems there is a few ways..
this way is my fav
...
|
I like this one... makes it easy to turn the script ON and OFF, on a per channel basis:
| Code: |
setudef flag whatever
bind pub - "!command" someproc
proc someproc {nick uhost handle chan text} {
if {([lsearch -exact [channel info $chan] {+whatever}] != -1)} {
# your procedure code
# line by line
# goes here
}
}
|
Use:
.chanset #channel +whatever
to enable, on a given channel
and
.chanset #channel -whatever
to disable it on a given channel. |
|
| Back to top |
|
 |
nml375 Revered One
Joined: 04 Aug 2006 Posts: 2857
|
Posted: Sun Feb 27, 2011 1:32 pm Post subject: |
|
|
willyw,
You can simplify that code further, since you've defined "whatever" as a flag.
| Code: | ..
if {[channel get "whatever"]} {
... |
Gemster,
Case-matching with channel names is generally an issue, as on most networks, clients are free to use whatever case they prefer. Also, most irc clients preserve the case the user used to join the channel:
Thus, someone joining #help and then typing .help, would then trigger the code, while someone joining #Help (same channel, different case) and then typing .help, would not. That is why you should use the code suggested by Caesar _________________ NML_375, idling at #eggdrop@IrcNET |
|
| Back to top |
|
 |
willyw Revered One
Joined: 15 Jan 2009 Posts: 1175
|
Posted: Sun Feb 27, 2011 1:51 pm Post subject: |
|
|
| nml375 wrote: | willyw,
You can simplify that code further, since you've defined "whatever" as a flag.
| Code: | ..
if {[channel get "whatever"]} {
... |
...
|
Cool. Thanks for coming in.
Now that you mention it, vaguely I remember finding more than one way to do it.
Honestly, I have no idea where I got that bit of code. I have no doubt that I borrowed it from someone else's scripts. |
|
| Back to top |
|
 |
caesar Mint Rubber

Joined: 14 Oct 2001 Posts: 3741 Location: Mint Factory
|
Posted: Sun Feb 27, 2011 2:16 pm Post subject: |
|
|
| Gemster wrote: | Thanks guys for the help.
Using willyw's vsrsion as its the simplest and works great
caesar,
| Code: | if {[string tolower $chan] = "#help"} {
# do whatever
} |
It dont make any difference if the chan name is upper or lower case
|
ahh snap.. should have been == not =, meaning:
| Code: | if {[string tolower $chan] == "#help"} {
# do whatever
} |
_________________ Once the game is over, the king and the pawn go back in the same box. |
|
| Back to top |
|
 |
|