Come faccio correttamente arresto un bot Bot :: BasicBot (sulla base di POE :: :: Componente IRC)?

StackOverflow https://stackoverflow.com/questions/2471373

  •  20-09-2019
  •  | 
  •  

Domanda

Questo è uno script di esempio. Quando mi ha colpito Ctrl + C, il bot IRC si chiude ma si ricollega di nuovo dopo qualche tempo. Come faccio chiudo il bot correttamente?

#!/usr/bin/perl

package main;

my $bot = Perlbot->new (server => 'irc.dal.net');

$SIG{'INT'} = 'Handler';
$SIG{'TERM'} = 'Handler';

sub Handler {
print "\nShutting down bot...\n";
$bot->shutdown('Killed.');
};

$bot->run;

package Perlbot;
use base qw(Bot::BasicBot);

sub connected {
my $self = shift;
$self->join('#codetestchan');
}
È stato utile?

Soluzione

ho prelevato mantenitore di Bot :: BasicBot, e dalla versione 0.82, è possibile spegnerlo correttamente con $bot->shutdown($quit_message).

Altri suggerimenti

Da guardare il codice sorgente di documentazione e di Bot :: BasicBot, non riesco a trovare un modo elegante per spegnerlo. Come dimostrato, chiamando $bot->shutdown() (che in realtà invia un evento shutdown a POE::Component::IRC) sarà solo causare Bot::BasicBot di riconnettersi (stesso con $bot->quit() tra l'altro).

Questo e altri utenti limitazioni hanno incontrato mi hanno causato alla consiglia di utilizzare direttamente POE::Component::IRC. Ha molti plugin giorno d'oggi per caratteristiche fornite dal Bot::BasicBot che mancavano quando Bot::BasicBot è stato creato. Mentre si potrebbe essere necessario digitare un po 'di più per ottenere un bot up di base e in esecuzione, si ha molta più flessibilità. Di seguito è riportato un bot come quella nel tuo esempio, senza l'utilizzo di Bot::BasicBot. Invierà un messaggio di chiusura al server IRC quando si preme CTRL + C, attendere che sia stato scollegato, quindi uscire:

#!/usr/bin/env perl

use strict;
use warnings;
use POE;
use POE::Component::IRC::State;
use POE::Component::IRC::Common qw(parse_user);
use POE::Component::IRC::Plugin::Connector;
use POE::Component::IRC::Plugin::AutoJoin;

# create our session
POE::Session->create(
    package_states => [
        # event handlers
        (__PACKAGE__) => [qw(_start int irc_join irc_disconnected)]
    ]
);

# start the event loop
POE::Kernel->run();

# session start handler
sub _start {
    my ($kernel, $heap) = @_[KERNEL, HEAP];

    # handle CTRL+C
    $kernel->sig(INT => 'int');

    # create bot object
    my $irc = POE::Component::IRC::State->spawn(
        server => 'irc.freenode.net',
        nick   => 'basic123bot',
        debug  => 1,
    );

    # save $irc in our session's storage heap
    $heap->{irc} = $irc;

    # handle reconnects
    $irc->plugin_add('Connector', POE::Component::IRC::Plugin::Connector->new());

    # handle channel joining
    $irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new(
        Channels => ['#foo123bar'],
    ));

    # connect to IRC
    $irc->yield('connect');
}

# interrupt signal handler
sub int {
    my ($kernel, $heap) = @_[KERNEL, HEAP];

    $heap->{irc}->yield('quit', 'Quitting, bye!');
    $heap->{shutting_down} = 1;
    $kernel->sig_handled();
}

# join handler
sub irc_join {
    my ($who, $chan) = @_[ARG0, ARG1];
    my $irc = $_[HEAP]->{irc};

    my ($nick, $user, $host) = parse_user($who);
    if ($nick eq $irc->nick_name()) {
        # say hello to channel members
        $irc->yield('privmsg', $chan, 'Hello everybody');
    }
}

# disconnect handler
sub irc_disconnected {
    my ($heap) = $_[HEAP];

    # shut down if we disconnected voluntarily
    $heap->{irc}->yield('shutdown') if $heap->{shutting_down};
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top