Skip to content

feat: adapt to v1 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
deps
.vscode
**/deps/
.vscode/
203 changes: 103 additions & 100 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,120 +1,123 @@
# topgg-lua
# Top.gg Lua SDK

The community-maintained Lua library for Top.gg.

## Installation
To install this library, place the topgg folder beside your root folder and require it by using this segment of code:

To install this library, place [the `topgg` directory](https://github.com/top-gg-community/lua-sdk/tree/main/topgg) beside your root directory, then install the following dependencies from the lit repository:

```
creationix/coro-http
luvit/json
luvit/secure-socket
luvit/timer
```

## Setting up

```lua
package.path = './?/init.lua' .. package.path
local topgg = require('topgg')
local topgg = require("topgg");

local botId = "BOT_ID";

topgg.Api:init(os.getenv("TOPGG_TOKEN"), botId);
```
to ensure that it ran successfully, you can run

## Usage

### Getting a bot

```lua
topgg.test()
local bot = topgg.Api:getBot("264811613708746752");
```

## Dependencies
Install the following dependencies from the lit repository:
### Getting several bots

```lua
local bots = topgg.Api:getBots({
sort = "date",
limit = 50,
offset = 0
});
```
creationix/[email protected]
luvit/json
luvit/secure-socket

### Getting your bot's voters

```lua
-- Page number
local voters = topgg.Api:getVotes(1);
```

## Using the library
Start using the API component of the library by using
### Check if a user has voted for your bot

```lua
topgg.Api:init(token, id)
local hasVoted = topgg.Api:hasVoted("661200758510977084");
```

## Example usage
Here we use our `isWeekend()` and `getStats()` method as an example.
### Getting your bot's server count

```lua
local topgg = require('topgg');
local Api = topgg.Api:init('YOUR-TOP.GG-TOKEN-GOES-HERE', 'YOUR-CLIENT-ID-GOES-HERE');
local stats = topgg.Api:getStats();
local serverCount = stats.server_count;
```

local checkWeekend = coroutine.create(function()
print(topgg.Api:isWeekend());
end);
### Posting your bot's server count

coroutine.resume(checkWeekend); -- This will print `false` if it's not the weekends but it'll be `true` when it's the weekends.
```lua
topgg.Api:postStats({
serverCount = bot:getServerCount()
});
```

### Automatically posting your bot's server count every few minutes

With Discordia:

```lua
local discordia = require("discordia");
local client = discordia.Client();

client:on('ready', function()
print(client.user.username .. " is now ready!");

autoposter = topgg.AutoPoster:init(os.getenv("TOPGG_TOKEN"), client);

local getBotStats = coroutine.create(function(id)
print(topgg.Api:getStats(id));
autoposter:on("posted", function()
print("Posted stats to Top.gg!");
end);
end);

coroutine.resume(getBotStats, '716061781172158464'); -- This will print a value that can be encoded into a table by using json.decode()
client:run("Bot " .. os.getenv("BOT_TOKEN"));
```

### Checking if the weekend vote multiplier is active

```lua
local isWeekend = topgg.Api:isWeekend();
```

### Generating widget URLs

#### Large

```lua
local widgetUrl = topgg.Widget.large("discord_bot", "574652751745777665");
```

#### Votes

```lua
local widgetUrl = topgg.Widget.votes("discord_bot", "574652751745777665");
```

## Documentation (Api)
`Api:init(token, id)`
Params | Type | Required | Description
--- | --- | --- | ---
`token` | `string` | ✅ | Your top.gg token
`id` | `string` | ✅ | Your client ID
---

`Api:request(method, path, body, query)`
Params | Type | Required | Description
--- | --- | --- | ---
`method` | `string` | ✅ | The HTTP request method (e.g. `GET`)
`path` | `string` | ✅ | The top.gg endpoint
`body` | `table` | ❌ | The payload to send while making a `POST`, `PATCH` or `PUT` request
`query` | `table` | ❌ | The query for the passed endpoint
---

`Api:commit(method, url, req, body)`
Params | Type | Required | Description
--- | --- | --- | ---
`method` | `string` | ✅ | The HTTP request method (e.g. `GET`)
`url` | `string` | ✅ | The URL to make a request to
`req` | `table` | ❌ | The headers of the request
`body` | `table` | ❌ | The payload to send while making a `POST`, `PATCH` or `PUT` request
---

`Api:postStats(stats)`
Params | Type | Required | Description
--- | --- | --- | ---
`stats` | `table` | ✅ | The stats object
`stats.serverCount` / `stats.server_count` | `number` | ✅ | The client's server count
`stats.shardId` / `stats.shard_id` | `number` | ❌ | The client's shard ID
`stats.shardCount` / `stats.shard_count` | `number` | ❌ | The client's shard count
---

`Api:getStats(id)`
Params | Type | Required | Description
--- | --- | --- | ---
`id` | `string` | ✅ | The ID of the bot to get stats of
---

`Api:getBot(id)`
Params | Type | Required | Description
--- | --- | --- | ---
`id` | `string` | ✅ | The ID of the bot to get information of
---

`Api:getBots(query)`
Params | Type | Required | Description
--- | --- | --- | ---
`query` | `table` | ❌ | The query object
`query.fields` | `any` | ❌ | The fields of the query
`query.search` | `any` | ❌ | The search query
---

`Api:getUser(id)`
Params | Type | Required | Description
--- | --- | --- | ---
`id` | `string` | ✅ | The ID of the user to get information of (top.gg user info)
---

`Api:hasVoted(id)`
Params | Type | Required | Description
--- | --- | --- | ---
`id` | `string` | ✅ | The ID of the user to check if they have voted for the bot the `Api` class was invoked with
---

`Api:getVotes()`<br>No params.

---

`Api:isWeekend()`<br>No params.

## Contributors
[Voltrex](https://github.com/VoltrexMaster)<br>[Matthew.](https://github.com/matthewthechickenman)<br>[MILLION](https://github.com/Million900o)<br>[null](https://github.com/vierofernando)
#### Owner

```lua
local widgetUrl = topgg.Widget.owner("discord_bot", "574652751745777665");
```

#### Social

```lua
local widgetUrl = topgg.Widget.social("discord_bot", "574652751745777665");
```
14 changes: 0 additions & 14 deletions rootfile.lua

This file was deleted.

3 changes: 2 additions & 1 deletion topgg/init.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package.path = './deps/?/init.lua;./deps/?.lua;./topgg/lib/?.lua;./deps/secure-socket/?.lua;' .. package.path;
return {
Api = require('api'),
Autoposter = require('autoposter'),
AutoPoster = require('autoposter'),
Widget = require('widget'),
test = require('test')
}
3 changes: 0 additions & 3 deletions topgg/lib/EventEmitter.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
local timer = require('timer');

local wrap, yield = coroutine.wrap, coroutine.yield;
local resume, running = coroutine.resume, coroutine.running;
local insert, remove = table.insert, table.remove;
local setTimeout, clearTimeout = timer.setTimeout, timer.clearTimeout;

local EventEmitter = require('class')('EventEmitter');

Expand Down
Loading