Added redis caching to guild top command

This commit is contained in:
2023-12-14 22:24:07 +01:00
parent 5631a0d3bc
commit b55b3e88f7

View File

@@ -1,7 +1,7 @@
const { getUUID, getPlayer, getGuild, getIGN } = require("../../utils/utils.js") const { getUUID, getPlayer, getGuild, getIGN } = require("../../utils/utils.js")
const { color, devMessage } = require("../../../config/options.json") const { color, devMessage } = require("../../../config/options.json")
const { admin } = require("../../../config/roles.json")
const { ChannelType } = require("discord.js") const { ChannelType } = require("discord.js")
const { redis } = require("../../utils/redis.js")
/** @param { import('discord.js').ChatInputCommandInteraction } interaction */ /** @param { import('discord.js').ChatInputCommandInteraction } interaction */
@@ -15,19 +15,7 @@ async function guildTop(interaction) {
if (interaction.channel.type === ChannelType.DM) { if (interaction.channel.type === ChannelType.DM) {
interaction.editReply({ interaction.editReply({
embeds: [{ embeds: [{
description: "You can't use this command in DMs!\n" + description: "You can't use this command in DMs!",
"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",
color: embedColor color: embedColor
}] }]
}) })
@@ -128,6 +116,9 @@ async function guildTop(interaction) {
const guildName = guild.name const guildName = guild.name
const guildMembers = guild.members const guildMembers = guild.members
const guildId = guild._id
const cachedData = await redis.get("guildTop+" + guildId)
const gexpTodayUnformatted = guildMembers.map((member) => { const gexpTodayUnformatted = guildMembers.map((member) => {
return member.expHistory[Object.keys(member.expHistory)[0]] return member.expHistory[Object.keys(member.expHistory)[0]]
@@ -152,6 +143,13 @@ async function guildTop(interaction) {
amount = 1 amount = 1
} }
let cacheStatus
let guildData = []
const fieldsValueRaw = []
const allMembersSorted = allMembersDailyGEXP.sort((a, b) => b.gexp - a.gexp)
if (!cachedData) {
cacheStatus = false
await interaction.editReply({ await interaction.editReply({
embeds: [{ embeds: [{
description: "Fetching the top " + amount + " members of " + guildName + "...", description: "Fetching the top " + amount + " members of " + guildName + "...",
@@ -159,15 +157,35 @@ async function guildTop(interaction) {
}] }]
}) })
const fieldsValueRaw = [] for (let i = 0; i < allMembersSorted.length; i++) {
const allMembersSorted = allMembersDailyGEXP.sort((a, b) => b.gexp - a.gexp) const ign = await getIGN(allMembersSorted[i].uuid)
const topMembers = allMembersSorted.slice(0, amount) 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 const sliceSize = amount / 3
for (let i = 0; i < amount; i++) { for (let i = 0; i < amount; i++) {
const ign = await getIGN(topMembers[i].uuid) const ign = topMembers[i].ign
const gexpUnformatted = topMembers[i].gexp const gexp = topMembers[i].gexp
const gexp = new Intl.NumberFormat("en-US").format(gexpUnformatted)
const position = i + 1 const position = i + 1
@@ -189,6 +207,7 @@ async function guildTop(interaction) {
const footerText = interaction.guild ? interaction.guild.name : interaction.user.username 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 footerIcon = interaction.guild ? interaction.guild.iconURL({ dynamic: true }) : interaction.user.avatarURL({ dynamic: true })
const cacheStatusText = cacheStatus ? " | [Cache]" : ""
await interaction.editReply({ await interaction.editReply({
embeds: [{ embeds: [{
@@ -198,7 +217,7 @@ async function guildTop(interaction) {
color: embedColor, color: embedColor,
fields: newList, fields: newList,
footer: { footer: {
text: footerText + " | " + devMessage, text: footerText + " | " + devMessage + cacheStatusText,
icon_url: footerIcon icon_url: footerIcon
} }
}] }]