0

Connecting to discord in perl. Don't do this.

Free lines Perl 1 revisions 18 9 days ago 9 days ago
package DiscordBot;

use Discord;
use Discord::Loader; # imports signatures and Moo (optional)

has 'discord' => (
    is      => 'ro',
    default => sub {
        Discord->new(
            token => 'LOLGONE'
        );
    }
);

# called when the constructor is run (new)
method BUILD {
    # you can perform any other pre-loadout stuff here
    # then finally load all the events
    $self->load_events();
}

sub escapeshellarg {
        my $str = @_ ? shift : $_;
        $str =~ s/((?:^|[^\\])(?:\\\\)*)'/$1'\\''/g;
        return "'$str'";
}

local $SIG{__WARN__} = sub {
        my $message = shift;
        print "W: " . $message . "\n";
};

method load_events {
    my $disc = $self->discord;

    $disc->on(ready => func {
        say "=> " . $disc->session->user->username . " is ready!";
    });

    $disc->on(disconnected => func ($code, $reason) {
        say "Disconnect $code";
    });

    $disc->on(guild_create => func ($guild) {
        say "=> Joined server " . $guild->name;
    });

    $disc->on(message => func ($message) {
        say $message->author->username . ": " . $message->content;
        my $user = escapeshellarg($message->author->username);
        my $content = escapeshellarg($message->content);
        $content =~ s/<\@630730262765895680>/Sporks/g;
        chomp(my $reply = `botnix/sporkscmd.sh $user $content`);
        $reply =~ s/\r|\n//gim;
        $reply =~ s/^\s+|\s+$//g;

        if ($message->mentioned && $user ne "Sporks") {
                $reply =~ s/^\001ACTION (.*) \001$/\*$1\*/g;
                if ($reply !~  "/\*NOTHING\*/") {
                        $message->channel->send_message($reply);
                } else {
                }
        }
    });

    $disc->connect();
}

DiscordBot->new;

Using P5-Discord to connect to discord via perl. This is just .... no. don't even.