Statsbot - Discord

Discord bot that creates Rest API and SVG Badges

dependenciesversion commit license

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

Setup

Follow these steps if you did not deploy to Heroku

Create an .env file to store your environment

.env
TOKEN=NjMzdTQ0NDY5gTU5Mzc3OTdw.Xg-GNQ.CCePaYCt44kja_d3OWTPJaiBRCY
PORT=5000
ACTIVITY=andre.gg/statsbot

Download Node.js and your required dependencies

npm install

Lastly to run the app run:

node app.js

Use the endpoint /invite to add the bot to your server!

Test Links

https://statsbot-test.herokuapp.com

https://statsbot-test.herokuapp.com/api/bot/stats

https://statsbot-test.herokuapp.com/server/650903145739976704

https://statsbot-test.herokuapp.com/api/server/650903145739976704

https://statsbot-test.herokuapp.com/badge/owner/650903145739976704.svg

https://statsbot-test.herokuapp.com/badge/members/650903145739976704.svg

get
Server Stats

https://example.com/api/server/:id
This returns info about the server that is specified in the "id" parameter
Request
Response
Request
Path Parameters
id
required
number
ID of the server
Response
200: OK
{
"id": "650903145739976704",
"name": "testing",
"memberCount": 2,
"online": 1,
"channels": 4,
"createdAt": "2019-12-02T03:36:43.140Z",
"createdTimestamp": 1575257803140,
"emojis": 1,
"iconURL": "https://cdn.discordapp.com/icons/650903145739976704/7d4ee07f8bf3acf5e5341a233995c758.jpg",
"large": false,
"owner": {
"displayName": "andrejarrell",
"tag": "andrejarrell#1942",
"nickname": null,
"presence": {
"status": "offline",
"game": null,
"clientStatus": { }
},
"avatarURL": "https://cdn.discordapp.com/avatars/355518701056950283/d6668ed29dca464398fd1872bd35ea0a.png?size=2048"
},
"region": "us-south",
"roles": 1,
"verified": false
}
404: Not Found
{
"error": "server unavailable"
}
500: Internal Server Error
{
"error": "internal server error"
}

Caching has been set to 5 minutes by default. You may change this in the code.

get
Server Page

https://example.com/server/:id
Renders page for server
Request
Response
Request
Path Parameters
id
optional
string
ID of the server
Response
200: OK
Returns rendered page with ejs
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1">
<link rel="shortcut icon" href="https://statsbot-test.herokuapp.com/assets/images/512x512-128x128.png" type="image/x-icon">
<title>testing</title>
<link rel="stylesheet" href="https://statsbot-test.herokuapp.com/assets/web/assets/mobirise-icons/mobirise-icons.css">
<link rel="stylesheet" href="https://statsbot-test.herokuapp.com/assets/tether/tether.min.css">
<link rel="stylesheet" href="https://statsbot-test.herokuapp.com/assets/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" href="https://statsbot-test.herokuapp.com/assets/bootstrap/css/bootstrap-reboot.min.css">
<link rel="stylesheet" href="https://statsbot-test.herokuapp.com/assets/bootstrap/css/bootstrap-grid.min.css">
<link rel="stylesheet" href="https://statsbot-test.herokuapp.com/assets/theme/css/style.css">
<link rel="stylesheet" href="https://statsbot-test.herokuapp.com/assets/formoid-css/recaptcha.css">
<link rel="preload" as="style" href="https://statsbot-test.herokuapp.com/assets/mobirise/css/mbr-additional.css">
<link rel="stylesheet" href="https://statsbot-test.herokuapp.com/assets/mobirise/css/mbr-additional.css" type="text/css">
</head>
<body>
<section class="header4 cid-rMzqLfELrq mbr-fullscreen mbr-parallax-background" id="header4-5">
<div class="mbr-overlay" style="opacity: 0.8;">
</div>
<div class="container align-center">
<div class="row justify-content-center">
<div class="media-container-column mbr-white col-md-10">
<div class="align-center mb-4">
<img class="server-image" src="https://cdn.discordapp.com/icons/650903145739976704/7d4ee07f8bf3acf5e5341a233995c758.jpg">
</div>
<h1 class="mbr-section-title pb-3 mbr-fonts-style display-1"><strong>testing</strong></h1>
<div class="align-center mb-4">
<img class="statsbot-badge" src="https://statsbot-test.herokuapp.com/badge/members/650903145739976704.svg">
<img class="statsbot-badge" src="https://statsbot-test.herokuapp.com/badge/owner/650903145739976704.svg">
</div>
<h3 class="mbr-section-subtitle align-center mbr-light pb-3 mbr-fonts-style display-5">Roles: 1<br>Online: 2<br>Channels: 4<br>Region: us-south</h3>
</div>
</div>
</div>
</section>
<script src="https://statsbot-test.herokuapp.com/assets/web/assets/jquery/jquery.min.js"></script>
<script src="https://statsbot-test.herokuapp.com/assets/popper/popper.min.js"></script>
<script src="https://statsbot-test.herokuapp.com/assets/tether/tether.min.js"></script>
<script src="https://statsbot-test.herokuapp.com/assets/bootstrap/js/bootstrap.min.js"></script>
<script src="https://statsbot-test.herokuapp.com/assets/smoothscroll/smooth-scroll.js"></script>
<script src="https://statsbot-test.herokuapp.com/assets/parallax/jarallax.min.js"></script>
<script src="https://statsbot-test.herokuapp.com/assets/theme/js/script.js"></script>
<script src="https://statsbot-test.herokuapp.com/assets/formoid/formoid.min.js"></script>
</body>
</html>

