Skip to content

Commit 9b40f36

Browse files
committed
Ignore stuff for npm pkg, api changes
1 parent 187a51c commit 9b40f36

File tree

5 files changed

+57
-29
lines changed

5 files changed

+57
-29
lines changed

.npmignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.github/
2+
.eslintrc.json
3+
index.test.js

README.md

+28-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ You can find the public HTTP api docs on https://disstat.pages.dev/docs if you d
99
npm i disstat
1010
```
1111

12-
# Usage
12+
# Main usage
1313
```js
1414
const DisStat = require("disstat")
1515

@@ -44,24 +44,23 @@ const newBotData = await disstat.postData({ servers: 42, users: 100, shards: 1 }
4444
console.log(newBotData)
4545

4646
/*
47-
* Posts a command to DisStat.
47+
* Posts a command to DisStat using custom graphs.
4848
* You shouldn't post user generated commands like custom commands to protect user privacy.
4949
* You also should exclude the prefix and command arguments from the command.
5050
*
5151
* @param {string} command - The command to post
5252
* @param {string} userId? - The user's id
5353
* @param {string} guildId? - The guild's id
54-
* @param {Boolean} force? - Whether to force the command to be posted instantly IF autoposting is enabled,
55-
* defaults to false, causing a delay from up to 90 seconds.
5654
*/
5755
disstat.postCommand("info")
5856
disstat.postCommand("help", "581146486646243339", "1081089799324180490")
5957

6058
/*
6159
* Posts data for a custom graph to DisStat.
6260
* Note that using a not used type here creates the custom graph on DisStat if you have enough unused graph slots.
61+
* Don't use names like "servers" or "users" here, as they are reserved for the main graphs, and would get overwritten.
6362
*
64-
* @param {string} type - The type of event to post
63+
* @param {string} type - The name of the custom graph to post to
6564
* @param {string|Number} value1? - First custom value (e.g. an event name like "interactionCreate")
6665
* @param {string|Number} value2? - Second custom value (e.g. a user ID)
6766
* @param {string|Number} value3? - Third custom value (e.g. a guild ID)
@@ -72,3 +71,27 @@ if (message.content.includes("<@" + bot.user.id + ">")) {
7271
disstat.postCustom("ping")
7372
}
7473
```
74+
75+
# Events
76+
The client emits events for different things which you can react to.
77+
78+
```js
79+
const DisStat = require("disstat")
80+
const disstat = new DisStat(...)
81+
82+
disstat.on("ready", () => {
83+
console.log("DisStat is ready!")
84+
})
85+
86+
disstat.on("autopost", () => {
87+
console.log("Starting autoposting!")
88+
// Emits on every autopost, not once. Use "ready" or .once() for that.
89+
})
90+
disstat.on("autopostError", (error, data) => {
91+
console.log("Autoposting failed: " + error, data)
92+
})
93+
disstat.on("autopostSuccess", () => {
94+
console.log("Finished autoposting!")
95+
})
96+
97+
```

index.js

+23-21
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ class DisStat extends EventEmitter {
1212
this.botId = ""
1313
this.bot = {}
1414
this.autoposting = false
15-
16-
this.unposted = {
17-
commands: []
18-
}
15+
this.unpostedCustom = []
1916

2017
if (!apiKeyInput) throw new TypeError("No DisStat API key provided as first argument. You can find the API key on the Manage Bot page of your bot.")
2118
if (!apiKeyInput.startsWith("DS-")) console.warn("[DisStat " + new Date().toLocaleTimeString() + "] The provided API key as first argument doesn't start with \"DS-\".")
@@ -33,12 +30,14 @@ class DisStat extends EventEmitter {
3330
this.bot = botInput
3431
setTimeout(this.autopost, 30000)
3532
}
33+
34+
this.emit("ready")
3635
}
3736

3837
async autopost() {
3938
this.emit("autopost")
4039

41-
const data = this.unposted
40+
const data = this.unpostedCustom
4241
if (this.bot) {
4342
data.guildCount = this.bot.guilds.cache.size
4443
data.shardCount = this.bot.shard ? this.bot.shard.count : 0
@@ -61,15 +60,17 @@ class DisStat extends EventEmitter {
6160
try {
6261
result = await this.postData(data)
6362
} catch (e) {
63+
this.emit("autopostError", e, data)
6464
console.warn("[DisStat " + new Date().toLocaleTimeString() + "] Failed to post data to DisStat API. Error: " + e.message, result)
65+
66+
setTimeout(this.autopost, autopostInterval / 2)
67+
return
6568
}
6669

6770
setTimeout(this.autopost, autopostInterval)
68-
this.unposted = {
69-
commands: []
70-
}
71+
this.unpostedCustom = []
7172

72-
this.emit("autopost-finish", data)
73+
this.emit("autopostSuccess", data)
7374
}
7475

7576
async getBot(botIdInput = "") {
@@ -97,33 +98,34 @@ class DisStat extends EventEmitter {
9798
if (!response.ok) return await response.json()
9899
}
99100

100-
async postCommand(command = "", userId = "", guildId = "", force = false) {
101+
async postCommand(command = "", userId = void 0, guildId = void 0) {
101102
if (!command || command.trim() == "") return new TypeError("No command name provided to postCommand().")
102103

103-
if (force || !this.autoposting) await fetch(baseURL + "bot/" + this.botId + "/command", {
104+
if (this.autoposting) this.unpostedCustom.push({
105+
type: "command",
106+
value1: command,
107+
value2: userId,
108+
value3: guildId
109+
})
110+
else await fetch(baseURL + "bot/" + this.botId + "/custom", {
104111
method: "POST",
105112
headers: {
106113
"Content-Type": "application/json",
107114
Authorization: this.apiKey
108115
},
109116
body: JSON.stringify({
110-
command,
111-
user: userId,
112-
guild: guildId
117+
type: "command",
118+
value1: command,
119+
value2: userId,
120+
value3: guildId
113121
})
114122
})
115-
else this.unposted.commands.push({
116-
command,
117-
user: userId,
118-
guild: guildId
119-
})
120123
}
121124

122125
async postCustom(type = "", value1 = void 0, value2 = void 0, value3 = void 0) {
123126
if (!type || type.trim() == "") return new TypeError("No custom graph type provided to postCustom().")
124127

125-
if (!this.unposted[type]) this.unposted[type] = []
126-
this.unposted[type].push({
128+
this.unpostedCustom.push({
127129
type,
128130
value1,
129131
value2,

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "disstat",
3-
"version": "1.0.0-pre1",
3+
"version": "1.0.0-pre2",
44
"description": "Post data from a discord.js client automatically, or manually to DisStat for Discord bot statistic tracking.",
55
"main": "index.js",
66
"scripts": {

0 commit comments

Comments
 (0)