GitHunt
NU

null8626/php-sdk

A simple Top.gg API wrapper written in PHP.

Top.gg PHP SDK

The community-maintained PHP library for Top.gg.

Chapters

Installation

$ composer require top-gg/php-sdk

Setting up

API v1

NOTE: API v1 also includes API v0.

include_once __DIR__ . "/vendor/autoload.php";

use DBL\V1DBL;

$client = new V1DBL([
  "token" => "Top.gg API token"
]);

API v0

include_once __DIR__ . "/vendor/autoload.php";

use DBL\DBL;

$client = new DBL([
  "token" => "Top.gg API token"
]);

Usage

API v1

Getting your project's vote information of a user

$vote = $client->get_vote(661200758510977084);

Posting your bot's application commands list

$client->post_commands([
  [
    "options" => [],
    "name" => "test",
    "name_localizations" => null,
    "description" => "command description",
    "description_localizations" => null,
    "contexts" => [],
    "default_permission" => null,
    "default_member_permissions" => null,
    "dm_permission" => false,
    "integration_types" => [],
    "nsfw" => false
  ]
]);

API v0

Getting a bot

$bot = $client->get_bot(1026525568344264724);

Getting several bots

//                        Limit  Offset  Sort by
$bots = $client->get_bots(50,    0,      "monthlyPoints");

Getting your project's voters

First page
$voters = $client->get_votes();
Subsequent pages
//                           Bot ID (unused)  Page number
$voters = $client->get_votes(0,               2);

Check if a user has voted for your project

//                                  Bot ID (unused)  User ID
$has_voted = $client->get_user_vote(0,               661200758510977084);

Getting your bot's statistics

$stats = $client->get_stats();

Posting your bot's statistics

$client->post_stats(0, [
  "server_count" => $bot->get_server_count()
]);

Automatically posting your bot's statistics every few minutes

NOTE: Considering PHP's shortcomings, this is only possible via a URL.

In your original client declaration:

$client = new DBL([
  "token" => "Insert your Top.gg API token here.",
  "auto_stats" => [
    "url": "Your URL",
    "callback": => function () use ($bot) {
      return [
        "server_count" => $bot->get_server_count()
      ];
    }
  ]
]);

Checking if the weekend vote multiplier is active

$is_weekend = $client->is_weekend();

Generating widget URLs

Large
use DBL\Widget;
use DBL\WidgetType;

$widget_url = Widget::large(WidgetType::DiscordBot, 574652751745777665);
Votes
use DBL\Widget;
use DBL\WidgetType;

$widget_url = Widget::votes(WidgetType::DiscordBot, 574652751745777665);
Owner
use DBL\Widget;
use DBL\WidgetType;

$widget_url = Widget::owner(WidgetType::DiscordBot, 574652751745777665);
Social
use DBL\Widget;
use DBL\WidgetType;

$widget_url = Widget::social(WidgetType::DiscordBot, 574652751745777665);

Webhooks

Being notified whenever someone voted for your project

With Laravel:

In your config/logging.php:

"channels" => [
  "stdout" => [
    "driver" => "single",
    "path" => "php://stdout",
    "level" => "debug"
  ]
]

In your routes/api.php:

use DBL\Webhook;

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Log;

class MyVoteListener extends Webhook {
  public function getAuthorization() {
    return getenv("MY_TOPGG_WEBHOOK_SECRET");
  }

  public function callback(array $vote) {
    Log::channel("stdout")->info("A user with the ID of " . $vote["user"] . " has voted us on Top.gg!");
  }
}

Route::post('/votes', [MyVoteListener::class, "main"]);