Skip to content

Commit

Permalink
Simplify command & fix custom posting
Browse files Browse the repository at this point in the history
  • Loading branch information
DEVTomatoCake committed Dec 17, 2023
1 parent 9b40f36 commit c10f526
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
29 changes: 18 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const DisStat = require("disstat")
* @param {string} apiKey - Your api key, found in your dashboard on https://disstat.pages.dev/me
* @param {string|Discord.Client} bot - Your bot's user id OR a discord.js based bot client.
*
* If a client is provided, the package will automatically post server and user count to DisStat ("autoposting").
* Note that the client has to be ready, so if you're using a client there, put this into it's ready event.
* If a client is provided, the package will automatically post server
* and user count to DisStat ("autoposting").
*/
const disstat = new DisStat("DS-apikey123", "685166801394335819")
const disstat = new DisStat("DS-apikey123", client)
Expand All @@ -45,7 +45,7 @@ console.log(newBotData)

/*
* Posts a command to DisStat using custom graphs.
* You shouldn't post user generated commands like custom commands to protect user privacy.
* Don'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
Expand All @@ -57,8 +57,10 @@ 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.
* Note that using a new 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 name of the custom graph to post to
* @param {string|Number} value1? - First custom value (e.g. an event name like "interactionCreate")
Expand All @@ -72,8 +74,7 @@ if (message.content.includes("<@" + bot.user.id + ">")) {
}
```

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

```js
const DisStat = require("disstat")
Expand All @@ -83,15 +84,21 @@ disstat.on("ready", () => {
console.log("DisStat is ready!")
})

disstat.on("autopost", () => {
console.log("Starting autoposting!")
disstat.on("autopostStart", () => {
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!")
disstat.on("autopostSuccess", data => {
console.log("Finished posting this data:", data)
})

disstat.on("post", error => {
if (error) console.log("An error occurred while posting:", error)
else console.log("Posted data successfully!")
// This event also gets emitted on autoposting.
})

```
50 changes: 27 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class DisStat extends EventEmitter {
this.botId = ""
this.bot = {}
this.autoposting = false
this.noManualWarningSent = false
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.")
Expand All @@ -35,7 +36,7 @@ class DisStat extends EventEmitter {
}

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

const data = this.unpostedCustom
if (this.bot) {
Expand All @@ -56,6 +57,7 @@ class DisStat extends EventEmitter {

// TODO: Bandwidth usage

Check warning on line 58 in index.js

View workflow job for this annotation

GitHub Actions / ESLint

Unexpected 'todo' comment: 'TODO: Bandwidth usage'

this.noManualWarningSent = true
let result = {}
try {
result = await this.postData(data)
Expand Down Expand Up @@ -84,6 +86,12 @@ class DisStat extends EventEmitter {
}

async postData(data = {}) {
if (this.autoposting && !this.noManualWarningSent) {
console.warn("[DisStat " + new Date().toLocaleTimeString() +
"] You are using autoposting, but you are still manually posting data. This is not recommended, as it can cause duplication and data loss due to overwriting.")
this.noManualWarningSent = true
}

if (!data || typeof data != "object" || Object.keys(data).length == 0) throw new TypeError("No data object provided to postData().")

const response = await fetch(baseURL + "bot/" + this.botId, {
Expand All @@ -94,42 +102,38 @@ class DisStat extends EventEmitter {
Authorization: this.apiKey
},
body: JSON.stringify(data)
}).catch(e => {
this.emit("post", false)
return e
})
if (!response.ok) return await response.json()
const json = await response.json()

this.emit("post", response.ok, json)
if (!response.ok) return json
}

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

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({
type: "command",
value1: command,
value2: userId,
value3: guildId
})
})
this.postCustom("command", command, userId, 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().")

this.unpostedCustom.push({
const body = {
type,
value1,
value2,
value3
}
if (this.autoposting) this.unpostedCustom.push(body)
else await fetch(baseURL + "bot/" + this.botId + "/custom", {
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: this.apiKey
},
body: JSON.stringify(body)
})
}
}
Expand Down

0 comments on commit c10f526

Please sign in to comment.