diff --git a/package.json b/package.json index 46dbba2..641c628 100644 --- a/package.json +++ b/package.json @@ -18,6 +18,7 @@ "axios": "^1.3.4", "discord.js": "^14.8.0", "dotenv": "^16.0.3", + "ioredis": "^5.3.2", "log-beautify": "^1.2.0", "mongoose": "^7.0.1", "ms": "^2.1.3", diff --git a/src/commands/guild/top.js b/src/commands/guild/top.js index 00fffac..44c7ba5 100644 --- a/src/commands/guild/top.js +++ b/src/commands/guild/top.js @@ -1,7 +1,7 @@ const { getUUID, getPlayer, getGuild, getIGN } = require("../../utils/utils.js") const { color, devMessage } = require("../../../config/options.json") -const { admin } = require("../../../config/roles.json") const { ChannelType } = require("discord.js") +const { redis } = require("../../utils/redis.js") /** @param { import('discord.js').ChatInputCommandInteraction } interaction */ @@ -15,19 +15,7 @@ async function guildTop(interaction) { if (interaction.channel.type === ChannelType.DM) { interaction.editReply({ embeds: [{ - description: "You can't use this command in DMs!\n" + - "While taken checks will this rate limit the bot", - color: embedColor - }] - }) - return - } - - if (!interaction.member.roles.cache.has(admin)) { - await interaction.editReply({ - embeds: [{ - description: "Command temporarily disabled\n" + - "While taken checks will this rate limit the bot", + description: "You can't use this command in DMs!", color: embedColor }] }) @@ -128,6 +116,9 @@ async function guildTop(interaction) { const guildName = guild.name const guildMembers = guild.members + const guildId = guild._id + + const cachedData = await redis.get("guildTop+" + guildId) const gexpTodayUnformatted = guildMembers.map((member) => { return member.expHistory[Object.keys(member.expHistory)[0]] @@ -152,22 +143,49 @@ async function guildTop(interaction) { amount = 1 } - await interaction.editReply({ - embeds: [{ - description: "Fetching the top " + amount + " members of " + guildName + "...", - color: embedColor - }] - }) - + let cacheStatus + let guildData = [] const fieldsValueRaw = [] const allMembersSorted = allMembersDailyGEXP.sort((a, b) => b.gexp - a.gexp) - const topMembers = allMembersSorted.slice(0, amount) + + if (!cachedData) { + cacheStatus = false + await interaction.editReply({ + embeds: [{ + description: "Fetching the top " + amount + " members of " + guildName + "...", + color: embedColor + }] + }) + + for (let i = 0; i < allMembersSorted.length; i++) { + const ign = await getIGN(allMembersSorted[i].uuid) + const gexpUnformatted = allMembersSorted[i].gexp + const gexp = new Intl.NumberFormat("en-US").format(gexpUnformatted) + + guildData.push({ + ign: ign, + gexp: gexp, + }) + } + + await redis.set("guildTop+" + guildId, JSON.stringify(guildData), "EX", 60 * 30) + } else { + cacheStatus = true + await interaction.editReply({ + embeds: [{ + description: "Fetching the top " + amount + " members of " + guildName + "using cache...", + color: embedColor, + }] + }) + guildData = JSON.parse(cachedData) + } + + const topMembers = guildData.slice(0, amount) const sliceSize = amount / 3 for (let i = 0; i < amount; i++) { - const ign = await getIGN(topMembers[i].uuid) - const gexpUnformatted = topMembers[i].gexp - const gexp = new Intl.NumberFormat("en-US").format(gexpUnformatted) + const ign = topMembers[i].ign + const gexp = topMembers[i].gexp const position = i + 1 @@ -189,6 +207,7 @@ async function guildTop(interaction) { const footerText = interaction.guild ? interaction.guild.name : interaction.user.username const footerIcon = interaction.guild ? interaction.guild.iconURL({ dynamic: true }) : interaction.user.avatarURL({ dynamic: true }) + const cacheStatusText = cacheStatus ? " | [Cache]" : "" await interaction.editReply({ embeds: [{ @@ -198,7 +217,7 @@ async function guildTop(interaction) { color: embedColor, fields: newList, footer: { - text: footerText + " | " + devMessage, + text: footerText + " | " + devMessage + cacheStatusText, icon_url: footerIcon } }] diff --git a/src/index.js b/src/index.js index bc82728..36bea53 100644 --- a/src/index.js +++ b/src/index.js @@ -5,6 +5,7 @@ const { init } = require("./utils/init.js") require("dotenv").config() const mongoURI = process.env.MONGOURI const { connect } = require("mongoose") +const { redis } = require("./utils/redis.js") init() @@ -39,6 +40,10 @@ if (process.env.NODE_ENV === "dev") { client.login(token) +redis.on("ready", () => { + console.log("Connected to Redis") +}) + connect(mongoURI, {}).then(() => { console.log("Connected to MongoDB") }) diff --git a/src/utils/redis.js b/src/utils/redis.js new file mode 100644 index 0000000..70a3716 --- /dev/null +++ b/src/utils/redis.js @@ -0,0 +1,5 @@ +const { Redis } = require("ioredis") + +const redis = new Redis(process.env.REDISURI) + +module.exports = { redis } diff --git a/yarn.lock b/yarn.lock index a1381d2..8212898 100644 --- a/yarn.lock +++ b/yarn.lock @@ -72,6 +72,11 @@ resolved "https://registry.yarnpkg.com/@fastify/busboy/-/busboy-2.1.0.tgz#0709e9f4cb252351c609c6e6d8d6779a8d25edff" integrity sha512-+KpH+QxZU7O4675t3mnkQKcZZg56u+K/Ct2K+N2AZYNVK8kyeo/bI18tI8aPm3tvNNRyTWfj6s5tnGNlcbQRsA== +"@ioredis/commands@^1.1.1": + version "1.2.0" + resolved "https://registry.yarnpkg.com/@ioredis/commands/-/commands-1.2.0.tgz#6d61b3097470af1fdbbe622795b8921d42018e11" + integrity sha512-Sx1pU8EM64o2BrqNpEO1CNLtKQwyhuXuqyfH7oGKCk+1a33d2r5saW8zNwm3j6BTExtjrv2BxTgzzkMwts6vGg== + "@mongodb-js/saslprep@^1.1.0": version "1.1.1" resolved "https://registry.yarnpkg.com/@mongodb-js/saslprep/-/saslprep-1.1.1.tgz#9a6c2516bc9188672c4d953ec99760ba49970da7" @@ -170,6 +175,11 @@ chalk@^3.0.0: ansi-styles "^4.1.0" supports-color "^7.1.0" +cluster-key-slot@^1.1.0: + version "1.1.2" + resolved "https://registry.yarnpkg.com/cluster-key-slot/-/cluster-key-slot-1.1.2.tgz#88ddaa46906e303b5de30d3153b7d9fe0a0c19ac" + integrity sha512-RMr0FhtfXemyinomL4hrWcYJxmX6deFdCxpJzhDttxgO1+bcCnkk+9drydLVDmAMG7NE6aN/fl4F7ucU/90gAA== + color-convert@^2.0.1: version "2.0.1" resolved "https://registry.yarnpkg.com/color-convert/-/color-convert-2.0.1.tgz#72d3a68d598c9bdb3af2ad1e84f21d896abd4de3" @@ -194,7 +204,7 @@ combined-stream@^1.0.8: dependencies: delayed-stream "~1.0.0" -debug@4.x: +debug@4.x, debug@^4.3.4: version "4.3.4" resolved "https://registry.yarnpkg.com/debug/-/debug-4.3.4.tgz#1319f6579357f2338d3337d2cdd4914bb5dcc865" integrity sha512-PRWFHuSU3eDtQJPvnNY7Jcket1j0t5OuOsFzPPzsekD52Zl8qUfFIPEiswXqIvHWGVHOgX+7G/vCNNhehwxfkQ== @@ -206,6 +216,11 @@ delayed-stream@~1.0.0: resolved "https://registry.yarnpkg.com/delayed-stream/-/delayed-stream-1.0.0.tgz#df3ae199acadfb7d440aaae0b29e2272b24ec619" integrity sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ== +denque@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/denque/-/denque-2.1.0.tgz#e93e1a6569fb5e66f16a3c2a2964617d349d6ab1" + integrity sha512-HVQE3AAb/pxF8fQAoiqpvg9i3evqug3hoiwakOyZAwJm+6vZehbkYXZ0l4JxS+I3QxM97v5aaRNhj8v5oBhekw== + discord-api-types@0.37.61: version "0.37.61" resolved "https://registry.yarnpkg.com/discord-api-types/-/discord-api-types-0.37.61.tgz#9dd8e58c624237e6f1b23be2d29579af268b8c5b" @@ -260,6 +275,21 @@ has-flag@^4.0.0: resolved "https://registry.yarnpkg.com/has-flag/-/has-flag-4.0.0.tgz#944771fd9c81c81265c4d6941860da06bb59479b" integrity sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ== +ioredis@^5.3.2: + version "5.3.2" + resolved "https://registry.yarnpkg.com/ioredis/-/ioredis-5.3.2.tgz#9139f596f62fc9c72d873353ac5395bcf05709f7" + integrity sha512-1DKMMzlIHM02eBBVOFQ1+AolGjs6+xEcM4PDL7NqOS6szq7H9jSaEkIUH6/a5Hl241LzW6JLSiAbNvTQjUupUA== + dependencies: + "@ioredis/commands" "^1.1.1" + cluster-key-slot "^1.1.0" + debug "^4.3.4" + denque "^2.1.0" + lodash.defaults "^4.2.0" + lodash.isarguments "^3.1.0" + redis-errors "^1.2.0" + redis-parser "^3.0.0" + standard-as-callback "^2.1.0" + ip@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/ip/-/ip-2.0.0.tgz#4cf4ab182fee2314c75ede1276f8c80b479936da" @@ -270,6 +300,16 @@ kareem@2.5.1: resolved "https://registry.yarnpkg.com/kareem/-/kareem-2.5.1.tgz#7b8203e11819a8e77a34b3517d3ead206764d15d" integrity sha512-7jFxRVm+jD+rkq3kY0iZDJfsO2/t4BBPeEb2qKn2lR/9KhuksYk5hxzfRYWMPV8P/x2d0kHD306YyWLzjjH+uA== +lodash.defaults@^4.2.0: + version "4.2.0" + resolved "https://registry.yarnpkg.com/lodash.defaults/-/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c" + integrity sha512-qjxPLHd3r5DnsdGacqOMU6pb/avJzdh9tFX2ymgoZE27BmjXrNy/y4LoaiTeAb+O3gL8AfpJGtqfX/ae2leYYQ== + +lodash.isarguments@^3.1.0: + version "3.1.0" + resolved "https://registry.yarnpkg.com/lodash.isarguments/-/lodash.isarguments-3.1.0.tgz#2f573d85c6a24289ff00663b491c1d338ff3458a" + integrity sha512-chi4NHZlZqZD18a0imDHnZPrDeBbTtVN7GXMwuGdRH9qotxAjYs3aVLKc7zNOG9eddR5Ksd8rvFEBc9SsggPpg== + lodash.snakecase@4.1.1: version "4.1.1" resolved "https://registry.yarnpkg.com/lodash.snakecase/-/lodash.snakecase-4.1.1.tgz#39d714a35357147837aefd64b5dcbb16becd8f8d" @@ -386,6 +426,18 @@ punycode@^2.1.1: resolved "https://registry.yarnpkg.com/punycode/-/punycode-2.3.1.tgz#027422e2faec0b25e1549c3e1bd8309b9133b6e5" integrity sha512-vYt7UD1U9Wg6138shLtLOvdAu+8DsC/ilFtEVHcH+wydcSpNE20AfSOduf6MkRFahL5FY7X1oU7nKVZFtfq8Fg== +redis-errors@^1.0.0, redis-errors@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad" + integrity sha512-1qny3OExCf0UvUV/5wpYKf2YwPcOqXzkwKKSmKHiE6ZMQs5heeE/c8eXK+PNllPvmjgAbfnsbpkGZWy8cBpn9w== + +redis-parser@^3.0.0: + version "3.0.0" + resolved "https://registry.yarnpkg.com/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4" + integrity sha512-DJnGAeenTdpMEH6uAJRK/uiyEIH9WVsUmoLwzudwGJUwZPp80PDBWPHXSAGNPwNvIXAbe7MSUB1zQFugFml66A== + dependencies: + redis-errors "^1.0.0" + sift@16.0.1: version "16.0.1" resolved "https://registry.yarnpkg.com/sift/-/sift-16.0.1.tgz#e9c2ccc72191585008cf3e36fc447b2d2633a053" @@ -411,6 +463,11 @@ sparse-bitfield@^3.0.3: dependencies: memory-pager "^1.0.2" +standard-as-callback@^2.1.0: + version "2.1.0" + resolved "https://registry.yarnpkg.com/standard-as-callback/-/standard-as-callback-2.1.0.tgz#8953fc05359868a77b5b9739a665c5977bb7df45" + integrity sha512-qoRRSyROncaz1z0mvYqIE4lCd9p2R90i6GxW3uZv5ucSu8tU7B5HXUP1gG8pVZsYNVaXjk8ClXHPttLyxAL48A== + supports-color@^7.1.0: version "7.2.0" resolved "https://registry.yarnpkg.com/supports-color/-/supports-color-7.2.0.tgz#1b7dcdcb32b8138801b3e478ba6a51caa89648da"