get
Client Stats

https://example.com/api/bot/stats
This returns information about the bot
Request
Response
Request
Response
200: OK
{
"users": 3,
"servers": 1,
"channels": 4,
"ping": 35
}

Ajax

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

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>
script.js
// Using jquery to add text
$(document).ready(() => {
$.get('https://example.com/server/650903145739976704', d => {
$('.name').text(d.name);
$('.online').text(`${d.members} Members`);
$('.pic').attr('src', d.iconURL);
$('.owner').text(`Owner: ${d.displayName}`);
});
});

If you are running into a Access-Control-Allow-Origin error when using ajax make sure the server has this header set:

Access-Control-Allow-Origin: *

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

index.js
var request = require('request');
request('https://example.com/server/650903145739976704', (err, resp, body) => {
console.log('error:', err);
console.log('statusCode:', resp && resp.statusCode);
console.log('body:', body);
});

Badges

get
Server Badge

https://example.com/badge/:type/:id.svg
Renders a svg badge for your server
Request
Response
Request
Path Parameters
id
required
number
ID of the server
type
required
string
Choose either "members" or "owner"
Query Parameters
color
optional
string
Hexadecimal color
Response
200: OK
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="264.5" height="28">
<g shape-rendering="crispEdges">
<path fill="#232323" d="M0 0h87.5v28H0z"/>
<path fill="#0080ff" d="M87.5 0h177v28H87.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,PHN2ZyB2aWV3Qm94PSIwIDAgMjQgMjQiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgZmlsbD0id2hpdGUiPjxwYXRoIGQ9Ik00LjMzNCA0LjYzNmMxLjM1MS0uNzI1IDIuODUyLTEuMjIyIDQuMzkzLTEuMjY4bC4yMDUuMjVjLTEuMzYzLjM5OS0yLjY5My45MjUtMy45MiAxLjY0NS0uNTA3LjMxMi0xLjAyMy42MjQtMS40NjIgMS4wMjkgMS4xMDYtLjU1MyAyLjIzNy0xLjA3OSAzLjQzOC0xLjM5NC42NDgtLjE4MyAxLjMwMi0uMzUxIDEuOTcyLS40MjggMS42MDQtLjIyNSAzLjIzNi0uMzI1IDQuODUtLjE0NGExNy42MzcgMTcuNjM3IDAgMCAxIDMuMzk1LjczMiAxOC41OTYgMTguNTk2IDAgMCAxIDIuODA1IDEuMjAxYy0uMjQtLjI0My0uNTI0LS40MzQtLjgwNi0uNjIyLTEuMjk1LS44NjQtMi43NDEtMS40OC00LjIyNC0xLjk0LjEzMy0uMTIzLjIyLS4zNjkuNDQxLS4zMyAxLjk0Ny4xMjQgMy44MTkuODg2IDUuNCAyLjAxLjExNC4wNzQuMjQzLjE0NC4yOTEuMjhhMjguMzMzIDI4LjMzMyAwIDAgMSAyLjIwOCA2LjIwMWMuNDE0IDEuOC42NTIgMy42NDIuNjc4IDUuNDg5LjAwNS4wOTIuMDA1LjE5Mi0uMDYuMjY3LTEuMDUgMS40NjYtMi43MDkgMi40MTctNC40NTEgMi44MDVhOS4yNTQgOS4yNTQgMCAwID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LS41NDRtNy4zNzYuMDE4Yy0xLjAxOC4xOS0xLjc0OCAxLjE5Ni0xLjczNSAyLjIxMS0uMDEzLjguMzg2IDEuNjE1IDEuMDggMi4wMzQuNDYuMjk0IDEuMDUuMzU2IDEuNTcuMi41NzktLjE2IDEuMDUtLjYxIDEuMzA0LTEuMTQ0LjQ2Mi0uOTQuMjMtMi4xODYtLjU3OS0yLjg2M2ExLjk5NCAxLjk5NCAwIDAgMC0xLjY0LS40Mzh6Ii8+PC9zdmc+"/>
<text x="522.5" y="175" transform="scale(.1)" textLength="465">OWNER</text>
<text x="1760" y="175" font-weight="bold" transform="scale(.1)" textLength="1530">ANDREJARRELL#1942</text>
</g>
</svg>
404: Not Found
{
"error": "server unavailable"
}
500: Internal Server Error
{
"error": "internal server error"
}