Skip to content

Commit

Permalink
Ignore stuff for npm pkg, api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Dec 17, 2023
1 parent 187a51c commit 9b40f36
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 29 deletions.
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.github/
.eslintrc.json
index.test.js
33 changes: 28 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ You can find the public HTTP api docs on https://disstat.pages.dev/docs if you d
npm i disstat
```

# Usage
# Main usage
```js
const DisStat = require("disstat")

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

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

/*
* Posts data for a custom graph to DisStat.
* Note that using a not used type here creates the custom graph on DisStat if you have enough unused graph slots.
* Don't use names like "servers" or "users" here, as they are reserved for the main graphs, and would get overwritten.
*
* @param {string} type - The type of event to post
* @param {string} type - The name of the custom graph to post to
* @param {string|Number} value1? - First custom value (e.g. an event name like "interactionCreate")
* @param {string|Number} value2? - Second custom value (e.g. a user ID)
* @param {string|Number} value3? - Third custom value (e.g. a guild ID)
Expand All @@ -72,3 +71,27 @@ if (message.content.includes("<@" + bot.user.id + ">")) {
disstat.postCustom("ping")
}
```

# Events
The client emits events for different things which you can react to.

```js
const DisStat = require("disstat")
const disstat = new DisStat(...)

disstat.on("ready", () => {
console.log("DisStat is ready!")
})

disstat.on("autopost", () => {
console.log("Starting autoposting!")
// Emits on every autopost, not once. Use "ready" or .once() for that.
})
disstat.on("autopostError", (error, data) => {
console.log("Autoposting failed: " + error, data)
})
disstat.on("autopostSuccess", () => {
console.log("Finished autoposting!")
})

```
44 changes: 23 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ class DisStat extends EventEmitter {
this.botId = ""
this.bot = {}
this.autoposting = false

this.unposted = {
commands: []
}
this.unpostedCustom = []

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.")
if (!apiKeyInput.startsWith("DS-")) console.warn("[DisStat " + new Date().toLocaleTimeString() + "] The provided API key as first argument doesn't start with \"DS-\".")
Expand All @@ -33,12 +30,14 @@ class DisStat extends EventEmitter {
this.bot = botInput
setTimeout(this.autopost, 30000)
}

this.emit("ready")
}

async autopost() {
this.emit("autopost")

const data = this.unposted
const data = this.unpostedCustom
if (this.bot) {
data.guildCount = this.bot.guilds.cache.size
data.shardCount = this.bot.shard ? this.bot.shard.count : 0
Expand All @@ -61,15 +60,17 @@ class DisStat extends EventEmitter {
try {
result = await this.postData(data)
} catch (e) {
this.emit("autopostError", e, data)
console.warn("[DisStat " + new Date().toLocaleTimeString() + "] Failed to post data to DisStat API. Error: " + e.message, result)

setTimeout(this.autopost, autopostInterval / 2)
return
}

setTimeout(this.autopost, autopostInterval)
this.unposted = {
commands: []
}
this.unpostedCustom = []

this.emit("autopost-finish", data)
this.emit("autopostSuccess", data)
}

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

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

if (force || !this.autoposting) await fetch(baseURL + "bot/" + this.botId + "/command", {
if (this.autoposting) this.unpostedCustom.push({
type: "command",
value1: command,
value2: userId,
value3: guildId
})
else await fetch(baseURL + "bot/" + this.botId + "/custom", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: this.apiKey
},
body: JSON.stringify({
command,
user: userId,
guild: guildId
type: "command",
value1: command,
value2: userId,
value3: guildId
})
})
else this.unposted.commands.push({
command,
user: userId,
guild: guildId
})
}

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

if (!this.unposted[type]) this.unposted[type] = []
this.unposted[type].push({
this.unpostedCustom.push({
type,
value1,
value2,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "disstat",
"version": "1.0.0-pre1",
"version": "1.0.0-pre2",
"description": "Post data from a discord.js client automatically, or manually to DisStat for Discord bot statistic tracking.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 9b40f36

Please sign in to comment.