Difference between revisions of "Nanobot code help"

From Insomnia 24/7 Wiki
Jump to: navigation, search
imported>Wikiadmin
imported>Wikiadmin
Line 86: Line 86:
 
=== The last line ===
 
=== The last line ===
 
Every Perl module must end with the line '''1;''' this is so that Perl can keep track of where modules end.
 
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 ==
 
== What modules should have ==
Line 94: Line 95:
  
 
=== Public ===
 
=== 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.<br />
+
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 commands that you want to be be available to all users.<br />
 
<pre>
 
<pre>
 
sub public {
 
sub public {
Line 101: Line 102:
 
</pre>
 
</pre>
 
In this example '''function1''' and '''function2''' can be called by any user, but no other functions are available to the public.
 
In this example '''function1''' and '''function2''' can be called by any user, but no other functions are available to the public.
 +
 +
==Writing subroutines==
 +
===How does a subroutine get called===
 +
You will almost always want to add a new subroutine to your module in order to make a command available to you and/or your users.<br />
 +
The commands you give it in IRC translate directly to package(module) names and functions.<br />
 +
For example:
 +
!hello.world
 +
Will call a module '''hello''' and it's subroutine '''world'''.
 +
 +
===What variables are available===
 +
===How to use nanobot's functions===
 +
 +
== Optional subroutines ==
 +
===Mesg===
 +
===Notice===
 +
===Join===
 +
===Raw===
 +
===First call===
 +
<pre>
 +
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 :Module was loaded!");
 +
$firstcall == 1;
 +
}
 +
</pre>

Revision as of 14:04, 1 March 2011

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 commands 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.

Writing subroutines

How does a subroutine get called

You will almost always want to add a new subroutine to your module in order to make a command available to you and/or your users.
The commands you give it in IRC translate directly to package(module) names and functions.
For example:

!hello.world

Will call a module hello and it's subroutine world.

What variables are available

How to use nanobot's functions

Optional subroutines

Mesg

Notice

Join

Raw

First call

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 :Module was loaded!");
	$firstcall == 1;
}