Setudef

From EggWiki
Jump to navigation Jump to search

Eggdrop users often like a script's functionality to work on only some of the channels the Eggdrop resides on. For example, an Eggdrop running a Tcl script that greets every user is nice on a small channel, but would quickly become annoying on a busy channel where users are joining every 30 seconds. There are two ways you can enable a script to work on only some channels the Eggdrop resides on.

User-defined Channel Flags (Preferred)

Adding a channel flag

From doc/tcl-commands.doc, you can use the setudef command:

setudef <flag/int/str> <name>

    Description: initializes a user defined channel flag, string or integer setting. You can use it like any other flag/setting. IMPORTANT: Don’t forget to reinitialize your flags/settings after a restart, or it’ll be lost.

This command creates a new flag setting that can be used via the

.chanset

command, and viewed via the

.chaninfo

command. To create a custom flag that a script can check, create it by adding the setudef command:

setudef flag myscript

This will add a flag that can be viewed, enabled or disabled via the partyline:

User defined channel flags:
     -myscript

As with any other Eggdrop channel flag, it can be enabled or disabled with the .chanset command:

.chanset #test +myscript
Successfully set modes { +myscript  } on #test.
[22:42:18] #-HQ# chanset #test +myscript 

.chaninfo #test
User defined channel flags:
     +myscript

Checking a channel flag

So now that you've added a channel flag that the user can interact with, how does a script use that information? The answer is the channel get Tcl command.

channel get <name> [setting]

    Returns: The value of the setting you specify. For flags, a value of 0 means it is disabled (-), and non-zero means enabled (+). If no setting is specified, a flat list of all available settings and their values will be returned.

To check if a flag is enabled for a flag, you would use:

channel get #test myscript

Or, perhaps a more useful example:

if [channel get #test myscript] {
  putserv "PRIVMSG #test :This is a message that is only sent if the myscript flag is enabled"
}

Example Tcl script

So now, let's write a simple greet script that can be turned on and off via the partyline for each channel:

bind join * * greetNewUser

setudef flag greetScript
proc greetNewUser {nick user hand chan} {
  if {[channel get $chan greetScript]} {
    putserv "PRIVMSG $chan :Hello $nick, welcome to $chan!"
  }
}

Bind Mask (less preferred)

Using a bind mask is generally considered less friendly to the user, as the user must edit the Tcl file and then restart the Eggdrop in order to enable or disable the script on a channel.

If your script uses a bind to trigger an action, it probably incorporates a mask that specifies which users or conditions are allowed to trigger the bind. For example, the documentation for a JOIN bind states:

    JOIN (stackable)

    bind join <flags> <mask> <proc>

    procname <nick> <user@host> <handle> <channel>

    Description: triggered by someone joining the channel. The mask in the bind is matched against “#channel nick!user@host” and can contain wildcards.

From that, we can see here that a mask that would trigger on only one channel would take the form of "channel nick!user@host", or specifically for this case, "test *". This creates a bind that can be triggered by any user on #test.

Example Tcl Script

bind join "#test *" * greetNewUser
bind join "#other channel *" * greetNewUser
# Each channel requires its own bind that will call the greetNewUser script

setudef flag greetScript
proc greetNewUser {nick user hand chan} {
  putserv "PRIVMSG $chan :Hello $nick, welcome to $chan!"
}