Skip to content

Commit

Permalink
v1.3.5 (BETA-7)
Browse files Browse the repository at this point in the history
UPDATES:
- Added eslint (dev)
- Update canvas
- Update mongoose
- rank() => Moved Funcs (eslint compliant)
  • Loading branch information
Abadima committed Jun 24, 2023
1 parent 85a9936 commit c567aa5
Show file tree
Hide file tree
Showing 18 changed files with 662 additions and 628 deletions.
29 changes: 29 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"env": {
"commonjs": true,
"es2021": true,
"node": true
},
"extends": "eslint:recommended",
"parserOptions": {
"ecmaVersion": "latest"
},
"rules": {
"indent": [
"error",
4
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
}
11 changes: 7 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "simply-xp",
"version": "1.3.5-beta-6",
"version": "1.3.5-beta-7",
"description": "A Simple, Easy and Beginner Friendly XP System",
"main": "simplyxp.js",
"typings": "index.d.ts",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"beta": "npm publish --tag beta"
"beta": "eslint . --fix && npm update && npm publish --tag beta"
},
"author": "Rahuletto",
"keywords": [
Expand Down Expand Up @@ -40,11 +40,14 @@
"url": "https://github.com/Rahuletto/simply-xp"
},
"dependencies": {
"@napi-rs/canvas": "^0.1.33",
"@napi-rs/canvas": "^0.1.41",
"chart.js": "^3.9.1",
"mongoose": "^6.8.3"
"mongoose": "^6.11.2"
},
"peerDependencies": {
"discord.js": ">=13.12.0"
},
"devDependencies": {
"eslint": "^8.43.0"
}
}
30 changes: 15 additions & 15 deletions simplyxp.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
try {
require('discord.js')
require('discord.js');
} catch (e) {
console.warn('[XP] Discord.js is recommended for this package.')
console.warn('[XP] Discord.js is recommended for this package.');
}

module.exports.roleSetup = require('./src/roleSetup')
module.exports.roleSetup = require('./src/roleSetup');

module.exports.addLevel = require('./src/addLevel')
module.exports.addLevel = require('./src/addLevel');

module.exports.addXP = require('./src/addXP')
module.exports.addXP = require('./src/addXP');

module.exports.charts = require('./src/charts')
module.exports.charts = require('./src/charts');

module.exports.connect = require('./src/connect')
module.exports.connect = require('./src/connect');

module.exports.create = require('./src/create')
module.exports.create = require('./src/create');

module.exports.fetch = require('./src/fetch')
module.exports.fetch = require('./src/fetch');

module.exports.leaderboard = require('./src/leaderboard')
module.exports.leaderboard = require('./src/leaderboard');

module.exports.lvlRole = require('./src/lvlRole')
module.exports.lvlRole = require('./src/lvlRole');

module.exports.rank = require('./src/rank')
module.exports.rank = require('./src/rank');

module.exports.setLevel = require('./src/setLevel')
module.exports.setLevel = require('./src/setLevel');

module.exports.setXP = require('./src/setXP')
module.exports.setXP = require('./src/setXP');

module.exports.reset = require('./src/reset')
module.exports.reset = require('./src/reset');
40 changes: 20 additions & 20 deletions src/addLevel.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const levels = require('../src/models/level.js')
let {roleSetup} = require('../simplyxp')
const levels = require('../src/models/level.js');
let {roleSetup} = require('../simplyxp');

/**
* @param {Discord.Message} message
Expand All @@ -8,59 +8,59 @@ let {roleSetup} = require('../simplyxp')
* @param {number} level
*/
async function addLevel(message, userID, guildID, level) {
if (!userID) throw new Error('[XP] User ID was not provided.')
if (!userID) throw new Error('[XP] User ID was not provided.');

if (!guildID) throw new Error('[XP] Guild ID was not provided.')
if (!guildID) throw new Error('[XP] Guild ID was not provided.');

if (!level) throw new Error('[XP] Level amount is not provided.')
if (!level) throw new Error('[XP] Level amount is not provided.');

let {client} = message
let {client} = message;

const user = await levels.findOne({user: userID, guild: guildID})
const user = await levels.findOne({user: userID, guild: guildID});

if (!user) {
const newUser = new levels({
user: userID,
guild: guildID,
xp: 0,
level: 0
})
});

await newUser.save().catch(() => console.log(`[XP] Failed to save new user to database`))
await newUser.save().catch(() => console.log('[XP] Failed to save new user to database'));

let xp = (level * 10) ** 2
let xp = (level * 10) ** 2;

