Howto Make Plugins

From NoxBot the modular PHP IRC bot

Jump to: navigation, search

This guide is not compatible with NoxBot 0.4 or higher.

Contents

Introduction

The basic code

plugExample1

<?php
class plugExample1{
    public static function load(){

Here the loading code goes for this plugin.

Things like registering to events.

We are registering a handler for a command.

        eventhandler::addHandler(__CLASS__, 'example', EVENT_TYPE_COMMAND, 'example');
  • The fisrt parameter is basicly a pointer to this class.
  • The second parameter is to what function the eventhandler should go if the event was raised.
  • The third parameter is the event type. In this case we want a command.
  • The fourth paramater is on what command we want it to be triggered.
    }

Here we build our handler function that will be called on the !example command. Note that the parameter always is an event. The rest of the data is in that event.

    public static function example(event $event){

We will use the datahelper class to parse this data and send a reply.

        $dh = new datahelper($event);

We use the simpler way of using the datahelper here by giving it the event and let the datahelper do our work. Which is perfectly fine.

Sending a reply is quite easy when you have it setup like this. Let me show:

        $dh->reply('You used the "example" command with the following text: "'.$dh->getCommandTrailing().'".');

Or use the following method. It will show up as an action.

        $dh->replyAction('noticed that you used the "example" command with the following text: "'.$dh->getCommandTrailing().'".');

Or a notice:

        $dh->notice('You used the "example" command with the following text: "'.$dh->getCommandTrailing().'".');

We can also force it to reply or notice in private.

        $dh->replyPrivate('You used the "example" command with the following text: "'.$dh->getCommandTrailing().'".');
        $dh->noticePrivate('You used the "example" command with the following text: "'.$dh->getCommandTrailing().'".');

Thats it. Except we need to make the eventhandler aware of that this event is completed. We do this like this:

        $event->delete();

Quite easy right?

To make it even better you should clean up all used data to avoid memory leaks:

        unset($eventdata, $dh);

Note that we don't "unset" the $event here. And it should not be done here actually. The event handler will take care of it.

Thats it a plugin that reply's on a command. It couldn't be more easy.

Also read the stuff in the unload() function please!

    }
    
    public static function unload(){

This is the unloading part.

Here you can clean up the object and other data that you use.

We register an event handler on load so we need to unregister it at unloading. Unloading is basicly the same but a different eventhandler function is used.

        eventhandler::removeHandler(__CLASS__, 'example', EVENT_TYPE_COMMAND, 'example');
    }
}
?>

Lesson 2

This is the second lesson in making plugins for Noxbot. In this lesson we will learn howto use multiple commands in one plugin.

<?php

As we have seen in the previous lesson, we need to load the commands but this time we are loading mulitple commands

class plugFoxxCmds{
    public static function load(){
        eventhandler::addHandler(__CLASS__, 'Food', EVENT_TYPE_COMMAND, 'food');
        eventhandler::addHandler(__CLASS__, 'Whack', EVENT_TYPE_COMMAND, 'whack');
        eventhandler::addHandler(__CLASS__, 'ROFLOLMAO', EVENT_TYPE_COMMAND, 'ROFLOLMAO');
        eventhandler::addHandler(__CLASS__, 'beer', EVENT_TYPE_COMMAND, 'beer'); 
        eventhandler::addHandler(__CLASS__, 'wine', EVENT_TYPE_COMMAND, 'wine');
        eventhandler::addHandler(__CLASS__, 'champaign', EVENT_TYPE_COMMAND, 'champaign');
        eventhandler::addHandler(__CLASS__, 'peace', EVENT_TYPE_COMMAND, 'peace');
        eventhandler::addHandler(__CLASS__, 'hug', EVENT_TYPE_COMMAND, 'hug');
        eventhandler::addHandler(__CLASS__, 'lick', EVENT_TYPE_COMMAND, 'lick');
        eventhandler::addHandler(__CLASS__, 'kiss', EVENT_TYPE_COMMAND, 'kiss');    
        eventhandler::addHandler(__CLASS__, 'punnish', EVENT_TYPE_COMMAND, 'punnish');  
    }
   

Now we have determine each function for each command

