MrAugu / discord-xp

A lightweight and easy to use economy framework for discord bots, uses MongoDB.
MIT License
118 stars 31 forks source link

Discord server

Discord-Xp

Download & Update

You can download it from npm:

npm i discord-xp

You can update to a newer version to receive updates using npm.

npm update discord-xp

Changelog

WARNING: This semi-major version contains breaking changes in the way leaderboard computing function works.

console.log(xpRequired); // Output: 90000


# Setting Up
First things first, we include the module into the project.
```js
const Levels = require("discord-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

Examples can be found in /test

Methods

createRoleReward

Creates a role reward entry in database for the guild if it doesnt exist.

Levels.createRoleReward(<GuildID - String>, <level - Number>, <roleId - String>);

Deletes a role reward entry in database for the guild.

Levels.deleteRoleReward(<GuildID - String>, <level - Number>);

Fetches a role reward entry in database for the guild.

Levels.fetchRoleReward(<GuildID - String>, <level - Number>);

Cleans the database from unknown users for a guild.

Levels.CleanDatabase(<Client - Discord.js Client>, <GuildID - String>);

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

Levels.createUser(<UserID - String>, <GuildID - String>);

If the entry exists, it deletes it from database.

Levels.deleteUser(<UserID - String>, <GuildID - String>);

If the entry exists, it deletes it from database.

Levels.deleteGuild(<GuildID - String>);

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>);

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>);

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

Levels.setXp(<UserID - String>, <GuildID - String>, <Amount - Integer>);

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

Levels.setLevel(<UserID - String>, <GuildID - String>, <Amount - Integer>);

Retrives selected entry from the database, if it exists.

Levels.fetch(<UserID - String>, <GuildID - String>, <FetchPosition - Boolean>);

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.subtractXp(<UserID - String>, <GuildID - String>, <Amount - Integer>);

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.subtractLevel(<UserID - String>, <GuildID - String>, <Amount - Number>);

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>);

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

Levels.computeLeaderboard(<Client - Discord.js Client>, <Leaderboard - fetchLeaderboard output>, <fetchUsers - boolean, disabled by default>);

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

Levels.xpFor(<TargetLevel - Integer>);