Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/commandstring/phpcord

A modern discord api wrapper in PHP
https://github.com/commandstring/phpcord

composer discord discord-api discord-api-wrapper php php8 php81 php82

Last synced: 26 days ago
JSON representation

A modern discord api wrapper in PHP

Awesome Lists containing this project

README

        

# PHPCord

A modern Discord API wrapper for PHP

**NOTE**: This is still in development and is not ready for production use

## Requirements

- PHP 8.1+
- Composer

## Resources

- [Discord Developer Portal](https://discord.com/developers/docs/intro)

## Installation

```bash
composer require phpcord/phpcord
```

## Example

```php
withRest(
new Guzzle(options: ['verify' => false])
)->withGateway();

define('START_TIME', time());

$bot->gateway->onEvent(Event::READY, static function (Ready $ready) use ($bot) {
LOGGER->info("{$ready->user->username} is ready!");

return; // remove to register command

$command = new ApplicationCommand();
$command->name = 'status';
$command->description = 'Get the status of the bot';

$typeOption = new ApplicationCommandOption();
$typeOption->name = 'type';
$typeOption->description = 'The type of status';
$typeOption->type = ApplicationCommandOptionType::STRING;
$typeOption->required = true;

$typeOptionRam = new ApplicationCommandOptionChoice();
$typeOptionRam->name = $typeOptionRam->value = 'ram';

$typeOptionUptime = new ApplicationCommandOptionChoice();
$typeOptionUptime->name = $typeOptionUptime->value = 'uptime';

$typeOption->choices = [$typeOptionRam, $typeOptionUptime];
$command->options = [$typeOption];

await($bot->rest->guildCommands->createCommand($ready->application->id, '988910112825548811', $command));

LOGGER->info('Created status command!');
});

$bot->gateway->onEvent(Event::INTERACTION_CREATE, static function (Interaction $interaction) use ($bot) {
if (
$interaction->type !== InteractionType::APPLICATION_COMMAND ||
$interaction->data->name !== 'status'
) {
return;
}

$type = $interaction->data->options[0]->value;

if ($type === 'ram') {
$messageContent = 'RAM Usage is ' . round(memory_get_usage() / 1024 / 1024, 2) . 'MB';
} else if ($type === 'uptime') {
$messageContent = 'The bot was started ';
}

$response = new InteractionResponse();
$response->type = InteractionResponseType::CHANNEL_MESSAGE_WITH_SOURCE;
$response->data = (new MessageBuilder())
->withContent($messageContent)
->withFlags(MessageFlag::EPHEMERAL)
->build();

$bot->rest->interactions->createResponse(
$interaction->id,
$interaction->token,
$response
);
});

$bot->gateway->start();
```