Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
26a58c2
fix: fix GET /bots/votes not working
null8626 May 25, 2025
d2cb2ce
feat: add widgets
null8626 Jun 17, 2025
2327b15
feat: add small widgets
null8626 Jun 18, 2025
13982a5
style: remove trailing comma
null8626 Jun 23, 2025
46ef8d1
docs: readme overhaul
null8626 Jun 23, 2025
db99c59
feat: make webhooks more specific
null8626 Jun 24, 2025
327e15b
feat: remove features deprecated by v0
null8626 Jun 24, 2025
1a03b27
feat: remove BotStats
null8626 Jul 4, 2025
ed43118
deps: remove the dependence on @top-gg/eslint-config
null8626 Jul 16, 2025
b27d294
feat: adapt to v1
null8626 Sep 10, 2025
1b2027c
*: minor tweaks
null8626 Sep 12, 2025
157a44c
[doc,revert]: rename WebhookVotePayload to WebhookPayload, TopggAPIEr…
null8626 Sep 13, 2025
4192bd3
refactor: remove redundant checks
null8626 Sep 13, 2025
8f986e1
feat: separate v1 from v0
null8626 Sep 13, 2025
6d866f5
refactor: use Bearer prefix for legacy and new tokens
null8626 Sep 15, 2025
66adfe3
doc: add @see Webhook#voteListener
null8626 Sep 15, 2025
3a234ad
doc: update documentation and readme
null8626 Sep 15, 2025
b2652b2
meta: make SDK description clearer
null8626 Sep 15, 2025
89be620
revert: revert breaking changes to webhooks
null8626 Sep 16, 2025
6fe8cf4
revert: revert breaking changes
null8626 Sep 18, 2025
840abb8
doc: update readme
null8626 Oct 3, 2025
15fb73a
ci: bump github workflow dependency versions
null8626 Oct 7, 2025
1ddf708
doc: fix less accurate naming
null8626 Oct 10, 2025
59c5257
doc: objects, not dicts
null8626 Oct 10, 2025
aa408ac
doc: documentation tweaks
null8626 Oct 10, 2025
9f88be3
feat: rename postBotCommands to postCommands
null8626 Oct 10, 2025
eb64cb1
doc: readme tweak
null8626 Oct 10, 2025
2c159e0
doc: fix API version links in README.md
null8626 Oct 20, 2025
40cae1c
doc: remove for more information link from README
null8626 Oct 21, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 0 additions & 7 deletions .eslintrc.js

This file was deleted.

6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ jobs:
matrix:
node: [18, 20]
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5
- name: Cache node_modules
uses: actions/cache@v3
uses: actions/cache@v4
env:
cache-name: cache-node-modules
with:
Expand All @@ -26,7 +26,7 @@ jobs:
${{ runner.os }}-build-
${{ runner.os }}-
- name: Setup Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v5
with:
node-version: ${{ matrix.node }}
check-latest: true
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ jobs:
publish:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v5
- uses: actions/setup-node@v5
with:
node-version: 18
check-latest: true
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v5
- name: Use Node.js
uses: actions/setup-node@v3
uses: actions/setup-node@v5
with:
node-version: 18
check-latest: true
Expand Down
5 changes: 1 addition & 4 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npm run lint
npm run lint
2 changes: 1 addition & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ logs/
*.log

!dist/
.eslintrc.json
eslint.config.js
.gitattributes
.gitignore
.github/
Expand Down
276 changes: 242 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,62 +1,270 @@
# Top.gg Node SDK
# Top.gg Node.js SDK

An official module for interacting with the Top.<span>gg API
The community-maintained Node.js library for Top.gg.

# Installation
## Chapters