return {
level: level,
exp: xp
}
};
}
let level1 = user.level
let level1 = user.level;

user.level += parseFloat(level)
user.xp = (user.level * 10) ** 2
user.level += parseFloat(level);
user.xp = (user.level * 10) ** 2;

await user.save().catch((e) =>
console.log(`[XP] Failed to add Level | User: ${userID} | Err: ${e}`)
)
);

if (level1 !== level) {
let data = {
xp: user.xp,
level: user.level,
userID,
guildID
}
};

let role = await roleSetup.find(client, guildID, level)
let role = await roleSetup.find(client, guildID, level);

client.emit('levelUp', message, data, role)
client.emit('levelUp', message, data, role);
}

return {
level: user.level,
xp: user.xp
}
};
}

module.exports = addLevel
module.exports = addLevel;
65 changes: 33 additions & 32 deletions src/addXP.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const levels = require('../src/models/level.js')
let {roleSetup} = require('../simplyxp')
const levels = require('../src/models/level.js');
let {roleSetup} = require('../simplyxp');

/**
* @param {Discord.Message} message
Expand All @@ -9,76 +9,77 @@ let {roleSetup} = require('../simplyxp')
*/

async function addXP(message, userID, guildID, xp) {
if (!userID) throw new Error('[XP] User ID was not provided.')
if (!userID) throw new Error('[XP] User ID was not provided.');

if (!guildID) throw new Error('[XP] Guild ID was not provided.')
if (!guildID) throw new Error('[XP] Guild ID was not provided.');

if (!xp) throw new Error('[XP] XP amount is not provided.')
if (!xp) throw new Error('[XP] XP amount is not provided.');

let {client} = message
let {client} = message;

let min
let max
let min;
let max;
if (xp.min) {
if (!xp.max)
throw new Error(
'[XP] XP min amount is provided but max amount is not provided.'
)
);

min = Number(xp.min)
min = Number(xp.min);

if (Number(xp.min).toString() === 'NaN')
throw new Error('[XP] XP amount (min) is not a number.')
throw new Error('[XP] XP amount (min) is not a number.');
}

if (xp.max) {
if (!xp.min)
throw new Error(
'[XP] XP max amount is provided but min amount is not provided.'
)
);

max = Number(xp.max)
max = Number(xp.max);

if (Number(xp.max).toString() === 'NaN')
throw new Error('[XP] XP amount (max) is not a number.')
throw new Error('[XP] XP amount (max) is not a number.');
}

if (xp.min && xp.max) {
xp = Math.floor(Math.random() * (max - min) + min)
xp = Math.floor(Math.random() * (max - min) + min);
}

const user = await levels.findOne({user: userID, guild: guildID})
const user = await levels.findOne({user: userID, guild: guildID});

let lvl = Math.floor(0.1 * Math.sqrt(xp))
let lvl = Math.floor(0.1 * Math.sqrt(xp));

// TODO: FIX THIS xp = xp & level = lvl
if (!user) {
const newUser = new levels({
user: userID,
guild: guildID,
xp: xp,
level: lvl
})
});

await newUser.save().catch(() => console.log(`[XP] Failed to save new user to database`))
await newUser.save().catch(() => console.log('[XP] Failed to save new user to database'));

return {
level: 0,
exp: 0
}
};
}
let level1 = user.level
let level1 = user.level;

user.xp += parseInt(xp, 10)
user.level = Math.floor(0.1 * Math.sqrt(user.xp))
user.xp += parseInt(xp, 10);
user.level = Math.floor(0.1 * Math.sqrt(user.xp));

await user.save().catch((e) => console.log(`[XP] Failed to add XP | User: ${userID} | Err: ${e}`))
await user.save().catch((e) => console.log(`[XP] Failed to add XP | User: ${userID} | Err: ${e}`));

let level = user.level
let level = user.level;

xp = user.xp
xp = user.xp;

if (user.xp === 0 || Math.sign(user.xp) === -1) {
xp = 0
xp = 0;
}

if (level1 !== level) {
Expand All @@ -87,17 +88,17 @@ async function addXP(message, userID, guildID, xp) {
level,
userID,
guildID
}
};

let role = await roleSetup.find(client, guildID, level)
let role = await roleSetup.find(client, guildID, level);

client.emit('levelUp', message, data, role)
client.emit('levelUp', message, data, role);
}

return {
level,
xp
}
};
}

module.exports = addXP
module.exports = addXP;
Loading

0 comments on commit c567aa5

Please sign in to comment.