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

whatsapp-xp

v1.0.4

Published

A lightweight and easy to use economy framework for whatsapp bots, uses MongoDB. Fork from MrAugu/discord-xp.

Downloads

5

Readme

Whatsapp-XP - Forked from MrAugu/discord-xp.

A lightweight and easy to use xp framework for whatsapp bots, uses MongoDB.

Changelog

  • 20 February 2021 (v1.0.4) - Remove unnecessary code for Leaderboard.
  • 16 February 2021 (v1.0.3) - Bump Dependencies, rename package, etc...
  • 16 February 2021 (v1.0.0) - Remove irrelevant code & updated examples for Whatsapp Bots using @open-wa/wa-automate.

Bugs, Glitches and Issues

If you encounter any of those fell free to open an issue in our github repository.

Download

You can download it from npm:

npm i whatsapp-xp

Setting Up

First things first, we include the module into the project.

const Levels = require("whatsapp-xp");

After that, you need to provide a valid mongo database url, and set it. You can do so by:

Levels.setURL("mongodb://..."); // You only need to do this ONCE per process.

Examples - Edited for Whatsapp Bots

Examples assume that you have setted up the module as presented in 'Setting Up' section. Following examples assume that your client is called client.

Following example contains isolated code which you need to integrate in your own command handler.

Following example assumes that you are able to write asynchronous code (use await).

  • Allocating Random XP For Each Message Sent
client.on("message", async (message) => {
  if (!message.chat.id) return;
  
  const randomAmountOfXp = Math.floor(Math.random() * 29) + 1; // Min 1, Max 30
  const hasLeveledUp = await Levels.appendXp(message.sender.id, message.chat.id, randomAmountOfXp);
  if (hasLeveledUp) {
    const user = await Levels.fetch(message.sender.id, message.chat.id);
    client.sendTextWithMentions(message.from, `@${message.author}, congratulations! You have leveled up to *${user.level}*. `);
  }
});
  • Rank Command
const user = await Levels.fetch(message.sender.id, message.chat.id); // Selects the target from the database.

if (!user) return message.channel.send("Seems like this user has not earned any xp so far."); // If there isnt such user in the database, we send a message in general.

client.sendTextWithMentions(message.from, `> *@${message.author}* is currently level ${user.level}.`); // We show the level.
  • Leaderboard Command
const rawLeaderboard = await Levels.fetchLeaderboard(message.chat.id, 10); // We grab top 10 users with most xp in the current server.

if (rawLeaderboard.length < 1) return client.reply(message.from, "Nobody's in leaderboard yet.", message.id);

const leaderboard = await Levels.computeLeaderboard(client, rawLeaderboard); // We process the leaderboard.

const lb = leaderboard.map(e => `${e.position}. @${e.userID.replace('@c.us', '')}\nLevel: ${e.level}\nXP: ${e.xp.toLocaleString()}`); // We map the outputs.

client.sendTextWithMentions(message.from, `*Leaderboard*:\n\n${lb.join("\n\n")}`);

Methods

createUser

Creates an entry in database for that user if it doesnt exist.

Levels.createUser(<UserID - String>, <GuildID - String>);
  • Output:
Promise<Object>

deleteUser

If the entry exists, it deletes it from database.

Levels.deleteUser(<UserID - String>, <GuildID - String>);
  • Output:
Promise<Object>

appendXp

It adds a specified amount of xp to the current amount of xp for that user, in that guild. It re-calculates the level. It creates a new user with that amount of xp, if there is no entry for that user.

Levels.appendXp(<UserID - String>, <GuildID - String>, <Amount - Integer>);
  • Output:
Promise<Boolean>

appendLevel

It adds a specified amount of levels to current amount, re-calculates and sets the xp reqired to reach the new amount of levels.

Levels.appendLevel(<UserID - String>, <GuildID - String>, <Amount - Integer>);
  • Output:
Promise<Boolean/Object>

setXp

It sets the xp to a specified amount and re-calculates the level.

Levels.setXp(<UserID - String>, <GuildID - String>, <Amount - Integer>);
  • Output:
Promise<Boolean/Object>

setLevel

Calculates the xp required to reach a specified level and updates it.

Levels.setLevel(<UserID - String>, <GuildID - String>, <Amount - Integer>);
  • Output:
Promise<Boolean/Object>

fetch (Updated recently!)

Retrives selected entry from the database, if it exists.

Levels.fetch(<UserID - String>, <GuildID - String>, <FetchPosition - Boolean>);
  • Output:
Promise<Object>

subtractXp

It removes a specified amount of xp to the current amount of xp for that user, in that guild. It re-calculates the level.

Levels.appendXp(<UserID - String>, <GuildID - String>, <Amount - Integer>);
  • Output:
Promise<Boolean/Object>

subtractLevel

It removes a specified amount of levels to current amount, re-calculates and sets the xp reqired to reach the new amount of levels.

Levels.appendLevel(<UserID - String>, <GuildID - String>, <Amount - Number>);
  • Output:
Promise<Boolean/Object>

fetchLeaderboard

It gets a specified amount of entries from the database, ordered from higgest to lowest within the specified limit of entries.

Levels.fetchLeaderboard(<GuildID - String>, <Limit - Integer>);
  • Output:
Promise<Array [Objects]>

computeLeaderboard (Updated recently!)

It returns a new array of object that include level, xp, guild id, user id, leaderboard position, username and discriminator.

Levels.fetch(<Client - Discord.js Client>, <Leaderboard - fetchLeaderboard output>);
  • Output:
Promise<Array [Objects]>

xpFor

It returns a number that indicates amount of xp required to reach a level based on the input.

Levels.xpFor(<TargetLevel - Integer>);
  • Output:
Integer