    public static function Food(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' was served some '.$dh->getCommandTrailing().'.');
        $event->delete();
        unset($eventdata, $message, $dh);
    } 
    public static function Whack(event $event){
        $dh = new datahelper($event);
            if (auth::getUserLevel($message) >= 3) $dh->replyAction('turns around and shoots at '.$dh->getPrefixNickname().', you bloody bastard');
            else $dh->replyAction('grabs his gun and shoots '.$dh->getCommandTrailing().'\'s brains out');
        $event->delete();
        unset($eventdata, $message, $dh);
    }
    public static function ROFLOLMAO(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' is roling on the floor laughing out loudly his ass off, how funny xD.');
        $event->delete();
        unset($eventdata, $dh);
    }
    public static function beer(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' gives '.$dh->getCommandTrailing().' a nice and cold beer.');
        $event->delete();
        unset($eventdata, $message, $dh);
    }    
    public static function wine(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' gives '.$dh->getCommandTrailing().' a glass of wine, how romantic.');
        $event->delete();
        unset($eventdata, $message, $dh);
    }    
    public static function champaign(event $event){
        $dh = new datahelper($event);
        $dh->reply('CELEBRATIONS!!!! champaign for everyone');
        $event->delete();
        unset($eventdata, $dh);
    }
    public static function peace(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' makes it up with '.$dh->getCommandTrailing().' \o/ PEACE!! \o/');
        $event->delete();
        unset($eventdata, $message, $dh);
    }   
    public static function lick(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' gives '.$dh->getCommandTrailing().' a sensual lick in the face, and goes much further!');
        $event->delete();
        unset($eventdata, $message, $dh);
    }     
    public static function hug(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' hugs '.$dh->getCommandTrailing().' I hope this will make you happy:)');
        $event->delete();
        unset($eventdata, $message, $dh);
    }  
    public static function kiss(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' gives '.$dh->getCommandTrailing().' a sweet, lip-tinteling kiss!');
        $event->delete();
        unset($eventdata, $message, $dh);
    }   
    public static function punnish(event $event){
        $dh = new datahelper($event);
        $dh->reply($dh->getPrefixNickname().' punnishes '.$eventdata[1][0].' with a '.$dh->getCommandTrailing().'!');
        $event->delete();
        unset($eventdata, $message, $dh);
    }

This is the unloading part which is as simple as the loading part

    public static function unload(){
        eventhandler::removeHandler(__CLASS__, 'Food', EVENT_TYPE_COMMAND, 'food');
        eventhandler::removeHandler(__CLASS__, 'Whack', EVENT_TYPE_COMMAND, 'whack');
        eventhandler::removeHandler(__CLASS__, 'ROFLOLMAO', EVENT_TYPE_COMMAND, 'ROFLOLMAO');
        eventhandler::removeHandler(__CLASS__, 'beer', EVENT_TYPE_COMMAND, 'beer');
        eventhandler::removeHandler(__CLASS__, 'wine', EVENT_TYPE_COMMAND, 'wine');
        eventhandler::removeHandler(__CLASS__, 'champaign', EVENT_TYPE_COMMAND, 'champaign');
        eventhandler::removeHandler(__CLASS__, 'peace', EVENT_TYPE_COMMAND, 'peace');
        eventhandler::removeHandler(__CLASS__, 'hug', EVENT_TYPE_COMMAND, 'hug');
        eventhandler::removeHandler(__CLASS__, 'lick', EVENT_TYPE_COMMAND, 'lick');
        eventhandler::removeHandler(__CLASS__, 'kiss', EVENT_TYPE_COMMAND, 'kiss');  
        eventhandler::removeHandler(__CLASS__, 'punnish', EVENT_TYPE_COMMAND, 'punnish');
    }
}
?>

Lesson 3

This is already the third lesson, this time we will make a simple game with the use of timers.

<?php

Now we are going to start. Note that this is a lot more difficult than the previous lessons.

class plugFoxxVampgame{

These are just two variables

    private static $players = array();
    private static $vampdh = null;

Like always we have to load the commands, now you think: "only two commands?" One handler is for the timer and the second one is the command for enabling and disabling the game.

    public static function load(){
        eventhandler::addHandler(__CLASS__, 'vampgame', EVENT_TYPE_COMMAND, 'vampgame', 49);
        eventhandler::addHandler(__CLASS__, 'timerTick', EVENT_TYPE_TIMER, 'foxx_vampgame');
    }

This is were we determine the timer

