Using

Scripting

A Basic Python Script

Always include module name, version and description

__module_name__ = "Cancel's HelloBot"
__module_version__ = "1.0.0"
__module_description__ = "HelloBot by Cancel"

Include this but it is very helpful it lets you and others know what module and version was loaded

print "\0034",__module_name__, __module_version__,"has been loaded\003"

Now it's time to import the modules you'll be using. Importing xchat is required. Also go ahead and import any other module your script may require

import xchat
import os
import re

So the question usually asked at this point is how do we get it to react to channel events? To do this we "hook" the events. To find the events you are able to hook look at the Advanced\Text Events dialog. Because we are writing a "HelloBot" we are going to "hook" the Join event.

This tuple has two parts. The first is the xchat event to hook. The second is the local method we are going to run when that hooked event occurs.

xchat.hook_print('Join', on_join);

I like to keep all my hooks in the same place makes it easier on my brain so even though I'm showing it to you now all of my hooks are at the bottom of my scripts. I also like to have the method defined before I hook it so it would look like this.

def on_join(word, word_eol, userdata):
      pass

xchat.hook_print('Join', on_join)

Let's take a look now at what goes into a method that's been hooked. What are word, word_eol, and userdata? We really only care about word and word_eol which are both arrays. How do we know what is in the word and word_eol arrays? Look back in xchat at your Advanced\Text Events and find the Join.

Notice the following at the bottom of the window:
1 The nick of the person joining
2 The channel being joined
3 The host of the person

Because we start counting at 0 however word[0] is the nick, word[1] is the channel, word[2] is the host. All of the other events that can be hooked work in a similar fashion. word_eol is similar but is all from that point until the end of the line hence eol and we're not going to worry about userdata for now.

So lets work on our on_join method. It is not required but as a convenience to myself and anyone else reading my script later I go ahead and assign a few variables which make it easier for me to read and use the script. Instead of word[0] all over the place I use triggernick , which is the nick that triggered the event for example.

def on_join(word, word_eol, userdata):
      triggernick, triggerchannel, triggerhost = word
      #we could also do triggernick = word[0]
      #we could also do triggernick = word[1] you get the picture
      destination = xchat.get_context()
      destination.command("say Hello " + triggernick + " and welcome to " + triggerchannel)

The first line I set some convenience variables. Again a lot easier for me to go triggernick triggerchannel throughout the rest of a long script then remembering word[0] or whatever else is in the XChat Text event List.

The next line sets the context. Context may seem a little confusing at first but think of context as where a conversation takes place. When you command xchat you don't want to be speaking out of context.... Get it.... so if someone joined #blueberries you want to reply in #blueberries not in #strawberries. Context is what allows that to happen.

The last line of that is the command to execute so if Cancel joins the channel #blueberries they'll get Hello Cancel and welcome to #blueberries.

The complete script should look like this

__module_name__ = "Cancel's HelloBot"
__module_version__ = "1.0.0"
__module_description__ = "HelloBot by Cancel"

print "\0034",__module_name__, __module_version__,"has been loaded\003"

import xchat

def on_join(word, word_eol, userdata):
      triggernick, triggerchannel, triggerhost = word
      destination = xchat.get_context()
      destination.command("say Hello " + triggernick + " and welcome to " + triggerchannel)

xchat.hook_print('Join', on_join)

That's a very simple xchat script. I hope this information helps get you started. You can find the full set of my python scripts at the locations below: http://cancelbot.sourceforge.net
http://sourceforge.net/projects/cancelbot



Print - Recent Changes - Search
Page last modified on March 13, 2010, at 03:04 PM