An unofficial way to structure a discordPHP bot.
- Installation
- Important Resources
- Configuration
- Slash Commands
- Events
- Disabling Commands and Events
- Extending The Template
composer create-project commandstring/dphp-bot
DiscordPHP Discord ServerOnly ask questions relevant to DiscordPHP's own wrapper, not on how to use this.
Developer HubIssues about this template can be asked here
Copy the .env.example file to .env and add your bot token.
Create a class that implements Core\Commands\CommandHandler and attach the Core\Commands\Command attribute to it.
<?phpnamespaceCommands;
useCore\Commands\Command;
useCore\Commands\CommandHandler;
useDiscord\Builders\CommandBuilder;
useDiscord\Parts\Interactions\Interaction;
usefunctionCore\messageWithContent;
#[Command]
class Ping implements CommandHandler
{
publicfunctionhandle(Interaction$interaction): void
{
$interaction->respondWithMessage(messageWithContent('Ping :ping_pong:'), true);
}
publicfunctionautocomplete(Interaction$interaction): void
{
}
publicfunctiongetConfig(): CommandBuilder
{
return (newCommandBuilder())
->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.
Create a class that implements any of the interfaces found inside of Core\Events.
Implement the interface that matches your desired event.
<?phpnamespaceEvents;
useCore\Events\Init;
useDiscord\Discord;
class Ready implements Init
{
publicfunctionhandle(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
If you want to disable a command handler or event listener attach the Core\Commands\Disabled attribute to it.
<?phpnamespaceEvents;
useCore\Events\Init;
useDiscord\Discord;
#[Disabled]
class Ready implements Init
{
publicfunctionhandle(Discord$discord): void
{
echo"Bot is ready!\n";
}
}Create a file inside /Bootstrap and then require it inside of /Boostrap/Requires.php.
Add a doc comment to /Core/Env.php and then add the variable to .env
You should also add it to .env.example