djs-cooldown

1.0.4 • Public • Published

Discord server npm version npm downloads repo size

About

djs-cooldown is a Node.js package to add cooldown for everything you want using MongoDB, but the main purpose of this package is for Discord BOT commands made by discord.js

Installation

Node.js 16.11.0 or newer is required.

# These are common JS runtime environment that you may use
# Just choose one that suitable for you

npm install djs-cooldown
yarn add djs-cooldown
pnpm add djs-cooldown
bun add djs-cooldown

Quick Setup

const { DJS_Cooldown } = require("djs-cooldown");

const djs_cooldown = new DJS_Cooldown({
  connection: "mongodb+srv://...", // your MongoDB Connection
  message: "Connected to MongoDB Successfully",
});

connection: String - The MongoDB connection string,
message: String - The success message when connected to MongoDB.
disconnect: String - The success message when disconnected from MongoDB.

Example usage

Set and check cooldown when user uses a command

client.on("message", async (message) => {
  // example Command Handler
  let cmd = client.getCommand(message.content);
  if (!cmd) return;

  // Cooldown System
  let isEnded = await djs_cooldown.checkCooldown({
    identity: message.author.id,
    name: `text/${cmd.name}`,
  });
  if (!isEnded) return message.channel.send(`You are on cooldown!`);

  // cooldown not found or ended
  cmd.run(...);

  // set new cooldown
  await djs_cooldown.set({
    identity: message.author.id,
    name: `text/${cmd.name}`,
    cooldown: 15 * 1000, // 15 seconds in ms
    usedAt: Date.now()
  }, function(error, message) {
    if(error) console.error(error);
  });
});

Features

Database Utilities

await djs_cooldown.setDB("mongodb+srv://..."); // new connetion URL

// You need to re-connect to MongoDB to change URL
await djs_cooldown.disconnect();
await djs_cooldown.connect();

// or simple way
await djs_cooldown.reconnect();

// shorter
await djs_cooldown.setDB("mongodb+srv://...", true); // "true" here means turn on automatically reconnect when reset connection URL

// Refresh Database connection state (avoid package required connect another time to use)
await djs_cooldown.refreshDB(); // Will log again if already connected

Cooldown Ultilities

// Set new cooldown for user
await djs_cooldown.set({
  identity: "1234812",
  name: "somethingherer??",
  cooldown: 15 * 1000,
  usedAt: Date.now(),
});

// Remove a created cooldown if you think something went wrong
await djs_cooldown.remove({
  identity: "1234812",
  name: "somethingherer??",
});

Check is cooldown ended

await djs_cooldown.checkCooldown({
  identity: "1234812",
  name: "somethingherer??",
});

// > true
// Is the cooldown ended? (true/false)

Count "Time Left"

await djs_cooldown.timeLeft({
  identity: "11111111111",
  name: "somethinghereig?",
});

// > 123412
// Time left in milliseconds
  • Output: Time in MS
  • If no data was found or timeLeft < 0, output will be 0

Default options

data - Object: Provide data to do job

  • identity - String: User ID / Guild ID / etc...
  • name - String: Cooldown for "name"
  • cooldown - Number: Cooldown time (in ms)
  • usedAt - Number | String: Timestamp when the cooldown was used

callback - Function | null (optional): Callback when code complete jobs. Return 2/3 variables:

  • error - Boolean: Check if error occurs
  • message - String: Return message
  • data - Object | null (depends on purpose): Return collected data (used for some functions)

Some functions do not required all fields, you can provide only what needed.

Support

If you have any problems or want to report some bugs, let us know

Package Sidebar

Install

npm i djs-cooldown

Weekly Downloads

4

Version

1.0.4

License

ISC

Unpacked Size

11.2 kB

Total Files

3

Last publish

Collaborators

  • kaizoffical