Livia 是一个用于 PHP 的 Discord Bot 框架。

0.1.0 2020-05-24 13:00 UTC

This package is not auto-updated.

Last update: 2024-09-24 08:27:57 UTC


README

Livia 是一个 Discord Bot 框架,它利用了 Yasmin,一个 Discord API 库。

Commando 被用作 Livia 设计的模板。

入门指南

使用 composer 安装 Livia 及其依赖项。

composer require charlottedunois/livia

内置参数类型

Livia 随带一些参数类型,您可以在命令参数中使用。以下是一个列表

  • 布尔型
  • 频道
  • 命令
  • 命令或组
  • 浮点型
  • 整型
  • 成员
  • 角色
  • 字符串
  • 用户

内置命令

Livia 内部包含以下命令

  • disable (禁用命令/组)
  • enable (启用命令/组)
  • load (加载新命令)
  • reload (重新加载命令)
  • unload (卸载命令)
  • eval (评估 PHP 代码)
    • 所有 PHP 错误都会抛出。
    • 为 eval 执行定义随机命名空间。
    • 它有一个名为 $doCallback 的匿名函数,它接受您提供的任何值,检查它并将它作为回复发送到 Discord。
    • 客户端通过 $this->client$client 暴露。
    • 您可以通过 $message 访问特定的 CommandMessage 实例。
    • 最后评估结果可以通过 $this->lastResult 访问。
  • help (在 DM 中显示帮助信息)
  • ping (计算机器人的延迟)
  • prefix (获取或设置公会/全局中机器人的前缀)

设置提供者

设置提供者位于 Livia 和 DBMS 之间。设置提供者的任务是永久存储设置,例如公会前缀。

Livia 包含以下两个提供者

MySQL/MariaDB (react/mysql)

MySQLProvider - 一个用于 MySQL/MariaDB 的提供者,它使用 react/mysql

使用方法

$factory = new \React\MySQL\Factory($your_livia_client->getLoop());
$factory->createConnection('user:password@localhost/database')->done(function (\React\MySQL\ConnectionInterface $db) use ($your_livia_client) {
    $provider = new \CharlotteDunois\Livia\Providers\MySQLProvider($db);
    $your_livia_client->setProvider($provider);
});

Plasma

PlasmaProvider - 一个用于 Plasma plasma/plasma 的提供者。

Plasma 是一个非阻塞、异步数据访问数据库抽象层。要使用 plasma,您还需要一个驱动程序,该驱动程序与数据库服务器进行通信。
对于 MySQL/MariaDB,有 plasma/driver-mysql,它支持预编译语句。

使用方法

// See the plasma project for how to create a plasma client

$provider = new \CharlotteDunois\Livia\Providers\PlasmaProvider($plasma_client);
$your_livia_client->setProvider($provider);

创建命令

Livia 具有命令重新加载功能,它要求您在命令文件中返回一个匿名函数,该函数返回一个新的匿名类。

不要在类外声明函数。这将变成一个 致命错误,因为函数无法重新声明。
还有其他一些限制。如果您需要任何(辅助)函数或常量,请将它们声明在类内作为方法或类常量。

示例

// /rootBot/commands/moderation/ban.php

// Livia forces you to use lowercase command name and group ID.
// (moderation = group ID, ban = command name)

// Livia will automatically call the anonymous function and pass the LiviaClient instance.
return function ($client) {
    // Extending is required
    return (new class($client) extends \CharlotteDunois\Livia\Commands\Command {
        function __construct(\CharlotteDunois\Livia\Client $client) {
            parent::__construct($client, array(
                'name' => 'ban',
                'aliases' => array(),
                'group' => 'moderation',
                'description' => 'Bans an user.',
                'guildOnly' => true,
                'throttling' => array( // Throttling is per-user
                    'usages' => 2,
                    'duration' => 3
                ),
                'args' => array(
                    array(
                        'key' => 'user',
                        'prompt' => 'Which user do you wanna ban?',
                        'type' => 'member'
                    )
                )
            ));
        }
        
        // Checks if the command is allowed to run - the default method from Command class also checks userPermissions.
        // Even if you don't use all arguments, you are forced to match that method signature.
        function hasPermission(\CharlotteDunois\Livia\Commands\Context $context, bool $ownerOverride = true) {
            return $context->message->member->roles->has('SERVER_STAFF_ROLE_ID');
        }
        
        // Even if you don't use all arguments, you are forced to match that method signature.
        function run(\CharlotteDunois\Livia\Commands\Context $context, \ArrayObject $args,
                      bool $fromPattern) {
            // Do what the command has to do.
            // You are free to return a Promise, or do all-synchronous tasks synchronously.
            
            // If you send any messages (doesn't matter how many),
            // return (resolve) the Message instance, or an array of Message instances.
            // Promises are getting automatically resolved.
            
            return $args->user->ban()->then(function () use ($context) {
                return $context->reply('The user got banned!');
            });
        }
    });
};

示例

require_once(__DIR__.'/vendor/autoload.php');

$loop = \React\EventLoop\Factory::create();
$client = new \CharlotteDunois\Livia\Client(array(
    'owners' => array('YOUR_USER_ID'),
    'unknownCommandResponse' => false
), $loop);

// Registers default commands, command groups and argument types
$client->registry->registerDefaults();

// Register the command group for our example command
$client->registry->registerGroup(array('id' => 'moderation', 'name' => 'Moderation'));

// Register our commands (this is an example path)
$client->registry->registerCommandsIn(__DIR__.'/commands/');

// If you have created a command, like the example above, you now have registered the command.

$client->on('ready', function () use ($client) {
    echo 'Logged in as '.$client->user->tag.' created on '.
           $client->user->createdAt->format('d.m.Y H:i:s').PHP_EOL;
});

$client->login('YOUR_TOKEN')->done();
$loop->run();

文档

https://charlottedunois.github.io/Livia/