From 7a31a2e50a5a5c912b07e32d84664ea65d6f7a9d Mon Sep 17 00:00:00 2001 From: WubzyGD Date: Sat, 20 Feb 2021 20:42:36 -0700 Subject: [PATCH] XP gaining and levelup --- events/message.js | 17 +++++++++++++++++ util/lxp/gainxp.js | 23 +++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 util/lxp/gainxp.js diff --git a/events/message.js b/events/message.js index e25f04c..22bce82 100644 --- a/events/message.js +++ b/events/message.js @@ -5,6 +5,7 @@ const wait = require('../util/wait'); const UserData = require('../models/user'); const AR = require('../models/ar'); +const LXP = require('../models/localxp'); module.exports = async (client, message) => { if (message.author.bot) {return undefined;} @@ -48,6 +49,22 @@ module.exports = async (client, message) => { }); } + if (message.guild && client.misc.cache.lxp.enabled.includes(message.guild.id)) { + LXP.findOne({gid: message.guild.id}).then(xp => { + if (!client.misc.cache.lxp.xp[message.guild.id]) {client.misc.cache.lxp.xp[message.guild.id] = {};} + if (!client.misc.cache.lxp.xp[message.guild.id][message.author.id]) {client.misc.cache.lxp.xp[message.guild.id][message.author.id] = { + xp: xp.xp[message.author.id] ? xp.xp[message.author.id][0] : 0, + level: xp.xp[message.author.id] ? xp.xp[message.author.id][1] : 1, + lastXP: new Date().getTime() - 60000 + };} + if (new Date().getTime() - client.misc.cache.lxp.xp[message.guild.id][message.author.id].lastXP > 60000) { + require('../util/lxp/gainxp')(client, message.member.id, message.channel); + } + }); + } + + + try { if (msg.startsWith(prefix) || msg.startsWith(`<@${client.user.id}>`) || msg.startsWith(`<@!${client.user.id}>`)) { let command = client.commands.get(cmd) || client.commands.get(client.aliases.get(cmd)); diff --git a/util/lxp/gainxp.js b/util/lxp/gainxp.js new file mode 100644 index 0000000..cfca254 --- /dev/null +++ b/util/lxp/gainxp.js @@ -0,0 +1,23 @@ +const LXP = require('../../models/localxp'); + +module.exports = async (client, member, channel) => { + client.misc.cache.lxp.xp[channel.guild.id][member].lastXP = new Date().getTime(); + client.misc.cache.lxp.xp[channel.guild.id][member].xp += 10; + + let x = client.misc.cache.lxp.xp[channel.guild.id][member].level; + let max = Math.ceil(100 + (((x / 3) ** 1.4) * 1.3)); + + if (client.misc.cache.lxp.xp[channel.guild.id][member].xp > max) { + client.misc.cache.lxp.xp[channel.guild.id][member].xp -= max; + client.misc.cache.lxp.xp[channel.guild.id][member].level += 1; + + LXP.findOne({gid: channel.guild.id}).then(async xp => { + if (!xp || !xp.msg) {return;} + try { + let ch = xp.lvch.length ? channel.guild.channels.cache.get(xp.lvch) : channel; + if (ch.partial) {await ch.fetch().catch(() => {});} + if (ch && ch.permissionsFor(ch.guild.me.id).has('SEND_MESSAGES')) {ch.send(`<:awoo:560193779764559896> <@${member}> has reached **Level ${x + 1}**!`).catch((e) => {/*console.error(e)*/});} + } catch (e) {/*console.error(e);*/} + }).catch((e) => {/*console.error(e);*/}) + } +}; \ No newline at end of file