定制 commandstring/dphp-bot 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

commandstring/dphp-bot

最新稳定版本:v7.0.3

Composer 安装命令:

composer create-project commandstring/dphp-bot

包简介

An unofficial way to structure a DPHP Bot

README 文档

README

An unofficial way to structure a discordPHP bot.

Table of Contents

Installation

composer create-project commandstring/dphp-bot

Important Resources

DiscordPHP Class Reference

DiscordPHP Documentation

DiscordPHP Discord Server Only ask questions relevant to DiscordPHP's own wrapper, not on how to use this.

Developer Hub Issues about this template can be asked here

Configuration

Copy the .env.example file to .env and add your bot token.

Slash Commands

Create a class that implements Core\Commands\CommandHandler and attach the Core\Commands\Command attribute to it.

<?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');
    }
}

Once you start the bot, it will automatically register the command with Discord. And if you make any changes to the config, it will update the command on the fly.

Events

Create a class that implements any of the interfaces found inside of Core\Events. Implement the interface that matches your desired event.

<?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";
    }
}

If the interface doesn't exist use the Class Reference. Just create a interface that has a handle methods with args that match up with the ones in the event. Then sit it inside /Core/Events

Disabling Commands and Events

If you want to disable a command handler or event listener attach the Core\Commands\Disabled attribute to it.

<?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";
    }
}

Extending The Template

Bootstrap Sequence

Create a file inside /Bootstrap and then require it inside of /Boostrap/Requires.php.

Environment Variables

Add a doc comment to /Core/Env.php and then add the variable to .env

You should also add it to .env.example

统计信息

  • 总下载量: 28
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 13
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 13
  • Watchers: 2
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-01