dblcore.js Official Documentation

An official documentation for the package dblcore.js

An official documentation for the package dblcore.js

JavaScript Library

An unofficial JavaScript Library for top.gg, if you have any issues please submit an issue on our github.

Installation

npm install dblcore.js

Example

Example of posting server count with supported libraries (Discord.js and Eris)

const Discord = require("discord.js");
const client = new Discord.Client();
const DBL = require("dblcore.js");
const dbl = new DBL('Your top.gg token', client);
// Optional events
dbl.on('posted', () => {
console.log('Server count posted!');
})
dbl.on('error', e => {
console.log(`Oops! ${e}`);
})

Example of using webhooks to receive vote updates

const DBL = require('dblcore.js');
const dbl = new DBL(yourDBLTokenHere, { webhookPort: 5000, webhookAuth: 'password' });
dbl.webhook.on('ready', hook => {
console.log(`Webhook running at http://${hook.hostname}:${hook.port}${hook.path}`);
});
dbl.webhook.on('vote', vote => {
console.log(`User with ID ${vote.user} just voted!`);
});

Constructor

new DBL(token, [options], [client])

or

new DBL(token, [client])

Arguments

Parameter

Type

Optional

Default

Description

token

String

No

Your bot's top.gg token. Obtainable from the My Bots page.

options

Object

Yes

{}

DBL client options listed below

options.statsInterval

Number

Yes

1800000 (30 minutes)

How often the autoposter should post stats in ms. May not be smaller than 900000 (15 minutes).

options.webhookPort

Number

Yes

The port to run the webhook on. Will activate webhook when set.

options.webhookAuth

String

Yes

The string for Authorization you set on the site for verification.

options.webhookPath

String

Yes

'/dblwebhook'

The path for the webhook request.

options.webhookServer

http.Server

Yes

An existing server to run the webhook on. Will activate webhook when set.

client

Client

Yes

A discord.js or Eris client. If set, the module will automatically handle posting of the bot's server count.

If you don't have options to set but want to set the client. You can put the client where options are.

Methods

.postStats(serverCount, [shardId], [shardCount])

Post Stats to Discord Bot List.

Arguments

Parameter

Type

Optional

Description

serverCount

Number

No

The server count of your bot.

shardId

Number

Yes

The ID of this shard.

shardCount

Number

Yes

The count of all shards of your bot.

Returns: Promise

Example

client.on('ready', () => {
setInterval(() => {
dbl.postStats(client.guilds.size, client.shards.Id, client.shards.total);
}, 1800000);
});

.getStats(id)

Gets stats from a bot. See more: Get Bot's Stats

Arguments

Parameter

Type

Optional

Default

Description

id

Snowflake

Yes

The ID of the current bot (requires client to be set)

The ID of the bot you want to get the stats from.

Returns: Promise<Object>

Example

dbl.getStats("264811613708746752").then(stats => {
console.log(stats) // {"server_count":2,"shards":[]}
});

.getBot(id)

Gets information about a bot. See more: Get Bot

Arguments

Parameter

Type

Optional

Default

Description

id

Snowflake

Yes

The ID of the current bot (requires client to be set)

The ID of the bot you want to get the information from.

Returns: Promise<Object>

Example

dbl.getBot("264811613708746752").then(bot => {
console.log(bot.username) // "Luca"
});

.getUser(id)

Gets information about a user. See more: Get User

Arguments

Parameter

Type

Optional

Description

id

Snowflake

No

The ID of the user you want to get the information from.

Returns: Promise<Object>

Example

dbl.getUser("95579865788456960").then(user => {
console.log(user.username) // "Tonkku"
});

.getBots([query])

Gets a list of bots matching your query. See more: Get Bots

Arguments

Parameter

Type

Optional

Default

Description

query

Object

Yes

{}

The query for the search.

query.limit

Number

Yes

50

The amount of bots to return. Max. 500

query.offset

Number

Yes

0

Amount of bots to skip

query.search

String

Yes

A search string in the format of field: value field2: value2

query.sort

String

Yes

The field to sort by. Prefix with - to reverse the order

query.fields

String

Yes

All fields

A comma separated list of fields to show.

Returns: Promise<Object>

Example

dbl.getBots({sort: 'points', limit: 5}).then(bots => {
console.log(bots.results.length) // 5
console.log(bots.results[0].username) // "Pokécord"
});

.getVotes()

Gets votes from your bot. See more: Get Bot's Last 1000 Votes

Returns: Promise<Object>

Example

dbl.getVotes().then(votes => {
if (votes.find(vote => vote.id == "95579865788456960")) console.log("Tonkku has voted!!!")
});

.hasVoted(id)

Returns true if a user has voted for your bot in the last 24h. See more: Individual User Id Voting Check for the past 24 hours.

Arguments

Parameter

Type

Optional

Description

Parameter

Type

Optional

Description

id

Snowflake

No

The ID of the user to check for.

Returns: Promise<Boolean>

Example

dbl.hasVoted("95579865788456960").then(voted => {
if (voted) console.log("Tonkku has voted!!!")
});

.isWeekend()

Returns true if the weekend multiplier is currently active. See more: Voting Multiplier

Returns: Promise<Boolean>

Example

dbl.isWeekend().then(weekend => {
if (weekend) console.log("Woo! Multiplier time!")
});

Autoposter Events

posted

Emitted when the stats have been posted successfully by the autoposter.

Example

dbl.webhook.on('posted', () => {
console.log('Server count posted!');
});

error

Emitted when the autoposter post request failed.

Example

dbl.webhook.on('error', e => {
console.log(`Oops! ${e}`);
});

Webhook Events

ready

Emitted when the webhook is ready to listen.

Parameters

Parameter

Type

Description

Parameter

Type

Description

hostname

String

The hostname of the webhook server

port

Number

The port the webhook server is running on

path

String

The path for the webhook

Example

dbl.webhook.on('ready', hook => {
console.log(`Webhook running at http://${hook.hostname}:${hook.port}${hook.path}`);
});

vote

Emitted when the webhook has received a vote.

Parameters

Parameter

Type

Description

Parameter

Type

Description

bot

Snowflake

Id of the bot that was voted for.

user

Snowflake

Id of the user that voted.

type

String

Type of the vote. Is always "upvote" except when using the test button it's "test".

isWeekend

Boolean

Whether the weekend multiplier is in effect, meaning users votes count as two.

query

Object

The possible querystring parameters from the vote page.

Example

dbl.webhook.on('vote', vote => {
console.log(`User with ID ${vote.user} just voted!`);
});

More examples

Example of linking webhooks with an existing http server (like express)

const DBL = require('dblcore.js');
const express = require('express');
const http = require('http');
const app = express();
const server = http.createServer(app);
const dbl = new DBL(yourDBLTokenHere, { webhookAuth: 'password', webhookServer: server });
dbl.webhook.on('ready', hook => {
console.log(`Webhook running with path ${hook.path}`);
});
dbl.webhook.on('vote', vote => {
console.log(`User with ID ${vote.user} just voted!`);
});
app.get('/', (req, res) => {
// ...
});
server.listen(5000, () => {
console.log('Listening');
});