Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
211 lines (170 sloc) 8.36 KB
description
An open source Rest API bot that retrieves data from your Discord server. Features SVG Badges!

Statsbot - Discord

dependenciesversion commit license

{% embed url="https://andre.gg/git/statsbot" caption="Source code is available here" %}

{% embed url="https://elements.heroku.com/buttons/andrejarrell/statsbot" caption="You may deploy the bot directly to Heroku with this link" %}

Intro

Statsbot was created with express.js and acts as a RESTful API. Each function of the bot has an endpoint.

{% api-method method="get" host="https://example.com" path="/servers/:id.json" %} {% api-method-summary %} Server Stats {% endapi-method-summary %}

{% api-method-description %} This returns info about the server that is specified in the "id" parameter {% endapi-method-description %}

{% api-method-spec %} {% api-method-request %} {% api-method-path-parameters %} {% api-method-parameter name="id" type="number" required=true %} Id of the server {% endapi-method-parameter %} {% endapi-method-path-parameters %} {% endapi-method-request %}

{% api-method-response %} {% api-method-response-example httpCode=200 %} {% api-method-response-example-description %}

{% endapi-method-response-example-description %}

{
    id: "553408019120128020",
    name: "Tech Central",
    members: 79,
    online: 34,
    channels: 29,
    createdAt: "2019-03-08T02:45:53.582Z",
    createdTimestamp: 1552013153582,
    emojis: 23,
    iconURL: "https://cdn.discordapp.com/icons/553408019120128020/2bdca74734ed8aafaafb187a6ac01897.jpg",
    large: false,
    owner: {
        displayName: "andrejarrell",
        nickname: null,
        presence: {
            status: "online",
            game: null,
        },
        avatarURL: "https://cdn.discordapp.com/avatars/355518701056950283/04715f1dbc611a21689858db81bc5697.png?size=2048",
    },
    region: "us-east",
    roles: 16,
    verified: false,
}

{% endapi-method-response-example %} {% endapi-method-response %} {% endapi-method-spec %} {% endapi-method %}

{% hint style="info" %} Caching has been set to 5 minutes by default. You may change this in the code. {% endhint %}

{% api-method method="get" host="https://example.com" path="/client/stats.json" %} {% api-method-summary %} Client Stats {% endapi-method-summary %}

{% api-method-description %} This returns information about the bot {% endapi-method-description %}

{% api-method-spec %} {% api-method-request %} {% api-method-path-parameters %} {% api-method-parameter name="" type="string" required=false %}

{% endapi-method-parameter %} {% endapi-method-path-parameters %} {% endapi-method-request %}

{% api-method-response %} {% api-method-response-example httpCode=200 %} {% api-method-response-example-description %}

{% endapi-method-response-example-description %}

{
    users: 107,
    servers: 1,
    channels: 29,
    ping: 15,
}

{% endapi-method-response-example %} {% endapi-method-response %} {% endapi-method-spec %} {% endapi-method %}

Ajax

With Ajax you can make request to the API within the browser

{% code title="index.js" %}

<!-- Place in your head tag -->
<script src="script.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<!-- Place in your head tag -->
<h1 class="name"></h1>
<p class="online"></p>
<img class="pic">
<p class="owner"></p>

{% endcode %}

{% code title="script.js" %}

// Using jquery to add text
$(document).ready(() => {
  $.get('https://example.com/servers/553408019120128020.json', data => {
    $('.name').text(data.name);
    $('.online').text(`${data.members} Members`);
    $('.pic').attr('src', data.iconURL);
    $('.owner').text(`Owner: ${data.owner.displayName}`);
  });
});

{% endcode %}

Request

Another option is to use the request module that is available in node.js. You can install it with this command: npm i request

{% code title="index.js" %}

var request = require('request');
request('https://example.com/servers/553408019120128020.json', (error, response, body) => {
  console.log('error:', error);
  console.log('statusCode:', response && response.statusCode);
  console.log('body:', body);
});

{% endcode %}

You can run this code with: node index.js

Badges

{% api-method method="get" host="https://example.com" path="/badge/:type/:id.svg" %} {% api-method-summary %} Server Badge {% endapi-method-summary %}

