Skip to content

The official npm package for the DisStat project

License

Notifications You must be signed in to change notification settings

Statcord/DisStat-npm

Repository files navigation

DisStat-npm

The official npm package for DisStat - DisStat itself is open source btw!

You can find the public HTTP api docs on https://disstat.pages.dev/docs if you dont want to use an api wrapper.

Installation

npm i disstat

Main usage

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").
 */
const disstat = new DisStat("DS-apikey123", "685166801394335819")
const disstat = new DisStat("DS-apikey123", client)

/*
 * Gets data from your bot or someone else public bot.
 * @param {string} botId? - The bot's id
 * @returns {Promise<Object>} - The bot's data
 */
const botData = await disstat.getBot()
console.log(botData)

/*
 * Posts your bots data to DisStat.
 * Warning: You shouldn't use this when autoposting.
 *
 * @param {Object} data - The data to post
 * @see https://disstat.pages.dev/docs
 * @returns {Promise<Object>} - Only on error: The error object received from the API
 */
disstat.postData({ guilds: 42, users: 100, shards: 1 })

/*
 * Posts a command to DisStat using custom graphs.
 * 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
 * @param {string} userId? - The user's id
 * @param {string} guildId? - The guild's id
 */
disstat.postCommand("info")
disstat.postCommand("help", "581146486646243339", "1081089799324180490")

/*
 * Posts data for a custom graph to DisStat.
 * 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.
 *
 * "type" must not be longer than 50 characters.
 * "value1", "value2" and "value3" must not be longer than 100 characters.
 *
 * @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)
 */
disstat.postCustom("events", "interactionCreate")

if (message.content.includes("<@" + client.user.id + ">")) {
	message.reply("My prefix is `!` 🤖")
	disstat.postCustom("ping")
}

Listening to events

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

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

disstat.on("autopostStart", () => {
	console.log("Started autopost...")
})
disstat.on("autopostError", (error, data) => {
	console.log("Autopost failed: " + error, data)
})
disstat.on("autopostSuccess", data => {
	console.log("Successfully posted data:", data)
})