`yarn add @top-gg/sdk` or `npm i @top-gg/sdk`
- [Installation](#installation)
- [Setting up](#setting-up)
- [Usage](#usage)
- [API v1](#api-v1-1)
- [Getting your project's vote information of a user](#getting-your-projects-vote-information-of-a-user)
- [Posting your bot's application commands list](#posting-your-bots-application-commands-list)
- [API v0](#api-v0-1)
- [Getting a bot](#getting-a-bot)
- [Getting several bots](#getting-several-bots)
- [Getting your project's voters](#getting-your-projects-voters)
- [Check if a user has voted for your project](#check-if-a-user-has-voted-for-your-project)
- [Getting your bot's statistics](#getting-your-bots-statistics)
- [Posting your bot's statistics](#posting-your-bots-statistics)
- [Automatically posting your bot's statistics every few minutes](#automatically-posting-your-bots-statistics-every-few-minutes)
- [Checking if the weekend vote multiplier is active](#checking-if-the-weekend-vote-multiplier-is-active)
- [Generating widget URLs](#generating-widget-urls)
- [Webhooks](#webhooks)
- [Being notified whenever someone voted for your project](#being-notified-whenever-someone-voted-for-your-project)

# Introduction
## Installation

The base client is Topgg.Api, and it takes your Top.gg token and provides you with plenty of methods to interact with the API.
### NPM

See [this tutorial](https://github.com/top-gg/rust-sdk/assets/60427892/d2df5bd3-bc48-464c-b878-a04121727bff) on how to retrieve your API token.
```sh
$ npm i @top-gg/sdk
```

### Yarn

You can also setup webhooks via Topgg.Webhook, look down below at the examples for how to do so!
```sh
$ yarn add @top-gg/sdk
```

# Links
## Setting up

[Documentation](https://topgg.js.org)
### API v1

[API Reference](https://docs.top.gg) | [GitHub](https://github.com/top-gg/node-sdk) | [NPM](https://npmjs.com/package/@top-gg/sdk) | [Discord Server](https://discord.gg/EYHTgJX)
> **NOTE**: API v1 also includes API v0.

# Popular Examples
```js
import Topgg from "@top-gg/sdk";

## Auto-Posting stats
const client = new Topgg.V1Api(process.env.TOPGG_TOKEN);
```

If you're looking for an easy way to post your bot's stats (server count, shard count), check out [`topgg-autoposter`](https://npmjs.com/package/topgg-autoposter)
### API v0

```js
const client = Discord.Client(); // Your discord.js client or any other
const { AutoPoster } = require("topgg-autoposter");
import Topgg from "@top-gg/sdk";

const client = new Topgg.Api(process.env.TOPGG_TOKEN);
```

## Usage

### API v1

#### Getting your project's vote information of a user

##### Discord ID

```js
const vote = await client.getVote("661200758510977084");
```

##### Top.gg ID

```js
const vote = await client.getVote("8226924471638491136", "topgg");
```

#### Posting your bot's application commands list

##### Discord.js

```js
const commands = (await bot.application.commands.fetch()).map(cmd => cmd.toJSON());

await client.postCommands(commands);
```

##### Eris

```js
const commands = await bot.getCommands();

await client.postCommands(commands);
```

AutoPoster("topgg-token", client).on("posted", () => {
console.log("Posted stats to Top.gg!");
##### Discordeno

```js
import { getApplicationCommands } from "discordeno";

const commands = await getApplicationCommands(bot);

await client.postCommands(commands);
```

##### Harmony

```js
const commands = await bot.interactions.commands.all();

await client.postCommands(commands);
```

##### Oceanic

```js
const commands = await bot.application.getGlobalCommands();

await client.postCommands(commands);
```

##### Raw

```js
await client.postCommands([
{
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

```js
const bot = await client.getBot("461521980492087297");
```

#### Getting several bots

```js
const bots = await client.getBots();
```

#### Getting your project's voters

##### First page

```js
const voters = await client.getVoters();
```

##### Subsequent pages

```js
const voters = await client.getVoters(2);
```

#### Check if a user has voted for your project

```js
const hasVoted = await client.hasVoted("661200758510977084");
```

#### Getting your bot's statistics

```js
const stats = await client.getStats();
```

#### Posting your bot's statistics

```js

await api.postStats({
serverCount: bot.getServerCount(),
});
```

#### Automatically posting your bot's statistics every few minutes

You would need to use the third-party `topgg-autoposter` package to be able to autopost. Install it in your terminal like so:

##### NPM

```sh
$ npm i topgg-autoposter
```

##### Yarn

```sh
$ yarn add topgg-autoposter
```

Then in your code:

```js
import { AutoPoster } from "topgg-autoposter";

// Your discord.js client or any other
const client = Discord.Client();

AutoPoster(process.env.TOPGG_TOKEN, client).on("posted", () => {
console.log("Successfully posted statistics to Top.gg!");
});
```

With this your server count and shard count will be posted to Top.<span>gg
#### Checking if the weekend vote multiplier is active

```js
const isWeekend = await client.isWeekend();
```

#### Generating widget URLs

##### Large

```js
const widgetUrl = Topgg.Widget.large(Topgg.WidgetType.DiscordBot, "574652751745777665");
```

## Webhook server
##### Votes

```js
const express = require("express");
const Topgg = require("@top-gg/sdk");
const widgetUrl = Topgg.Widget.votes(Topgg.WidgetType.DiscordBot, "574652751745777665");
```

const app = express(); // Your express app
##### Owner

const webhook = new Topgg.Webhook("topggauth123"); // add your Top.gg webhook authorization (not bot token)
```js
const widgetUrl = Topgg.Widget.owner(Topgg.WidgetType.DiscordBot, "574652751745777665");
```

app.post(
"/dblwebhook",
webhook.listener((vote) => {
// vote is your vote object
console.log(vote.user); // 221221226561929217
})
); // attach the middleware
##### Social

app.listen(3000); // your port
```js
const widgetUrl = Topgg.Widget.social(Topgg.WidgetType.DiscordBot, "574652751745777665");
```

With this example, your webhook dashboard (`https://top.gg/bot/{your bot's id}/webhooks`) should look like this:
![](https://i.imgur.com/cZfZgK5.png)
### Webhooks

#### Being notified whenever someone voted for your project

With express:

```js
import { Webhook } from "@top-gg/sdk";
import express from "express";

const app = express();
const webhook = new Webhook(process.env.TOPGG_WEBHOOK_PASSWORD);

app.post("/votes", webhook.listener(vote => {
console.log(`A user with the ID of ${vote.user} has voted us on Top.gg!`);
}));

app.listen(8080);
```
Loading
Loading