{% api-method-description %} Renders a svg badge for your server {% endapi-method-description %}

{% api-method-spec %} {% api-method-request %} {% api-method-path-parameters %} {% api-method-parameter name="id" type="number" required=true %} Id of the server {% endapi-method-parameter %}

{% api-method-parameter name="type" type="string" required=true %} Choose either "members" or "owner" {% endapi-method-parameter %} {% endapi-method-path-parameters %}

{% api-method-query-parameters %} {% api-method-parameter name="color" type="string" required=false %} Hexadecimal color {% endapi-method-parameter %} {% endapi-method-query-parameters %} {% endapi-method-request %}

{% api-method-response %} {% api-method-response-example httpCode=200 %} {% api-method-response-example-description %}

{% endapi-method-response-example-description %}

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="149.5" height="28"><g shape-rendering="crispEdges"><path fill="#232323" d="M0 0h102.5v28H0z"/><path fill="#0080ff" d="M102.5 0h47v28H102.5z"/></g><g fill="#fff" text-anchor="middle" font-family="DejaVu Sans,Verdana,Geneva,sans-serif" font-size="100"><image x="9" y="7" width="14" height="14" xlink:href="data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbD0id2hpdGUiPjxwYXRoIGQ9Ik00LjMzNCA0LjYzNmMxLjM1MS0uNzI1IDIuODUyLTEuMjIyIDQuMzkzLTEuMjY4bC4yMDUuMjVjLTEuMzYzLjM5OS0yLjY5My45MjUtMy45MiAxLjY0NS0uNTA3LjMxMi0xLjAyMy42MjQtMS40NjIgMS4wMjkgMS4xMDYtLjU1MyAyLjIzNy0xLjA3OSAzLjQzOC0xLjM5NC42NDgtLjE4MyAxLjMwMi0uMzUxIDEuOTcyLS40MjggMS42MDQtLjIyNSAzLjIzNi0uMzI1IDQuODUtLjE0NGExNy42MzcgMTcuNjM3IDAgMCAxIDMuMzk1LjczMiAxOC41OTYgMTguNTk2IDAgMCAxIDIuODA1IDEuMjAxYy0uMjQtLjI0My0uNTI0LS40MzQtLjgwNi0uNjIyLTEuMjk1LS44NjQtMi43NDEtMS40OC00LjIyNC0xLjk0LjEzMy0uMTIzLjIyLS4zNjkuNDQxLS4zMyAxLjk0Ny4xMjQgMy44MTkuODg2IDUuNCAyLjAxLjExNC4wNzQuMjQzLjE0NC4yOTEuMjhhMjguMzMzIDI4LjMzMyAwIDAgMSAyLjIwOCA2LjIwMWMuNDE0IDEuOC42NTIgMy42NDIuNjc4IDUuNDg5LjAwNS4wOTIuMDA1LjE5Mi0uMDYuMjY3LTEuMDUgMS40NjYtMi43MDkgMi40MT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TIuNzg1LS40My0uNC0xLjAzNi0uNjI2LTEuNjI0LS41NDRtNy4zNzYuMDE4Yy0xLjAxOC4xOS0xLjc0OCAxLjE5Ni0xLjczNSAyLjIxMS0uMDEzLjguMzg2IDEuNjE1IDEuMDggMi4wMzQuNDYuMjk0IDEuMDUuMzU2IDEuNTcuMi41NzktLjE2IDEuMDUtLjYxIDEuMzA0LTEuMTQ0LjQ2Mi0uOTQuMjMtMi4xODYtLjU3OS0yLjg2M2ExLjk5NCAxLjk5NCAwIDAgMC0xLjY0LS40Mzh6Ii8+PC9zdmc+"/> <text x="597.5" y="175" transform="scale(.1)" textLength="615">MEMBERS</text><text x="1260" y="175" font-weight="bold" transform="scale(.1)" textLength="230">100</text></g> </svg>

{% endapi-method-response-example %} {% endapi-method-response %} {% endapi-method-spec %} {% endapi-method %}

You can’t perform that action at this time.