commandstring / dphp-bot
构建 DPHP Bot 的非官方方式
v7.0.3
2023-07-17 00:32 UTC
Requires
- php: >=8.1
- commandstring/utils: ^1.7
- react/async: ^4.1
- team-reflex/discord-php: dev-master
- tnapf/env: ^1.1
Requires (Dev)
- ergebnis/composer-normalize: ^2.31
- fakerphp/faker: ^1.21
- friendsofphp/php-cs-fixer: ^3.16
- jetbrains/phpstorm-attributes: ^1.0
- phpunit/phpunit: ^10.1
- roave/security-advisories: dev-latest
- xheaven/composer-git-hooks: ^3.0
README
构建 discordPHP bot 的非官方方式。
目录
安装
composer create-project commandstring/dphp-bot
重要资源
DiscordPHP Discord 服务器 仅询问与 DiscordPHP 自身包装相关的问题,不要询问如何使用此。
开发者中心 有关此模板的问题可以在这里询问
配置
将 .env.example
文件复制到 .env
并添加您的机器人令牌。
slash 命令
创建一个实现 Core\Commands\CommandHandler
的类,并将 Core\Commands\Command
属性附加到它上。
<?php namespace Commands; use Core\Commands\Command; use Core\Commands\CommandHandler; use Discord\Builders\CommandBuilder; use Discord\Parts\Interactions\Interaction; use function Core\messageWithContent; #[Command] class Ping implements CommandHandler { public function handle(Interaction $interaction): void { $interaction->respondWithMessage(messageWithContent('Ping :ping_pong:'), true); } public function autocomplete(Interaction $interaction): void { } public function getConfig(): CommandBuilder { return (new CommandBuilder()) ->setName('ping') ->setDescription('Ping the bot'); } }
启动机器人后,它将自动将命令注册到 Discord。如果您对配置进行了任何更改,它将即时更新命令。
事件
创建一个实现 Core\Events
中任何接口的类。实现匹配您所需事件的接口。
<?php namespace Events; use Core\Events\Init; use Discord\Discord; class Ready implements Init { public function handle(Discord $discord): void { echo "Bot is ready!\n"; } }
如果接口不存在,请使用 类参考。只需创建一个具有与事件中匹配的参数的 handle 方法的接口,并将其放在 /Core/Events
中。
禁用命令和事件
如果您想禁用命令处理器或事件监听器,请将 Core\Commands\Disabled
属性附加到它上。
<?php namespace Events; use Core\Events\Init; use Discord\Discord; #[Disabled] class Ready implements Init { public function handle(Discord $discord): void { echo "Bot is ready!\n"; } }
扩展模板
引导序列
在 /Bootstrap
中创建一个文件,然后将其包含在 /Boostrap/Requires.php
中。
环境变量
在 /Core/Env.php
中添加文档注释,并将变量添加到 .env
。
您还应该将其添加到 .env.example
。