discordthings-api 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +65 -0
  2. package/index.js +72 -0
  3. package/package.json +26 -0
package/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # DiscordThings API Wrapper
2
+
3
+ Official NPM package for interacting with the **DiscordThings.com** API.
4
+ Easily post bot stats and check user votes.
5
+
6
+ ## Installation
7
+
8
+ ```bash
9
+ npm install discordthings-api
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ First, get your **API Token** from your User Profile on DiscordThings.
15
+
16
+ ```javascript
17
+ const DiscordThings = require('discordthings-api');
18
+
19
+ // Initialize with your User API Token
20
+ const dthings = new DiscordThings('YOUR_API_TOKEN');
21
+
22
+ const BOT_ID = 'YOUR_BOT_ID';
23
+
24
+ async function syncStats() {
25
+ try {
26
+ // Post Server Count (and optional Shard Count)
27
+ await dthings.postStats(BOT_ID, 1500, 2);
28
+ console.log('Stats posted successfully!');
29
+ } catch (error) {
30
+ console.error('Failed to post stats:', error.message);
31
+ }
32
+ }
33
+
34
+ async function handleVoteCheck(userId) {
35
+ try {
36
+ // Check if a user voted
37
+ const result = await dthings.checkVote(BOT_ID, userId);
38
+
39
+ if (result.voted) {
40
+ console.log(`User ${userId} has voted!`);
41
+ console.log(`Next vote allowed at: ${result.nextVote}`);
42
+ } else {
43
+ console.log(`User ${userId} has not voted yet.`);
44
+ }
45
+ } catch (error) {
46
+ console.error('Error checking vote:', error.message);
47
+ }
48
+ }
49
+
50
+ syncStats();
51
+ ```
52
+
53
+ ## Methods
54
+
55
+ ### `constructor(token, options?)`
56
+ - `token`: Your API Token from the dashboard.
57
+ - `options.baseUrl`: Optional (default: `https://discordthings.com/api`).
58
+
59
+ ### `postStats(botId, serverCount, shardCount?)`
60
+ - Updates the server and shard count for the specified bot.
61
+ - **Note:** You must be the owner of the bot associated with the API Token.
62
+
63
+ ### `checkVote(botId, userId)`
64
+ - Checks if a specific user has voted for the bot in the last 12 hours.
65
+ - Returns: `{ voted: boolean, lastVote: Date|null, nextVote: Date|null }`.
package/index.js ADDED
@@ -0,0 +1,72 @@
1
+ const axios = require('axios');
2
+
3
+ class DiscordThings {
4
+ /**
5
+ * @param {string} token - Your Bot's API Token (from User Profile)
6
+ * @param {Object} options - Optional settings
7
+ * @param {string} [options.baseUrl='https://discordthings.com/api'] - API Base URL
8
+ */
9
+ constructor(token, options = {}) {
10
+ if (!token) throw new Error('A valid API Token is required');
11
+
12
+ this.token = token;
13
+ this.baseUrl = options.baseUrl || 'https://discordthings.com/api';
14
+
15
+ this.client = axios.create({
16
+ baseURL: this.baseUrl,
17
+ headers: {
18
+ 'Content-Type': 'application/json',
19
+ 'Authorization': this.token
20
+ }
21
+ });
22
+ }
23
+
24
+ /**
25
+ * Post Server and Shard Count Stats
26
+ * @param {string} botId - The ID of the bot
27
+ * @param {number} serverCount - Total number of servers
28
+ * @param {number} [shardCount=0] - Total number of shards (optional)
29
+ * @returns {Promise<Object>} API Response
30
+ */
31
+ async postStats(botId, serverCount, shardCount = 0) {
32
+ if (!botId) throw new Error('Bot ID is required');
33
+
34
+ try {
35
+ const response = await this.client.post(`/bots/${botId}/stats`, {
36
+ server_count: serverCount,
37
+ shard_count: shardCount
38
+ });
39
+ return response.data;
40
+ } catch (error) {
41
+ this._handleError(error);
42
+ }
43
+ }
44
+
45
+ /**
46
+ * Check if a user has voted for a bot
47
+ * @param {string} botId - The ID of the bot
48
+ * @param {string} userId - The ID of the user to check
49
+ * @returns {Promise<Object>} { voted: boolean, lastVote: Date, nextVote: Date }
50
+ */
51
+ async checkVote(botId, userId) {
52
+ if (!botId || !userId) throw new Error('Bot ID and User ID are required');
53
+
54
+ try {
55
+ const response = await this.client.get(`/bots/${botId}/check-vote`, {
56
+ params: { userId }
57
+ });
58
+ return response.data;
59
+ } catch (error) {
60
+ this._handleError(error);
61
+ }
62
+ }
63
+
64
+ _handleError(error) {
65
+ if (error.response) {
66
+ throw new Error(`API Error [${error.response.status}]: ${error.response.data.error || error.response.statusText}`);
67
+ }
68
+ throw error;
69
+ }
70
+ }
71
+
72
+ module.exports = DiscordThings;
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "name": "discordthings-api",
3
+ "version": "1.0.0",
4
+ "description": "Official API Wrapper for DiscordThings.com",
5
+ "main": "index.js",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1"
8
+ },
9
+ "keywords": [
10
+ "discord",
11
+ "botlist",
12
+ "discordthings",
13
+ "api",
14
+ "wrapper",
15
+ "stats"
16
+ ],
17
+ "author": "DiscordThings",
18
+ "license": "ISC",
19
+ "dependencies": {
20
+ "axios": "^1.6.0"
21
+ },
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://github.com/discordthings/npm-package.git"
25
+ }
26
+ }