    public static function timerTick(event $event){
        foreach (self::$players as $player => $time){

How much time is this timer going to take?

            if ($time + 20 <= time() & count(self::$players) > 0){
                unset(self::$players[$player]);
                if (self::$vampdh != null){
                    self::$vampdh->reply($player.' is no longer a vampire.');
                }
            }
        }

In this part we are saying that the game has to stop when there are no player left.

        if (count(self::$players) < 1 & self::$vampdh != null){

If the command bite exists (which is when the irritable bowel syndrome game is enabled) the command has to be unloaded for stopping the game.

                if (eventhandler::handlerExists(EVENT_TYPE_COMMAND, 'bite')) 
                eventhandler::removeHandler(__CLASS__, 'bite', EVENT_TYPE_COMMAND, 'bite');

If the timer exists (which is when the game is enabled) it has to be disabled in order to stop the game.

            if (timer::exists('foxx_vampgame')) timer::stop('foxx_vampgame');
            self::$players = array();

Now we have to have to make the bot say the game is stopped

            self::$vampdh->notice('There are no more vampires. Game stopped.');
            self::$vampdh = null;
        }
        $event->delete();
    }
   

This is the function for turning the game on or off.

    public static function vampgame(event $event){
        $dh = new datahelper($event);
        if (strtolower($dh->getCommandParam(0)) == 'on'){

If the command is already loaded (which means that the game is already enabled) the only thing the bot should do is saying the game is already started.

           if (eventhandler::handlerExists(EVENT_TYPE_COMMAND, 'bite')){
                $dh->noticePrivate('Game already started.');

If the command is loaded we have to load the command.

           }else{
                eventhandler::addHandler(__CLASS__, 'bite', EVENT_TYPE_COMMAND, 'bite');
                timer::create('foxx_vampgame', 1, 1);
                self::$players[$dh->getIrcPrefixNickname()] = time();

here is where we make the bot say that the game is enabled

                $dh->notice('Vampgame started. You are now a vampire.');
                self::$vampdh = $dh;
            }
        }elseif (strtolower($dh->getCommandParam(0)) == 'off'){

If the command bite is loaded we have to unload it

            if (eventhandler::handlerExists(EVENT_TYPE_COMMAND, 'bite')){

This is where we unload the command

                eventhandler::removeHandler(__CLASS__, 'bite', EVENT_TYPE_COMMAND, 'bite');

This is where we unload the timer

                if (timer::exists('foxx_vampgame')) timer::stop('foxx_vampgame');
                self::$players = array();
                self::$vampdh = null;

Like usual we have to give a message that an action was taken

                $dh->notice('Game has been stopped.');

If the command isn't loaded we have to message that there wasn't a game started.

            }else{
                $dh->noticePrivate('Vampgame was not starded so there is nothing to stop.');
            }

This command only accepts "on" or "off" so when something else is added we have to say it doesn't accept that.

        }else{
            $dh->noticePrivate('This command only accept "on" or "off".');
        }
        $event->delete();
        unset($dh);
    }

After the enabling and disabling part we now have determine what the bot has to do when the command "bite" is given

    public static function bite(event $event){
        $dh = new datahelper($event);

First we have to check if the one who uses the command is a vampire.

        if (self::$players[$dh->getIrcPrefixNickname()] > 0){

If there is a nick included with !bite the actual code has to be loaded

            if ($dh->getCommandParam(0) != ){

When the the player already is a vampire we cannot turn it into a vampire again so again a message has to be sent.

                if (isset(self::$players[$dh->getCommandParam(0)]) & self::$players[$dh->getCommandParam(0)] > 0){
                    $dh->noticePrivate($dh->getCommandParam(0).' is already a vamp.');

In case the player isn't a vampire we have to turn it into a vampire.

                }else{

First we have to say the person has been turned into a vampire

                    $dh->reply($dh->getIrcPrefixNickname().' bites '.$dh->getCommandParam(0).' in the neck turning '.$dh->getCommandParam(0).' into a vampire.');

Now we're going to enable the timer

                    self::$players[$dh->getCommandParam(0)] = time();
                }

If you don't give a nickname after the command the bot doesn't know who to bite so a it must give a message about that

            }else{
                $dh->noticePrivate($dh->getIrcPrefixNickname().', who are you going to bite?');
            }

When the player isn't a vamp we have to say he can't use the command.

        }else{

Another message but this time a private notice

            $dh->noticePrivate($dh->getIrcPrefixNickname().', You\'re no vamp');
        }

Now we have to delete the event like we have seen in the first lesson

        $event->delete();
        unset($dh);
    }

This is where we unload the commands/handlers

    public static function unload(){
        eventhandler::removeHandler(__CLASS__, 'vampgame', EVENT_TYPE_COMMAND, 'vampgame', 49);  
    }
}
?>

Modifieing the code

Personal tools