npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

djs-cooldown

v1.0.4

Published

Discord.JS BOT | Add cooldown for your BOT's commands

Downloads

14

Readme

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