|
| 1 | +const config = require("./config.json"); |
| 2 | +const canvacord = require("canvacord"); |
| 3 | +const Discord = require("discord.js"); |
| 4 | + |
| 5 | +module.exports = function (client) { |
| 6 | + const description = { |
| 7 | + name: "leveling", |
| 8 | + filename: "leveling.js", |
| 9 | + version: "2.0" |
| 10 | + } |
| 11 | + //log that the module is loaded |
| 12 | + console.log(` :: ⬜️ Module: ${description.name} | Loaded version ${description.version} from ("${description.filename}")`) |
| 13 | + //voice state update event to check joining/leaving channels |
| 14 | + client.on("message", async (message) => { |
| 15 | + |
| 16 | + if (message.author.bot) return; |
| 17 | + if (!message.guild) return; |
| 18 | + if (message.channel.type === `dm`) return; |
| 19 | + ////////////////////////////////////////// |
| 20 | + /////////////RANKING SYSTEM/////////////// |
| 21 | + ////////////////////////////////////////// |
| 22 | + //get the key of the user for this guild |
| 23 | + const key = `${message.guild.id}-${message.author.id}`; |
| 24 | + // do some databasing |
| 25 | + client.points.ensure(`${message.guild.id}-${message.author.id}`, { |
| 26 | + user: message.author.id, |
| 27 | + guild: message.guild.id, |
| 28 | + points: 0, |
| 29 | + level: 1 |
| 30 | + }); |
| 31 | + //create message length basically math for not too much xp for too long messages |
| 32 | + var msgl = message.content.length / (Math.floor(Math.random() * (message.content.length - message.content.length / 100 + 1) + 10)); |
| 33 | + //if too short the message |
| 34 | + if (msgl < 10) { |
| 35 | + //get a random num between 0 and 2 rounded |
| 36 | + var randomnum = Math.floor((Math.random() * 2) * 100) / 100 |
| 37 | + //basically databasing again |
| 38 | + client.points.math(key, `+`, randomnum, `points`) |
| 39 | + client.points.inc(key, `points`); |
| 40 | + } |
| 41 | + //if not too short do this |
| 42 | + else { |
| 43 | + //get a random num between rounded but it belongs to message length |
| 44 | + var randomnum = 1 + Math.floor(msgl * 100) / 100 |
| 45 | + //basically databasing again |
| 46 | + client.points.math(key, `+`, randomnum, `points`) |
| 47 | + client.points.inc(key, `points`); |
| 48 | + } |
| 49 | + //get current level |
| 50 | + const curLevel = Math.floor(0.1 * Math.sqrt(client.points.get(key, `points`))); |
| 51 | + //if its a new level then do this |
| 52 | + if (client.points.get(key, `level`) < curLevel) { |
| 53 | + //define ranked embed |
| 54 | + const embed = new Discord.MessageEmbed() |
| 55 | + .setTitle(`Ranking of: ${message.author.username}`) |
| 56 | + .setTimestamp() |
| 57 | + .setDescription(`You've leveled up to Level: **\`${curLevel}\`**! (Points: \`${Math.floor(client.points.get(key, `points`) * 100) / 100}\`) `) |
| 58 | + .setColor("GREEN"); |
| 59 | + //send ping and embed message |
| 60 | + message.channel.send(`<@` + message.author.id + `>`); |
| 61 | + message.channel.send(embed); |
| 62 | + //set the new level |
| 63 | + client.points.set(key, curLevel, `level`); |
| 64 | + } |
| 65 | + //else continue or commands... |
| 66 | + // |
| 67 | + if (message.content.toLowerCase().startsWith(`${config.PREFIX}rank`)) { |
| 68 | + //get the rankuser |
| 69 | + rankuser = message.mentions.users.first() || message.author; |
| 70 | + //do some databasing |
| 71 | + const filtered = client.points.filter(p => p.guild === message.guild.id).array(); |
| 72 | + const sorted = filtered.sort((a, b) => b.points - a.points); |
| 73 | + const top10 = sorted.splice(0, message.guild.memberCount); |
| 74 | + let i = 0; |
| 75 | + //count server rank sometimes an error comes |
| 76 | + for (const data of top10) { |
| 77 | + await delay(15); |
| 78 | + try { |
| 79 | + i++; |
| 80 | + if (client.users.cache.get(data.user).tag === rankuser.tag) break; |
| 81 | + } catch { |
| 82 | + i = `Error counting Rank`; |
| 83 | + break; |
| 84 | + } |
| 85 | + } |
| 86 | + const key = `${message.guild.id}-${rankuser.id}`; |
| 87 | + //math |
| 88 | + let curpoints = Number(client.points.get(key, `points`).toFixed(2)); |
| 89 | + //math |
| 90 | + let curnextlevel = Number(((Number(1) + Number(client.points.get(key, `level`).toFixed(2))) * Number(10)) * ((Number(1) + Number(client.points.get(key, `level`).toFixed(2))) * Number(10))); |
| 91 | + //if not level == no rank |
| 92 | + if (client.points.get(key, `level`) === undefined) i = `No Rank`; |
| 93 | + //define a temporary embed so its not coming delayed |
| 94 | + let tempmsg = await message.channel.send(new Discord.MessageEmbed().setColor("RED").setAuthor("Calculating...", "https://cdn.discordapp.com/emojis/769935094285860894.gif")) |
| 95 | + //global local color var. |
| 96 | + let color; |
| 97 | + //define status of the rankuser |
| 98 | + let status = rankuser.presence.status; |
| 99 | + //do some coloring for user status cause cool |
| 100 | + if (status === "dnd") { color = "#ff0048"; } |
| 101 | + else if (status === "online") { color = "#00fa81"; } |
| 102 | + else if (status === "idle") { color = "#ffbe00"; } |
| 103 | + else { status = "streaming"; color = "#a85fc5"; } |
| 104 | + //define the ranking card |
| 105 | + const rank = new canvacord.Rank() |
| 106 | + .setAvatar(rankuser.displayAvatarURL({ dynamic: false, format: 'png' })) |
| 107 | + .setCurrentXP(Number(curpoints.toFixed(2)), color) |
| 108 | + .setRequiredXP(Number(curnextlevel.toFixed(2)), color) |
| 109 | + .setStatus(status, false, 7) |
| 110 | + .renderEmojis(true) |
| 111 | + .setProgressBar(color, "COLOR") |
| 112 | + .setRankColor(color, "COLOR") |
| 113 | + .setLevelColor(color, "COLOR") |
| 114 | + .setUsername(rankuser.username, color) |
| 115 | + .setRank(Number(i), "Rank", true) |
| 116 | + .setLevel(Number(client.points.get(key, `level`)), "Level", true) |
| 117 | + .setDiscriminator(rankuser.discriminator, color); |
| 118 | + rank.build() |
| 119 | + .then(async data => { |
| 120 | + //add rankcard to attachment |
| 121 | + const attachment = new Discord.MessageAttachment(data, "RankCard.png"); |
| 122 | + //define embed |
| 123 | + const embed = new Discord.MessageEmbed() |
| 124 | + .setTitle(`Ranking of: ${rankuser.username}`) |
| 125 | + .setColor(color) |
| 126 | + .setImage("attachment://RankCard.png") |
| 127 | + .attachFiles(attachment) |
| 128 | + //send that embed |
| 129 | + await message.channel.send(embed); |
| 130 | + //delete that temp message |
| 131 | + await tempmsg.delete(); |
| 132 | + return; |
| 133 | + }); |
| 134 | + } |
| 135 | + //leaderboard command |
| 136 | + if (message.content.toLowerCase() === `${config.PREFIX}leaderboard`) { |
| 137 | + //some databasing and math |
| 138 | + const filtered = client.points.filter(p => p.guild === message.guild.id).array(); |
| 139 | + const sorted = filtered.sort((a, b) => b.points - a.points); |
| 140 | + const top10 = sorted.splice(0, 10); |
| 141 | + const embed = new Discord.MessageEmbed() |
| 142 | + .setTitle(`${message.guild.name}: Leaderboard`) |
| 143 | + .setTimestamp() |
| 144 | + .setDescription(`Top 10 Ranking:`) |
| 145 | + .setColor("ORANGE"); |
| 146 | + //set counter to 0 |
| 147 | + let i = 0; |
| 148 | + //get rank |
| 149 | + for (const data of top10) { |
| 150 | + await delay(15); try { |
| 151 | + i++; |
| 152 | + embed.addField(`**${i}**. ${client.users.cache.get(data.user).tag}`, `Points: \`${Math.floor(data.points * 100) / 100}\` | Level: \`${data.level}\``); |
| 153 | + } catch { |
| 154 | + i++; //if usernot found just do this |
| 155 | + embed.addField(`**${i}**. ${client.users.cache.get(data.user)}`, `Points: \`${Math.floor(data.points * 100) / 100}\` | Level: \`${data.level}\``); |
| 156 | + } |
| 157 | + } |
| 158 | + //schick das embed |
| 159 | + return message.channel.send(embed); |
| 160 | + } |
| 161 | + |
| 162 | + }) |
| 163 | + function delay(delayInms) { |
| 164 | + return new Promise(resolve => { |
| 165 | + setTimeout(() => { |
| 166 | + resolve(2); |
| 167 | + }, delayInms); |
| 168 | + }); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +//Coded by Tomato#6966! |
0 commit comments