PHP code example of reysa / discord-api

1. Go to this page and download the library: Download reysa/discord-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

reysa / discord-api example snippets



return [
    'bot_token' => env('DISCORD_BOT_TOKEN'),
    'guild_id'  => env('GUILD_ID'),
];
~~~

Then set the values in your `.env` file:

~~~env
DISCORD_BOT_TOKEN=your_bot_token_here
GUILD_ID=your_guild_id_here
~~~

---

## Quick Usage Example

The package ships with a facade alias `DAPI`.

~~~php


use Reysa\DiscordAPI\Facades\DAPI;
use Illuminate\Support\Facades\Route;

Route::get('/test-discord', function () {
    DAPI::sendMessageToChannel(
        '123456789012345678',
        ['content' => 'Hello from Reysa Discord API!']
    );

    return 'Message sent (if token and channel are correct).';
});
~~~

> **Note:** For methods like `sendMessageToChannel` and `sendEmbedMessageToUser`,  
> the second argument is the raw payload array that Discord accepts  
> (for example: `['content' => '...']`, `['embeds' => [...]]`, etc).  
> In v1 this payload is passed directly to the HTTP request body without additional validation.

---

## API Reference (v1)

All methods are static on `Reysa\DiscordAPI\DAPI`.  
In a Laravel app you typically use the facade: `Reysa\DiscordAPI\Facades\DAPI`.

### `getGuildInvites(string $guildId): Response`

Fetch all invites for a guild.

~~~php
$invites = DAPI::getGuildInvites(config('discord-api.guild_id'));
~~~

---

### `deleteGuildInvite(string $code): Response`

Delete a specific invite by code.

~~~php
DAPI::deleteGuildInvite('inviteCodeHere');
~~~

---

### `getMessage(string $channelId, string $messageId): Response`

Get a single message from a channel.

~~~php
$message = DAPI::getMessage($channelId, $messageId);
~~~

---

### `getGuildUser(string $userId): Response`

Get a guild member (by user ID) in the configured guild.

~~~php
$member = DAPI::getGuildUser('123456789012345678');
~~~

---

### `getGuildRoles(): Response`

Get the list of roles in the configured guild.

~~~php
$roles = DAPI::getGuildRoles();
~~~

---

### `giveRole(string $userId, array $roleIds): Response`

Give one or multiple roles to a member.

- `$roleIds` must be an **array of role IDs**.
- It can contain a single ID (for example: `[1234567890]`) or multiple IDs (for example: `[123, 456, 789]`).
- When multiple IDs are provided, the method is intended to give **all** of them to the user.

~~~php
// Give a single role
DAPI::giveRole($userId, [123456789012345678]);

// Give multiple roles
DAPI::giveRole($userId, [
    123456789012345678,
    234567890123456789,
    345678901234567890,
]);
~~~

---

### `removeRole(string $userId, array $roleIds): Response`

Remove one or multiple roles from a member.

- `$roleIds` works the same way as in `giveRole`:
  - `[oneRoleId]` → remove that single role
  - `[roleId1, roleId2, ...]` → remove each of those roles

~~~php
// Remove a single role
DAPI::removeRole($userId, [123456789012345678]);

// Remove multiple roles
DAPI::removeRole($userId, [
    123456789012345678,
    234567890123456789,
]);
~~~

---

### `setName(string $userId, string $name): Response`

Set the member's nickname in the configured guild.

~~~php
DAPI::setName($userId, 'New Nickname');
~~~

---

### `sendMessageToUser(string $userId, string $message): Response|false`

Send a direct message (plain text) to a user.

- Internally:
  - Creates a DM channel with the user.
  - Sends the message to that channel.
- Returns `false` if creating the DM channel fails.

~~~php
$result = DAPI::sendMessageToUser($userId, 'Hello in your DMs!');

if ($result === false) {
    // handle failure