From c10f526f340c63368bec5b64a769c181d0960d10 Mon Sep 17 00:00:00 2001 From: TomatoCake <60300461+DEVTomatoCake@users.noreply.github.com> Date: Sun, 17 Dec 2023 06:42:33 +0100 Subject: [PATCH] Simplify command & fix custom posting --- README.md | 29 ++++++++++++++++++----------- index.js | 50 +++++++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index 38509b6..23e3ddd 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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") @@ -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") @@ -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. }) ``` diff --git a/index.js b/index.js index c29edd5..98f2f84 100644 --- a/index.js +++ b/index.js @@ -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.") @@ -35,7 +36,7 @@ class DisStat extends EventEmitter { } async autopost() { - this.emit("autopost") + this.emit("autopostStart") const data = this.unpostedCustom if (this.bot) { @@ -56,6 +57,7 @@ class DisStat extends EventEmitter { // TODO: Bandwidth usage + this.noManualWarningSent = true let result = {} try { result = await this.postData(data) @@ -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, { @@ -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) }) } }