JSPM

  • Created
  • Published
  • Downloads 7
  • Score
    100M100P100Q58886F
  • License MIT

A Node.js package to have statistics graphs for your Discord Bot

Package Exports

  • discord-bot-stats
  • discord-bot-stats/dist/StatsManager.d.ts
  • discord-bot-stats/dist/StatsManager.js
  • discord-bot-stats/dist/StatsManager.js.map
  • discord-bot-stats/dist/StatsModule.d.ts
  • discord-bot-stats/dist/StatsModule.js
  • discord-bot-stats/dist/StatsModule.js.map
  • discord-bot-stats/dist/graphs/BarGraph.d.ts
  • discord-bot-stats/dist/graphs/BarGraph.js
  • discord-bot-stats/dist/graphs/BarGraph.js.map
  • discord-bot-stats/dist/graphs/BaseGraph.d.ts
  • discord-bot-stats/dist/graphs/BaseGraph.js
  • discord-bot-stats/dist/graphs/BaseGraph.js.map
  • discord-bot-stats/dist/graphs/DoughnutGraph.d.ts
  • discord-bot-stats/dist/graphs/DoughnutGraph.js
  • discord-bot-stats/dist/graphs/DoughnutGraph.js.map
  • discord-bot-stats/dist/graphs/LineGraph.d.ts
  • discord-bot-stats/dist/graphs/LineGraph.js
  • discord-bot-stats/dist/graphs/LineGraph.js.map
  • discord-bot-stats/dist/index.d.ts
  • discord-bot-stats/dist/index.js
  • discord-bot-stats/dist/index.js.map
  • discord-bot-stats/dist/types/types.d.ts
  • discord-bot-stats/dist/types/types.js
  • discord-bot-stats/dist/types/types.js.map
  • discord-bot-stats/src/StatsManager.d.ts
  • discord-bot-stats/src/StatsManager.js
  • discord-bot-stats/src/StatsManager.js.map
  • discord-bot-stats/src/StatsModule.d.ts
  • discord-bot-stats/src/StatsModule.js
  • discord-bot-stats/src/StatsModule.js.map
  • discord-bot-stats/src/graphs/BarGraph.d.ts
  • discord-bot-stats/src/graphs/BarGraph.js
  • discord-bot-stats/src/graphs/BarGraph.js.map
  • discord-bot-stats/src/graphs/BaseGraph.d.ts
  • discord-bot-stats/src/graphs/BaseGraph.js
  • discord-bot-stats/src/graphs/BaseGraph.js.map
  • discord-bot-stats/src/graphs/DoughnutGraph.d.ts
  • discord-bot-stats/src/graphs/DoughnutGraph.js
  • discord-bot-stats/src/graphs/DoughnutGraph.js.map
  • discord-bot-stats/src/graphs/LineGraph.d.ts
  • discord-bot-stats/src/graphs/LineGraph.js
  • discord-bot-stats/src/graphs/LineGraph.js.map
  • discord-bot-stats/src/index.d.ts
  • discord-bot-stats/src/index.js
  • discord-bot-stats/src/index.js.map
  • discord-bot-stats/src/types/types.d.ts
  • discord-bot-stats/src/types/types.js
  • discord-bot-stats/src/types/types.js.map

Readme

Discord-bot-stats

Discord-bot-stats is a cool Node.js package to have statistics graphs for your Discord Bot.

Installation

npm install discord-bot-stats
OR
yarn add discord-bot-stats

Example Usage With MongoDB in typescript

npm i mongoose
OR
yarn add mongoose

Creates a first file named MongoStatsManager.ts

import { StatsManager } from "discord-bot-stats";
import mongoose from "mongoose";
import { SavedStatsFormat, StatsManagerOptions } from "discord-bot-stats/types/types";

const StatsSchema = new mongoose.Schema({
    timestamp: Number,
    stats: {
        cpu: Number,
        ram: Number,
        servers: Number,
        users: Number,
        commands: Map,
        errors: Number
    }
});

const StatsModel = mongoose.model("BotStats", StatsSchema);

export default class extends StatsManager {
    constructor(options: StatsManagerOptions) {
        super(options);
        this._connect();
    }

    async _connect() {
        await mongoose.connect("mongodb://localhost:27017/botstatsmodule");
    }

    async deleteStats(timestamps: number[]): Promise<void> {
        StatsModel.deleteMany({ timestamp: { $in: timestamps } }).exec();
        return;
    }

    async getStats(): Promise<SavedStatsFormat[]> {
        return StatsModel.find().lean().exec() as unknown as Promise<SavedStatsFormat[]>;
    }

    async saveStats(stats: SavedStatsFormat): Promise<void> {
        StatsModel.create(stats);
        return;
    }
}

Crate a second file named bot.ts

import { Client, GatewayIntentBits } from "discord.js";
import { LineGraph } from "discord-bot-stats";
import MongoStatsManager from "./MongoStatsManager";

// Create a new client instance
const client = new Client({
    intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages, GatewayIntentBits.MessageContent]
});

const statsManager = new MongoStatsManager({
    enabledStats: {
        cpu: true,
        ram: true,
        servers: true,
        commands: true
    }
});

// When the client is ready, run this code (only once)
client.once("ready", () => {
    console.log("Ready!");
    statsManager.start(async () => {
        const cmds = new Map()
            .set("ping", Math.floor(Math.random() * 12))
            .set("stats", Math.floor(Math.random() * 12))
            .set("lmao", Math.floor(Math.random() * 12))
            .set("trala", Math.floor(Math.random() * 12))
            .set("xxx", Math.floor(Math.random() * 12))
            .set("pitttng", Math.floor(Math.random() * 12));
        return {
            timestamp: Date.now(),
            stats: {
                cpu: 0.1,
                ram: Math.round((process.memoryUsage().heapUsed / 1024 / 1024) * 100) / 100,
                servers: client.guilds.cache.size,
                users: client.users.cache.size,
                commands: cmds
            }
        };
    });
});

client.on("messageCreate", async message => {
    if (message.author.bot) return;
    console.log(message.content);
    if (message.content.startsWith("+stats")) {
        const graph = await new LineGraph(statsManager).generate("ram");
        await message.channel.send({
            files: [
                {
                    attachment: graph
                }
            ]
        });
    }
});

// Login to Discord with your client's token
client.login(token);