Nanobot code help

From Insomnia 24/7 Wiki
Revision as of 13:55, 1 March 2011 by imported>Wikiadmin
Jump to: navigation, search

Demo module

#!/usr/local/bin/perl
 
# These are the variables passed to each function call.
#
# $_[0] = name for this module (surely you already know this, but still)
# $_[1] = sending nickname
# $_[2] = sending username
# $_[3] = sending hostmask
# $_[4] = sending channel (or botnick in case of PM)
# $_[5] = current value for $modchan
# $_[6] = current value for $botnick
# $_[7] = first argument given in IRC
# $_[8] = second argument given in IRC etc.
 
# The package name must match the filename + .pm, so this file should be called "demo.pm".
package demo;
 
if ( $firstcall == 0 ) {
	# Here is some code that is executed when the module is loaded and when a function is called.
	nanobot::snd("PRIVMSG #bot :Demo module was loaded!");
	$firstcall == 1;
}
 
# sub join {} is a special name, if you implement this you will receive JOIN info trough this subroutine
# Implementing the join subroutine is optional.
 
# Example implemetation of join:
sub join {
	nanobot::snd("PRIVMSG $_[4] :Hello $_[1], welcome to $_[4]!");
}
 
 
# sub mesg {} is a special name, if you implement this you will receive all messages that don't start with ! (since these are bot commands)
# Implementing the mesg subroutine is optional.
 
 
# sub notice {} is a special name, if you implement this you will receive notices to the bot.
# Implementing the notice subroutine is optional.
 
 
# sub raw {} is a special name, if you implement this you will receive all the raw data the bot receives.
# This is all raw IRC data, so the normal variables will not be available, this means you'll have to parse this data yourself!
# The raw data will be available in the subroutine in the variable $_
# Use this function with caution.
# Implementing the raw subroutine is optional.
 
 
# sub public {} is a special name, this function should contain an array of publicly available function names.
# When this subroutine is not implemented, all functions will only be available to bot opers.
sub public {
	@public = ("help", "function");
}
 
 
# sub help {} is a special name, if you implement this subroutine it will be called when your module is called with no parameters.
# Implementing the mesg subroutine is optional, but highly reckomended.
sub help {
	nanobot::snd("NOTICE $_[1] :Commands: help, function, listargs");
}
 
# Every subroutine is a function that can be called from IRC.
sub function {
	# You can use nanobot::function to use the functions from nanobot. (obviously)
	nanobot::snd("PRIVMSG $_[4] :Message from demo module!");
}
 
# This command is only available to bot opers or when pubmods is set.
sub listargs {
	nanobot::snd("PRIVMSG $_[4] :@_");
}
 
# A module must always end with the line "1;", Perl demands it.
1;

What a module MUST have

Package name

You need to define your module as a Perl package, so the bot will be able to call it.
All you need to do this is add the line package yourpackagename; to the top of your module file.

File name

The bot needs to have a filename for your module that matches the package name, so if your module has package awesomemodule; your module file will need to be called awesomemodule.pm.

The last line

Every Perl module must end with the line 1; this is so that Perl can keep track of where modules end.

What modules should have

Help

Though implementing the help subroutine is optional, it is highly recommended.
The bot will call this subroutine when it receives no fucntion call to a module call. (When someone uses !examplemodule instead of !examplemodule.function.)
The help subroutine is ALWAYS a public command!

Public

The public subroutine is also optional. This subroutine will be called when a non-admin user calls a command. The subroutine should contain an array (@public) of command that you want to be be available to all users.

sub public {
	@public = ("function1", "function2");
}

In this example function1 and function2 can be called by any user, but no other functions are available to the public.