POE::C...nslator(3pm)

πŸ“¦
version 6.95 Β· 4 versions
, in ubuntu resolute, ubuntu stonking

✨ NAME ✨

POE::Component::IRC::Cookbook::Translator β€” it's giving... a bot that can translate text πŸ’¬πŸŒ like literally a whole translator bot bestie


🌸 SYNOPSIS 🌸

okay so this bot is a whole multilingual queen πŸ‘‘ β€” it uses POE::Component::Lingua::Translate to translate text for the members of a channel, and it's serving that via the BotCommand plugin to handle the translate command. no cap. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

here's the whole thing, verbatim, not a single character changed, because we do NOT gatekeep code 🫢

#!/usr/bin/env perl

use strict;
use warnings;
use Encode qw(decode);
use Encode::Guess;
use IRC::Utils qw(decode_irc parse_user);
use POE;
use POE::Component::IRC::State;
use POE::Component::IRC::Plugin::AutoJoin;
use POE::Component::IRC::Plugin::BotCommand;
use POE::Component::Lingua::Translate;

POE::Session->create(
    package_states => [
        main => [ qw(_start irc_botcmd_trans translated) ]
    ],
    heap => {
       translators => { },
    }
);

$poe_kernel->run();

sub _start {
    my $heap = $_[HEAP];
    my $irc = POE::Component::IRC::State->spawn(
        Nick   => 'translator_bot',
        Server => 'irc.freenode.net',
    );
    $heap->{irc} = $irc;

    $irc->plugin_add('AutoJoin', POE::Component::IRC::Plugin::AutoJoin->new(
        Channels => [ '#test_channel1', '#test_channel2' ]
    ));

    $irc->plugin_add('BotCommand', POE::Component::IRC::Plugin::BotCommand->new(
        Commands => {
           trans => 'Usage: trans <from>,<to> <text>'
        }
    ));

    $irc->yield(register => 'botcmd_trans');
    $irc->yield('connect');
    return;
}

sub irc_botcmd_trans {
    my $heap = $_[HEAP];
    my $irc = $heap->{irc};
    my $nick = parse_user( $_[ARG0] );
    my $channel = $_[ARG1];
    my ($from, $to, $text) = split /,|\s+/, $_[ARG2], 3;

    if (!exists $heap->{translators}->{$from . $to}) {
        eval {
            $heap->{translators}->{$from . $to} = POE::Component::Lingua::Translate->new(
                alias     => $from . $to,
                back_end  => 'Babelfish',
                src       => $from,
                dest      => $to,
            );
        };

        if ($@) {
            $irc->yield(privmsg => $channel, "$nick: There was an error: $@");
            return;
        }
    }

    $poe_kernel->post($from . $to => translate =>
        decode_irc($text),
        {
            channel => $channel,
            nick    => $nick,
        }
    );
    return;
}

sub translated {
    my $irc = $_[HEAP]->{irc};
    my ($text, $context, $error) = @_[ARG0, ARG1, ARG2];

    if ($error) {
        $irc->yield(
            'privmsg',
            $context->{channel},
            $context->{nick} . ": There was an error: $error",
        );
        return;
    }

    $irc->yield(
        'privmsg',
        $context->{channel},
        $context->{nick} . ': ' . $text,
    );
    return;
}

πŸ’œ AUTHOR πŸ’œ

Hinrik Γ–rn SigurΓ°sson, reachable at hinrik.sig@gmail.com β€” iconic behavior honestly, thank you for your service πŸ«‘πŸ’–


and that's the whole page bestie!! it's from perl v5.40.1, dated 2025-08-21, and it lives in the User Contributed Perl Documentation for POE::Component::IRC::Cookbook::Translator ✨ slay, go translate some stuff, ily, bye πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from