From 1bd71caa386eba6efb6ad6cfda75ca2fbcd6c76a Mon Sep 17 00:00:00 2001 From: TomatoCake <60300461+DEVTomatoCake@users.noreply.github.com> Date: Sun, 17 Dec 2023 22:00:01 +0100 Subject: [PATCH] Fix obj key names to work with new api (pre6) --- README.md | 19 +++++++------------ index.js | 18 ++++++++---------- package-lock.json | 4 ++-- package.json | 2 +- 4 files changed, 18 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 23e3ddd..ad3189f 100644 --- a/README.md +++ b/README.md @@ -80,25 +80,20 @@ if (message.content.includes("<@" + bot.user.id + ">")) { const DisStat = require("disstat") const disstat = new DisStat(...) -disstat.on("ready", () => { - console.log("DisStat is ready!") +disstat.on("post", (error, data) => { + if (error) console.log("An error occurred while posting:", error, data) + else console.log("Posted data successfully:", data) + // This event also gets emitted on autoposting. }) disstat.on("autopostStart", () => { - console.log("Starting autoposting...") - // Emits on every autopost, not once. Use "ready" or .once() for that. + console.log("Started autopost...") }) disstat.on("autopostError", (error, data) => { - console.log("Autoposting failed: " + error, data) + console.log("Autopost failed: " + error, data) }) 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. + console.log("Successfully posting data:", data) }) ``` diff --git a/index.js b/index.js index 5f7a8d5..f108148 100644 --- a/index.js +++ b/index.js @@ -19,7 +19,7 @@ class DisStat extends EventEmitter { if (!apiKeyInput.startsWith("DS-")) console.warn("[DisStat " + new Date().toLocaleTimeString() + "] The provided API key as first argument doesn't start with \"DS-\".") this.botId = typeof botInput == "object" ? botInput.user.id : botInput - if (!this.botId) throw new TypeError("Missing (falsy) Discord bot ID provided, but a bot ID is required as second argument") + if (!this.botId) throw new TypeError("Missing (falsy) Discord bot ID provided, but a discord.js bot client or ID is required as second argument") this.apiKey = apiKeyInput if (typeof botInput == "object") { @@ -31,21 +31,18 @@ class DisStat extends EventEmitter { this.bot = botInput setTimeout(() => this.autopost(), 30000) } - - this.emit("ready") } async autopost() { this.emit("autopostStart") - const data = { - custom: this.unpostedCustom - } + const data = {} + if (this.unpostedCustom.length > 0) data.custom = this.unpostedCustom if (this.bot) { - data.guildCount = this.bot.guilds.cache.size - data.shardCount = this.bot.shard ? this.bot.shard.count : 0 - data.userCount = this.bot.guilds.cache.filter(guild => guild.available).reduce((acc, cur) => acc + cur.memberCount, 0) - data.apiPing = this.bot.ws.ping + data.guilds = this.bot.guilds.cache.size + data.shards = this.bot.shard ? this.bot.shard.count : 0 + data.users = this.bot.guilds.cache.filter(guild => guild.available).reduce((acc, cur) => acc + cur.memberCount, 0) + data.apiPing = this.bot.ws.ping > 0 ? this.bot.ws.ping : void 0 } data.ramUsage = process.memoryUsage.rss() data.ramTotal = process.memoryUsage().heapTotal @@ -136,6 +133,7 @@ class DisStat extends EventEmitter { method: "POST", headers: { "Content-Type": "application/json", + Accept: "application/json", Authorization: this.apiKey }, body: JSON.stringify(body) diff --git a/package-lock.json b/package-lock.json index 87e4fcb..426da79 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "disstat", - "version": "1.0.0-pre5", + "version": "1.0.0-pre6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "disstat", - "version": "1.0.0-pre5", + "version": "1.0.0-pre6", "license": "MIT", "devDependencies": { "eslint": "^8.56.0", diff --git a/package.json b/package.json index 8390a4f..4f4c3a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "disstat", - "version": "1.0.0-pre5", + "version": "1.0.0-pre6", "description": "Post data from a discord.js client automatically, or manually to DisStat for Discord bot statistic tracking.", "main": "index.js", "scripts": {