Skip to content
Dmitry edited this page Apr 13, 2024 · 8 revisions

Discord Wrapper is an easy to use cacheable PHP wrapper.

Example of use:

<?php

require 'vendor/autoload.php';

use Naneynonn\Api\Client as DiscordApiClient;

$config = [
  'bot' => [
    'token' => '',
  ]
];

$api = new DiscordApiClient($config);
$guild = $api->guild->getGuild(guild_id: '763291052341461022', params: ['with_counts' => true]);

echo $guild['name'];
// or
var_dump($guild);

Cache

All GET requests are automatically cached for 10 minutes. If you need to disable this parameter, add cache_ttl: NULL to your request. You, also, can increase the cache time in seconds. Example:

$api->guild->getGuild(guild_id: 'guild_id', params: ['with_counts' => true], cache_ttl: null);

Proxy

Add this parameter to the config:

$config = [
  'proxy' => [
    'http'  => 'http://localhost:8125', // Use this proxy with "http"
    'https' => 'http://localhost:9124', // Use this proxy with "https",
    'no' => ['.mit.edu', 'foo.com']    // Don't use a proxy with these
    ]
];

Custom API request

Just plug in the data from the API: https://discord.com/developers/docs/intro

use Naneynonn\Enums\RequestTypes;

$api->request(
  method: RequestTypes::GET,
  endpoint: '/guilds/{guild.id}',
  options: [
    // API parameters
    'params' => [
      'guild.id' => 'guild_id' // or just paste it into the endpoint
    ],
    // For GET requests with additional parameters
    'query' => [
      'foo' => 'bar'
    ],
    // For POST requests with parameters
    'json' => [
      'foo' => 'bar'
    ],
    // Reason field
    'reason' => ''
  ]
  // If you don't want to use the cache, set it to Null
  // ... in seconds
  cache_ttl: 600
);

Config

$config = [
  // Default conf
  'bot' => [
    'token' => '',
  ],

  // If this is true, it will wait until the rate limit is reached and the code will be executed again. False will throw an error and stop.
  'retry' => true

  // If you need proxies
  'proxy' => [
    'http'  => 'http://localhost:8125', // Use this proxy with "http"
    'https' => 'http://localhost:9124', // Use this proxy with "https",
    'no' => ['.mit.edu', 'foo.com']    // Don't use a proxy with these
    ]
];

How to use

This wrapper almost completely copies the data from the original Discord API. All additional information can be found in the Discord documentation. The exception is the name of the resources. Application is shortened to Apps, and some sections are merged into a shorter one, such as Guild.

Clone this wiki locally