Skip to content

An unofficial way to structure a discordPHP bot.

License

Notifications You must be signed in to change notification settings

CommandString/DiscordPHP-Bot-Template

Repository files navigation

DiscordPHP Bot Template

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

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

Hot Reloading

This template has a built-in HMR (Hot Module Reloading) system. Which essentially means that while you're developing your bot.

The code will automatically be updated without having to restart the bot. Set HMR in your .env file to true to enable it.

Note: HMR only works on Commands and Events. (for now)