3
0
mirror of https://github.com/pragma-/pbot.git synced 2024-11-25 13:29:29 +01:00
pbot/lib/PBot/Core/Commands/Applets.pm

68 lines
2.0 KiB
Perl
Raw Normal View History

# File: Applets.pm
#
# Purpose: Registers commands to load and unload PBot applets.
2023-02-21 06:31:52 +01:00
# SPDX-FileCopyrightText: 2007-2023 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
package PBot::Core::Commands::Applets;
use parent 'PBot::Core::Class';
use PBot::Imports;
use IPC::Run qw/run timeout/;
use Encode;
sub initialize($self, %conf) {
# bot commands to load and unload applets
$self->{pbot}->{commands}->register(sub { $self->cmd_load(@_) }, "load", 1);
$self->{pbot}->{commands}->register(sub { $self->cmd_unload(@_) }, "unload", 1);
}
sub cmd_load($self, $context) {
my ($keyword, $applet) = $self->{pbot}->{interpreter}->split_args($context->{arglist}, 2);
return "Usage: load <keyword> <applet>" if not defined $applet;
my $factoids = $self->{pbot}->{factoids}->{data}->{storage};
if ($factoids->exists('.*', $keyword)) {
return 'There is already a keyword named ' . $factoids->get_data('.*', $keyword, '_name') . '.';
}
$self->{pbot}->{factoids}->{data}->add('applet', '.*', $context->{hostmask}, $keyword, $applet, 1);
2023-04-22 00:56:43 +02:00
$factoids->set('.*', $keyword, 'add_nick', 1);
$factoids->set('.*', $keyword, 'nooverride', 1);
$self->{pbot}->{logger}->log("$context->{hostmask} loaded applet $keyword => $applet\n");
return "Loaded applet $keyword => $applet";
}
sub cmd_unload($self, $context) {
my $applet = $self->{pbot}->{interpreter}->shift_arg($context->{arglist});
return "Usage: unload <keyword>" if not defined $applet;
my $factoids = $self->{pbot}->{factoids}->{data}->{storage};
if (not $factoids->exists('.*', $applet)) {
return "/say $applet not found.";
}
if ($factoids->get_data('.*', $applet, 'type') ne 'applet') {
return "/say " . $factoids->get_data('.*', $applet, '_name') . ' is not an applet.';
}
my $name = $factoids->get_data('.*', $applet, '_name');
$factoids->remove('.*', $applet);
$self->{pbot}->{logger}->log("$context->{hostmask} unloaded applet $applet\n");
return "/say $name unloaded.";
}
1;